Mycat分庫分表配置

hooca發表於2021-02-04

以下為Mycat為三臺Mysql資料庫分庫分表的配置,三臺資料庫主機名node0-2,資料庫名db01-03.


以下是Mycat的schema.xml配置檔案:

        <schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" randomDataNode="dn1">
                <!-- auto sharding by id (long) -->
                <!--splitTableNames 啟用<table name 屬性使用逗號分割配置多個表,即多個表使用這個配置-->
                <table name="users" primaryKey="id" dataNode="dn1" />
                <table name="item" primaryKey="id" dataNode="dn2,dn3" rule="rule1" />
                <table name="customer" dataNode="dn1,dn2,dn3" rule="auto-sharding-long-custom">
                <childTable name="orders" 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 name="dn1" dataHost="localhost1" database="db01" />
        <dataNode name="dn2" dataHost="localhost2" database="db02" />
        <dataNode name="dn3" dataHost="localhost3" database="db03" />
        <!--<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" /> -->
        <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="node0:3306" user="root" password="root123">
                </writeHost>
                <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
        </dataHost>
        <dataHost name="localhost2" 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="hostM2" url="node1:3306" user="root" password="root123">
                </writeHost>
                <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
        </dataHost>
        <dataHost name="localhost3" 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="hostM3" url="node2:3306" user="root" password="root123">
                </writeHost>
                <!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
        </dataHost>

然後編輯rule.xml:

        <tableRule name="rule1">
                <rule>
                        <columns>id</columns>
                        <algorithm>mod-long</algorithm>
                </rule>
        </tableRule>
        
        <tableRule name="auto-sharding-long-custom">
                <rule>
                        <columns>id</columns>
                        <algorithm>rang-long</algorithm>
                </rule>
        </tableRule>
        
        <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
                <!-- how many data nodes -->
                <property name="count">2</property>
        </function>
        
        <function name="rang-long"
                          class="io.mycat.route.function.AutoPartitionByLong">
                <property name="mapFile">auto-sharding-long-custom.txt</property>
        </function>

以上配置說明:

mod-long演算法,即按餘數分配,一共2臺。

rang-long,按範圍分配。

建立auto-sharding-long-custom.txt檔案:

cat auto-sharding-long-custom.txt
0-1000=0
1000-2000=1
2000-3000=2

表示0-1000分配到0號datanode,1000-2000到1號datanode,2000-3000到2號datanode。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22621861/viewspace-2756123/,如需轉載,請註明出處,否則將追究法律責任。

相關文章