1. 程式人生 > >3.Mysql叢集------Mycat分庫分表

3.Mysql叢集------Mycat分庫分表

前言:

分庫分表,在本節裡是水平切分,就是多個數據庫裡包含的表是一模一樣的。

只是把欄位雜湊的分到不同的庫中。

實踐:

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.檢查mycat\conf\ruledata\crc32slot_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

如果對應的分出來了四個節點,直接啟動,如果沒有,刪除這個檔案,再啟動。