echo編輯整理,歡迎轉載,轉載請聲明文章來源。歡迎添加echo微信(微信號:t2421499075)交流學習。 百戰不敗,依不自稱常勝,百敗不頹,依能奮力前行。——這纔是真正的堪稱強大!!!node
若是前面的準備工做都作好了,而且有配置Mycat實現讀寫分離,就能很快的上手Mycat實現數據庫分庫分表。Mycat實現讀寫分離請參考: https://blog.csdn.net/xlecho/...
因爲咱們的Mycat實現讀寫分離配置好了登陸用戶名和密碼,因此配置Mycat實現數據庫分庫分表的工做就不在須要去配置server.mxl了,可是咱們使用分表規則的時候,要涉及兩個新的配置文件mysql
實現數據庫分庫分表它和讀寫分離最大的不一樣就是dataHost該標籤的配置,讀寫分離,只須要一個dataHost便可。可是dataHost若是隻配置一個,就沒有辦法實現多庫讀寫。咱們要實現分表,固然要考慮每個庫對應的表都須要可以讀寫。因此咱們在配置table的時候,對應的每個庫,就須要對應到每個能夠寫的庫。同時和讀寫分離不一樣的是,咱們既然要分庫分表就須要分表的規則,這裏新增了分表規則auto-sharding-longgit
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="userDB" checkSQLschema="true" sqlMaxLimit="100"> <table name="user" dataNode="dn1,dn2,dn3" primaryKey="id" rule="auto-sharding-long"/> </schema> <dataNode name="dn1" dataHost="testNode1" database="test"/> <dataNode name="dn2" dataHost="testNode2" database="test"/> <dataNode name="dn3" dataHost="testNode3" database="test"/> <dataHost name="testNode1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM1" url="192.168.222.132:3306" user="root" password="123456" /> </dataHost> <dataHost name="testNode2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM2" url="192.168.222.133:3306" user="root" password="123456" /> </dataHost> <dataHost name="testNode3" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <writeHost host="hostM3" url="192.168.222.134:3306" user="root" password="123456" /> </dataHost> </mycat:schema>
咱們在schema中配置了auto-sharding-long的規則就須要在rule.xml中配置對應的規則。(Mycat原始的rule配置文件中就已經有了咱們須要配置的規則,因此咱們這裏不須要改動,可是auto-sharding-long對應的autopartition-long.txt文檔,因爲演示須要,咱們更改一下)github
<?xml version="1.0" encoding="UTF-8"?> <!-- - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. --> <!DOCTYPE mycat:rule SYSTEM "rule.dtd"> <mycat:rule xmlns:mycat="http://io.mycat/"> <tableRule name="rule1"> <rule> <columns>id</columns> <algorithm>func1</algorithm> </rule> </tableRule> <tableRule name="rule2"> <rule> <columns>user_id</columns> <algorithm>func1</algorithm> </rule> </tableRule> <tableRule name="sharding-by-intfile"> <rule> <columns>sharding_id</columns> <algorithm>hash-int</algorithm> </rule> </tableRule> <tableRule name="auto-sharding-long"> <rule> <columns>id</columns> <algorithm>rang-long</algorithm> </rule> </tableRule> <tableRule name="mod-long"> <rule> <columns>id</columns> <algorithm>mod-long</algorithm> </rule> </tableRule> <tableRule name="sharding-by-murmur"> <rule> <columns>id</columns> <algorithm>murmur</algorithm> </rule> </tableRule> <tableRule name="crc32slot"> <rule> <columns>id</columns> <algorithm>crc32slot</algorithm> </rule> </tableRule> <tableRule name="sharding-by-month"> <rule> <columns>create_time</columns> <algorithm>partbymonth</algorithm> </rule> </tableRule> <tableRule name="latest-month-calldate"> <rule> <columns>calldate</columns> <algorithm>latestMonth</algorithm> </rule> </tableRule> <tableRule name="auto-sharding-rang-mod"> <rule> <columns>id</columns> <algorithm>rang-mod</algorithm> </rule> </tableRule> <tableRule name="jch"> <rule> <columns>id</columns> <algorithm>jump-consistent-hash</algorithm> </rule> </tableRule> <function name="murmur" class="io.mycat.route.function.PartitionByMurmurHash"> <property name="seed">0</property><!-- 默認是0 --> <property name="count">2</property><!-- 要分片的數據庫節點數量,必須指定,不然無法分片 --> <property name="virtualBucketTimes">160</property><!-- 一個實際的數據庫節點被映射爲這麼多虛擬節點,默認是160倍,也就是虛擬節點數是物理節點數的160倍 --> <!-- <property name="weightMapFile">weightMapFile</property> 節點的權重,沒有指定權重的節點默認是1。以properties文件的格式填寫,以從0開始到count-1的整數值也就是節點索 引爲key,以節點權重值爲值。全部權重值必須是正整數,不然以1代替 --> <!-- <property name="bucketMapPath">/etc/mycat/bucketMapPath</property> 用於測試時觀察各物理節點與虛擬節點的分佈狀況,若是指定了這個屬性,會把虛擬節點的murmur hash值與物理節點的映射按行輸出到這個文件,沒有默認值,若是不指定,就不>會輸出任何東西 --> </function> <function name="crc32slot" class="io.mycat.route.function.PartitionByCRC32PreSlot"> <property name="count">2</property><!-- 要分片的數據庫節點數量,必須指定,不然無法分片 --> </function> <function name="hash-int" class="io.mycat.route.function.PartitionByFileMap"> <property name="mapFile">partition-hash-int.txt</property> </function> <function name="rang-long" class="io.mycat.route.function.AutoPartitionByLong"> <property name="mapFile">autopartition-long.txt</property> </function> <function name="mod-long" class="io.mycat.route.function.PartitionByMod"> <!-- how many data nodes --> <property name="count">3</property> </function> <function name="func1" class="io.mycat.route.function.PartitionByLong"> <property name="partitionCount">8</property> <property name="partitionLength">128</property> </function> <function name="latestMonth" class="io.mycat.route.function.LatestMonthPartion"> <property name="splitOneDay">24</property> </function> <function name="partbymonth" class="io.mycat.route.function.PartitionByMonth"> <property name="dateFormat">yyyy-MM-dd</property> <property name="sBeginDate">2015-01-01</property> </function> <function name="rang-mod" class="io.mycat.route.function.PartitionByRangeMod"> <property name="mapFile">partition-range-mod.txt</property> </function> <function name="jump-consistent-hash" class="io.mycat.route.function.PartitionByJumpConsistentHash"> <property name="totalBuckets">3</property> </function> </mycat:rule>
該配置文件的本來配置是M爲單位,這樣的數據太大,測試的時候,計算麻煩,咱們更改配置以下:sql
0-2000=0 # 表明id的大小在0-2000中間就是用dn1服務器 2000-4000=1 # 表明id的大小在2000-4000中間就是用dn2服務器 4000-8000=2 # 表明id的大小在4000-8000中間就是用dn3服務器
咱們有三個節點,至關於一個有一個服務器集合,每臺服務器都是根據下標來對應的,都是從0開始計數,0就表明咱們的dn1
插入三條數據,根據咱們配置的規則進行插入數據庫
INSERT INTO `user`(`id`, `user_name`, `pass_word`) VALUES (1000, 'a', '123456'); INSERT INTO `user`(`id`, `user_name`, `pass_word`) VALUES (3000, 'b', '123456'); INSERT INTO `user`(`id`, `user_name`, `pass_word`) VALUES (6000, 'c', '123456');
插入完成以後,咱們鏈接Mycat查看數據,以下圖:express
這裏咱們有3條1000的數據緣由是由於咱們id等於1000,因此插入到dn1服務裏面,可是dn1是咱們對應的MySQL主從複製的主服務器132,因此,插入該服務器以後,另外兩臺從服務器133/134都會直接將數據複製過去。
服務器dn1,應該id=1000的三臺服務器都有,id=3000在咱們的133從服務器上,id=6000在咱們134服務器上apache
作一個有底線的博客主segmentfault