前言:
分库分表,在本节里是水平切分,就是多个数据库里包含的表是一模一样的。
只是把字段散列的分到不同的库中。
实践:
1.修改schema.xml
这里是在同一台服务器上建立了4个数据库db1,db2,db3,db4
1 <?xml version="1.0"?> 2 <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> 3 <mycat:schema xmlns:mycat="http://io.mycat/"> 4 5 <!-- 配置逻辑库XMDB --> 6 <schema name="XMDB" checkSQLschema="false" sqlMaxLimit="100"> 7 <table name="user" dataNode="dn1,dn2,dn3,dn4" rule="crc32slot"/> 8 </schema> 9 <dataNode name="dn1" dataHost="node1" database="db1"/> 10 <dataNode name="dn2" dataHost="node1" database="db2"/> 11 <dataNode name="dn3" dataHost="node1" database="db3"/> 12 <dataNode name="dn4" dataHost="node1" database="db4"/> 13 <dataHost name="node1" maxCon="1000" minCon="10" balance="1" dbType="mysql" dbDriver="native"> 14 <heartbeat>select user()</heartbeat> 15 <writeHost host="192.168.40.3" url="192.168.40.3:3306" user="root" password="123456"> 16 <readHost host="192.168.40.3" url="192.168.40.3:3316" user="root" password="123456" /> 17 </writeHost> 18 </dataHost> 19 20 21 <!-- 22 <dataHost name="sequoiadb1" maxCon="1000" minCon="1" balance="0" dbType="sequoiadb" dbDriver="jdbc"> 23 <heartbeat> </heartbeat> 24 <writeHost host="hostM1" url="sequoiadb://1426587161.dbaas.sequoialab.net:11920/SAMPLE" user="jifeng" password="jifeng"></writeHost> 25 </dataHost> 26 27 <dataHost name="oracle1" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="oracle" dbDriver="jdbc"> <heartbeat>select 1 from dual</heartbeat> 28 <connectionInitSql>alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'</connectionInitSql> 29 <writeHost host="hostM1" url="jdbc:oracle:thin:@127.0.0.1:1521:nange" user="base" password="123456" > </writeHost> </dataHost> 30 31 <dataHost name="jdbchost" maxCon="1000" minCon="1" balance="0" writeType="0" dbType="mongodb" dbDriver="jdbc"> 32 <heartbeat>select user()</heartbeat> 33 <writeHost host="hostM" url="mongodb://192.168.0.99/test" user="admin" password="123456" ></writeHost> </dataHost> 34 35 <dataHost name="sparksql" maxCon="1000" minCon="1" balance="0" dbType="spark" dbDriver="jdbc"> 36 <heartbeat> </heartbeat> 37 <writeHost host="hostM1" url="jdbc:hive2://feng01:10000" user="jifeng" password="jifeng"></writeHost> </dataHost> --> 38 39 <!-- <dataHost name="jdbchost" maxCon="1000" minCon="10" balance="0" dbType="mysql" 40 dbDriver="jdbc"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" 41 url="jdbc:mysql://localhost:3306" user="root" password="123456"> </writeHost> 42 </dataHost> --> 43 </mycat:schema>
2.rule.xml
对应的rule
<property name="count">4</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 -->
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); 3 - you may not use this file except in compliance with the License. - You 4 may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 5 - - Unless required by applicable law or agreed to in writing, software - 6 distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 7 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 8 License for the specific language governing permissions and - limitations 9 under the License. --> 10 <!DOCTYPE mycat:rule SYSTEM "rule.dtd"> 11 <mycat:rule xmlns:mycat="http://io.mycat/"> 12 <tableRule name="rule1"> 13 <rule> 14 <columns>id</columns> 15 <algorithm>func1</algorithm> 16 </rule> 17 </tableRule> 18 19 <tableRule name="rule2"> 20 <rule> 21 <columns>user_id</columns> 22 <algorithm>func1</algorithm> 23 </rule> 24 </tableRule> 25 26 <tableRule name="sharding-by-intfile"> 27 <rule> 28 <columns>sharding_id</columns> 29 <algorithm>hash-int</algorithm> 30 </rule> 31 </tableRule> 32 <tableRule name="auto-sharding-long"> 33 <rule> 34 <columns>id</columns> 35 <algorithm>rang-long</algorithm> 36 </rule> 37 </tableRule> 38 <tableRule name="mod-long"> 39 <rule> 40 <columns>id</columns> 41 <algorithm>mod-long</algorithm> 42 </rule> 43 </tableRule> 44 <tableRule name="sharding-by-murmur"> 45 <rule> 46 <columns>id</columns> 47 <algorithm>murmur</algorithm> 48 </rule> 49 </tableRule> 50 <tableRule name="crc32slot"> 51 <rule> 52 <columns>id</columns> 53 <algorithm>crc32slot</algorithm> 54 </rule> 55 </tableRule> 56 <tableRule name="sharding-by-month"> 57 <rule> 58 <columns>create_time</columns> 59 <algorithm>partbymonth</algorithm> 60 </rule> 61 </tableRule> 62 <tableRule name="latest-month-calldate"> 63 <rule> 64 <columns>calldate</columns> 65 <algorithm>latestMonth</algorithm> 66 </rule> 67 </tableRule> 68 69 <tableRule name="auto-sharding-rang-mod"> 70 <rule> 71 <columns>id</columns> 72 <algorithm>rang-mod</algorithm> 73 </rule> 74 </tableRule> 75 76 <tableRule name="jch"> 77 <rule> 78 <columns>id</columns> 79 <algorithm>jump-consistent-hash</algorithm> 80 </rule> 81 </tableRule> 82 83 <function name="murmur" 84 class="io.mycat.route.function.PartitionByMurmurHash"> 85 <property name="seed">0</property><!-- 默认是0 --> 86 <property name="count">2</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 --> 87 <property name="virtualBucketTimes">160</property><!-- 一个实际的数据库节点被映射为这么多虚拟节点,默认是160倍,也就是虚拟节点数是物理节点数的160倍 --> 88 <!-- <property name="weightMapFile">weightMapFile</property> 节点的权重,没有指定权重的节点默认是1。以properties文件的格式填写,以从0开始到count-1的整数值也就是节点索引为key,以节点权重值为值。所有权重值必须是正整数,否则以1代替 --> 89 <!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 90 用于测试时观察各物理节点与虚拟节点的分布情况,如果指定了这个属性,会把虚拟节点的murmur hash值与物理节点的映射按行输出到这个文件,没有默认值,如果不指定,就不会输出任何东西 --> 91 </function> 92 93 <function name="crc32slot" 94 class="io.mycat.route.function.PartitionByCRC32PreSlot"> 95 <property name="count">4</property><!-- 要分片的数据库节点数量,必须指定,否则没法分片 --> 96 </function> 97 <function name="hash-int" 98 class="io.mycat.route.function.PartitionByFileMap"> 99 <property name="mapFile">partition-hash-int.txt</property> 100 </function> 101 <function name="rang-long" 102 class="io.mycat.route.function.AutoPartitionByLong"> 103 <property name="mapFile">autopartition-long.txt</property> 104 </function> 105 <function name="mod-long" class="io.mycat.route.function.PartitionByMod"> 106 <!-- how many data nodes --> 107 <property name="count">3</property> 108 </function> 109 110 <function name="func1" class="io.mycat.route.function.PartitionByLong"> 111 <property name="partitionCount">8</property> 112 <property name="partitionLength">128</property> 113 </function> 114 <function name="latestMonth" 115 class="io.mycat.route.function.LatestMonthPartion"> 116 <property name="splitOneDay">24</property> 117 </function> 118 <function name="partbymonth" 119 class="io.mycat.route.function.PartitionByMonth"> 120 <property name="dateFormat">yyyy-MM-dd</property> 121 <property name="sBeginDate">2015-01-01</property> 122 </function> 123 124 <function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod"> 125 <property name="mapFile">partition-range-mod.txt</property> 126 </function> 127 128 <function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash"> 129 <property name="totalBuckets">3</property> 130 </function> 131 </mycat:rule>
3.检查mycatconf uledatacrc32slot_USER.properties
#WARNING !!!Please do not modify or delete this file!!!
#Thu Sep 06 17:32:38 CST 2018
3=76800-102399
2=51200-76799
1=25600-51199
0=0-25599
如果对应的分出来了四个节点,直接启动,如果没有,删除这个文件,再启动。