Mycat配置水平垂直拆分
垂直拆分分库
配置schema.xml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"> <table name="order" dataNode="dn2"/> </schema> <dataNode name="dn1" dataHost="host1" database="mybatis" /> <dataNode name="dn2" dataHost="host2" database="mybatis" /> <dataHost name="host1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" url="localhost:3306" user="root" password="123456"> </writeHost> </dataHost> <dataHost name="host2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM2" url="localhost:3307" user="root" password="123456"> </writeHost> </dataHost> </mycat:schema>
|
水平拆分分表
配置schema.xml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1"> <table name="order" dataNode="dn1,dn2" rule="mod_rule">
<childTable name-"order_detail" primaryKey="id" joinKey="order_id" parentKey="id"></childTable> </table> </schema> <dataNode name="dn1" dataHost="host1" database="mybatis" /> <dataNode name="dn2" dataHost="host2" database="mybatis" /> <dataHost name="host1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" url="localhost:3306" user="root" password="123456"> </writeHost> </dataHost> <dataHost name="host2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM2" url="localhost:3307" user="root" password="123456"> </writeHost> </dataHost> </mycat:schema>
|
rule.xml配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mycat:rule SYSTEM "rule.dtd"> <mycat:rule xmlns:mycat="http://io.mycat/"> <tableRule name="mod_rule"> <rule> <columns>user_id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod"> <property name="count">2</property> </function> </mycat:rule>
|
分片规则
常用的分片规则有取模、分片枚举
取模
对分片字段进行取模运算
1 2 3 4 5 6 7 8 9 10 11
| <tableRule name="mod-long"> <rule> <columns>id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule>
<function name="mod-long" class="io.mycat.route.function.PartitionByMod"> <property name="count">3</property> </function>
|
分片枚举
在配置文件中配置可能的枚举,自己配置分片,适用于特定的场景,如保险中分省保存数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <tableRule name="sharding-by-intfile"> <rule> <columns>sharding_id</columns> <algorithm>hash-int</algorithm> </rule> </tableRule>
<function name="hash-int" class="io.mycat.route.function.PartitionByFileMap"> <property name="mapFile">partition-hash-int.txt</property> <property name="type">0</property> <property name="defaultNode">0</property> </function>
|
partition-hash-int.txt中配置的内容
范围分片
分片字段某个范围属于某个分片
1 2 3 4 5 6 7 8 9 10 11 12 13
| <tableRule name="auto-sharding-long"> <rule> <columns>id</columns> <algorithm>rang-long</algorithm> </rule> </tableRule>
<function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong"> <property name="mapFile">autopartition-long.txt</property> <property name="defaultNode">0</property> </function>
|
autopartition-long.txt中配置
1 2 3
| 2000001-4000000=1 0-2000000=0 4000001-8000000=2
|
日期分片
按照天进行分片
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <tableRule name="sharding-by-date"> <rule> <columns>createTime</columns> <algorithm>partbyday</algorithm> </rule> </tableRule>
<function name="partbyday" class="io.mycat.route.function.PartitionByDate"> <property name="dateFormat">yyyy-MM-dd</property> <property name="sNaturalDay">0</property> <property name="sBeginDate">2014-01-01</property> <property name="sEndDate">2014-01-31</property> <property name="sPartionDay">10</property> </function>
|