MyCat配置檔案詳解--schema.xml
阿新 • • 發佈:2021-06-18
schema.xml 涵蓋了MyCat的邏輯庫、表、分片規則、分片節點及資料來源。
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <!-- schema 定義mycat中的邏輯庫,可以有多個邏輯庫, 1)dataNode屬性:繫結邏輯庫到具體的Database上面, 2)checkSQLschema:如果為true,則會替換掉schema,如果為false則不會; 3)sqlMaxLimit:如果帶了該屬性,則每次執行sql的時候如果sql沒有limit則會帶上這個limit,如果schema為非拆分庫,則該屬性不會生效。--> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> <!-- table標籤定義了MyCat中的邏輯表,所有拆分的表都需要在table標籤中定義。 --> <!-- 1)name屬性:定義邏輯表的名稱--> <!-- 2)dataNode屬性:定義邏輯表所屬的dataNode,如果需要引用多個dataNode,則可以用dataNode="dn$0-99" 來代表dn0到dn99的資料庫--> <!-- 3)rule屬性:用來指定邏輯表使用的規則名字,規則名字在rule.xml中定義。--> <!-- 4)ruleRequired屬性:該屬性用於指定表是否繫結分片規則,如果配置為true,但是沒有具體的分片規則,則會報錯。--> <!-- 5)type屬性:定義邏輯表的型別,分為"全域性表(global)"和"普通表"兩種型別,不設定該值的時候未global的所有表。--> <!-- 6)autoIncrement屬性:使用該值的時候需要定義auto_increment,使用的時候最好配合資料庫模式的全域性序列。--> <!-- 7)subTables屬性:dataNode在分表的條件下只能配置一個,不支援各種條件的Join關聯查詢。--> <!-- 8)primaryKey屬性:邏輯表對應真實表的主鍵。--> <!-- 9)needAddLimit屬性:指定表是否需要欄位再每個語句的後面加上limit限制。--> <table name="travelrecord" dataNode="dn1,dn2,dn3" rule="auto-sharding-long" /> <!-- global table is auto cloned to all defined data nodes ,so can join with any table whose sharding node is in the same data node --> <table name="company" primaryKey="ID" type="global" dataNode="dn1,dn2,dn3" /> <table name="goods" primaryKey="ID" type="global" dataNode="dn1,dn2" /> <!-- random sharding using mod sharind rule --> <table name="hotnews" primaryKey="ID" autoIncrement="true" dataNode="dn1,dn2,dn3" rule="mod-long" /> <!-- <table name="dual" primaryKey="ID" dataNode="dnx,dnoracle2" type="global" needAddLimit="false"/> <table name="worker" primaryKey="ID" dataNode="jdbc_dn1,jdbc_dn2,jdbc_dn3" rule="mod-long" /> --> <table name="employee" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile" /> <table name="customer" primaryKey="ID" dataNode="dn1,dn2" rule="sharding-by-intfile"> <!--childTable標籤用於定義E-R分片的子表,通過標籤上的屬性與浮表進行關聯--> <!-- 1)name屬性:定義子表的名稱--> <!-- 2)joinKey屬性:插入子表時,回使用這個值查詢浮表存貯的資料節點--> <!-- 3)parentKey屬性:與父表建立關聯關係的列名,程式首先獲取joinKey的值,然後通過parentKey屬性指定的列名產生查詢語句,通過執行語句得知父表儲存在哪個分片上,從而確定子表存貯的位置。--> <!-- 4)primaryKey:和table標籤一樣--> <!-- 5)needAddLimit:和table標籤一樣--> <childTable name="orders" primaryKey="ID" joinKey="customer_id" parentKey="id"> <childTable name="order_items" joinKey="order_id" parentKey="id" /> </childTable> <childTable name="customer_addr" primaryKey="ID" joinKey="customer_id" parentKey="id" /> </table> <!-- <table name="oc_call" primaryKey="ID" dataNode="dn1$0-743" rule="latest-month-calldate" /> --> </schema> <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743" /> --> <!-- dataNode標籤定義了mycat中的資料節點,這也就是我們通常所說的資料分片,一個單獨的dataNode就是一個獨立的資料分片--> <!--1)name屬性:定義資料節點的唯一名字--> <!--2)dataHost屬性:定義該分片所屬的資料庫例項,屬性引用自dataHost標籤上定義的name屬性--> <!--3)database屬性:定義該分片所屬的資料庫例項上的具體資料庫。--> <dataNode name="dn1" dataHost="localhost1" database="db1" /> <dataNode name="dn2" dataHost="localhost1" database="db2" /> <dataNode name="dn3" dataHost="localhost1" database="db3" /> <!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" /> <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" /> <dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" /> <dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" /> --> <!-- 定義資料庫例項,讀寫分離和心跳語句--> <!--1)那麼屬性:標識唯一的dataHost,--> <!-- 2)maxCon屬性:指定每個讀寫例項連線池的最大連線數。內嵌writeHost、readHost標籤會使用這個屬性的值來例項化連線池的最大連線數--> <!--3)minCon屬性:指定每個讀寫例項連線池的最小連線數。初始化連線池的大小的屬性。--> <!--4)balance屬性:負債均衡型別,有四種--> <!-- balance="0" : 不開啟讀寫分離機制,所有的讀操作都發送到當前可以用的writeHost上--> <!-- balance="1" : 全部的readHost與stand by writeHost(雙主從模式下的master) 都參與select語句的負債均衡--> <!-- balance="2" : 所有的讀操作都隨機的往writeHost和readHost上分發--> <!-- balance="3" : 所有的讀分發到readHost上,writeHost負責寫--> <!--5)writeType屬性:負載均衡目前的取值有兩種:--> <!-- writeType="0":所有的寫操作都發送到第一個writeHost,writeHost1掛了,則切換到writeHost2上,重新恢復writeHost1後,還是以writeHost2為準--> <!-- writeType="1":所有的寫操作都隨機的傳送到配置的writeHost上,1.5版本以後不推薦使用該值。--> <!--6)dbType屬性:制定後端後端資料的型別:mysql,oracle、mongoDB--> <!--7)dbDriver屬性:制定後端資料庫使用的Driver.目前可選的值為native和JDBC。--> <!--8)switchType屬性:預設值為1,自動切換。--> <!-- -1表示不自動切換--> <!-- 2表示基於mysql主從同步的狀態決定是否切換。--> <!-- 3表示基於mysql galaxy cluster 的切換機制--> <!--9)tempReadHostAvailable屬性:如果配置了writeHost屬性,下面的readHost依舊可以使用,預設為0--> <dataHost name="localhost1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <!-- 用於後端資料庫心跳檢測的語句--> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="hostM1" url="localhost:3306" user="root" password="123456"> <!-- can have multi read hosts --> <readHost host="hostS2" url="192.168.1.200:3306" user="root" password="xxx" /> </writeHost> <writeHost host="hostS1" url="localhost:3316" user="root" password="123456" /> <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> --> </dataHost> <!-- <dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc"> <heartbeat> </heartbeat> <writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng" password="jifeng"></writeHost> </dataHost> <dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat> <connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql> <writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base" password="123456" > </writeHost> </dataHost> <dataHost name="jdbchost" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost> <dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc"> <heartbeat> </heartbeat> <writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng" password="jifeng"></writeHost> </dataHost> --> <!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql" dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost> </dataHost> --> </mycat:schema>