echo編輯整理,歡迎轉載,轉載請聲明文章來源。歡迎添加echo微信(微信號:t2421499075)交流學習。 百戰不敗,依不自稱常勝,百敗不頹,依能奮力前行。——這才是真正的堪稱強大!!!
准備工作:
- 請先在服務器上面安裝Mycat,安裝教程:https://blog.csdn.net/xlecho/article/details/102755484
- 了解一下他的核心配置文件:Server.xml詳解、schema.xml詳解、rule.xml詳解
- 准備三台服務器,都需要安裝mysql
- 搭建一主兩從的MySQL讀寫分離環境:Mycat分布式數據庫架構解決方案--搭建MySQL讀寫分離環境--一主多從
- 下載一份Mycat的原碼(在服務器上面編寫不方便,本地更改好,替換服務器配置文件更方便)Mycat下載
如果前面的准備工作都做好了,並且有配置Mycat實現讀寫分離,就能很快的上手Mycat實現數據庫分庫分表。Mycat實現讀寫分離請參考:https://blog.csdn.net/xlecho/article/details/102897050
由於我們的Mycat實現讀寫分離配置好了登錄用戶名和密碼,所以配置Mycat實現數據庫分庫分表的工作就不在需要去配置server.mxl了,但是我們使用分表規則的時候,要涉及兩個新的配置文件
- rule.xml
- autopartition-long.txt
配置schema
實現數據庫分庫分表它和讀寫分離最大的不同就是dataHost該標簽的配置,讀寫分離,只需要一個dataHost即可。但是dataHost如果只配置一個,就沒有辦法實現多庫讀寫。我們要實現分表,當然要考慮每一個庫對應的表都需要能夠讀寫。所以我們在配置table的時候,對應的每一個庫,就需要對應到每一個可以寫的庫。同時和讀寫分離不同的是,我們既然要分庫分表就需要分表的規則,這里新增了分表規則auto-sharding-long
<?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>
配置rule.xml
我們在schema中配置了auto-sharding-long的規則就需要在rule.xml中配置對應的規則。(Mycat原始的rule配置文件中就已經有了我們需要配置的規則,所以我們這里不需要改動,但是auto-sharding-long對應的autopartition-long.txt文檔,由於演示需要,我們更改一下)
<?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>
配置autopartition-long.txt
該配置文件的原本配置是M為單位,這樣的數據太大,測試的時候,計算麻煩,我們更改配置如下:
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查看數據,如下圖:
這里我們有3條1000的數據原因是因為我們id等於1000,所以插入到dn1服務里面,但是dn1是我們對應的MySQL主從復制的主服務器132,所以,插入該服務器之后,另外兩台從服務器133/134都會直接將數據復制過去。
驗證
服務器dn1,應該id=1000的三台服務器都有,id=3000在我們的133從服務器上,id=6000在我們134服務器上
- 主服務器132
- 從服務器133
- 從服務器134
做一個有底線的博客主