您好,登錄后才能下訂單哦!
項目環境:
192.168.8.30 mycat
192.168.8.31 node1
192.168.8.32 node2
192.168.8.33 node3
三個節點MySQL均為單實例
一、創建測試數據
node1
create database testdb01; create database testdb02; create database testdb03; create database testdb04; create database testdb05; create database testdb06; create database testdb07; create database testdb08; create database testdb09; create database testdb10; create database testdb11; create database testdb12;
node2
create database testdb13; create database testdb14; create database testdb15; create database testdb16; create database testdb17; create database testdb18; create database testdb19; create database testdb20; create database testdb21; create database testdb22; create database testdb23; create database testdb24;
node3
create database testdb25; create database testdb26; create database testdb27; create database testdb28; create database testdb29; create database testdb30; create database testdb31; create database testdb32; create database testdb33; create database testdb34; create database testdb35;
二、配置schema.xml
<?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="mycatdb" checkSQLschema="false" sqlMaxLimit="100"> <table name="user01" dataNode="dn$1-35" rule="sharding-by-intfile-mycatdb-kk_user"></table> </schema> <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743" /> --> <dataNode name="dn1" dataHost="node1" database="testdb01" /> <dataNode name="dn2" dataHost="node1" database="testdb02" /> <dataNode name="dn3" dataHost="node1" database="testdb03" /> <dataNode name="dn4" dataHost="node1" database="testdb04" /> <dataNode name="dn5" dataHost="node1" database="testdb05" /> <dataNode name="dn6" dataHost="node1" database="testdb06" /> <dataNode name="dn7" dataHost="node1" database="testdb07" /> <dataNode name="dn8" dataHost="node1" database="testdb08" /> <dataNode name="dn9" dataHost="node1" database="testdb09" /> <dataNode name="dn10" dataHost="node1" database="testdb10" /> <dataNode name="dn11" dataHost="node1" database="testdb11" /> <dataNode name="dn12" dataHost="node1" database="testdb12" /> <dataNode name="dn13" dataHost="node2" database="testdb13" /> <dataNode name="dn14" dataHost="node2" database="testdb14" /> <dataNode name="dn15" dataHost="node2" database="testdb15" /> <dataNode name="dn16" dataHost="node2" database="testdb16" /> <dataNode name="dn17" dataHost="node2" database="testdb17" /> <dataNode name="dn18" dataHost="node2" database="testdb18" /> <dataNode name="dn19" dataHost="node2" database="testdb19" /> <dataNode name="dn20" dataHost="node2" database="testdb20" /> <dataNode name="dn21" dataHost="node2" database="testdb21" /> <dataNode name="dn22" dataHost="node2" database="testdb22" /> <dataNode name="dn23" dataHost="node2" database="testdb23" /> <dataNode name="dn24" dataHost="node2" database="testdb24" /> <dataNode name="dn25" dataHost="node3" database="testdb25" /> <dataNode name="dn26" dataHost="node3" database="testdb26" /> <dataNode name="dn27" dataHost="node3" database="testdb27" /> <dataNode name="dn28" dataHost="node3" database="testdb28" /> <dataNode name="dn29" dataHost="node3" database="testdb29" /> <dataNode name="dn30" dataHost="node3" database="testdb30" /> <dataNode name="dn31" dataHost="node3" database="testdb31" /> <dataNode name="dn32" dataHost="node3" database="testdb32" /> <dataNode name="dn33" dataHost="node3" database="testdb33" /> <dataNode name="dn34" dataHost="node3" database="testdb34" /> <dataNode name="dn35" dataHost="node3" database="testdb35" /> <!--<dataNode name="dn4" dataHost="sequoiadb1" database="SAMPLE" /> <dataNode name="jdbc_dn1" dataHost="jdbchost" database="db1" /> <dataNode name="jdbc_dn2" dataHost="jdbchost" database="db2" /> <dataNode name="jdbc_dn3" dataHost="jdbchost" database="db3" /> --> <dataHost name="node1" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="192.168.8.31" url="192.168.8.31:3306" user="root" password="mysql"></writeHost> </dataHost> <dataHost name="node2" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="192.168.8.32" url="192.168.8.32:3306" user="root" password="mysql"></writeHost> </dataHost> <dataHost name="node3" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="192.168.8.33" url="192.168.8.33:3306" user="root" password="mysql"></writeHost> </dataHost> </mycat:schema>
三、配置rule.xml
<mycat:rule xmlns:mycat="http://io.mycat/"> <tableRule name="sharding-by-intfile-mycatdb-kk_user"> <rule> <columns>province</columns> <algorithm>hash-int</algorithm> </rule> </tableRule> <function name="hash-int" class="io.mycat.route.function.PartitionByFileMap"> <property name="mapFile">partition-hash-int-mycatdb-kk_user.txt</property> <property name="type">1</property> <property name="defaultNode">0</property> </function> </mycat:rule>
四、配置partition-hash-int-mycatdb-kk_user.txt
北京市=0 上海市=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 DEFAULT_NODE=34
五、配置server.xml
<user name="root" defaultAccount="true"> <property name="password">mysql</property> <property name="schemas">mycatdb</property> </user>
六、啟動mycat
/usr/local/mycat/bin/mycat start
查看mycat日志
STATUS | wrapper | 2018/11/22 10:07:48 | --> Wrapper Started as Daemon STATUS | wrapper | 2018/11/22 10:07:48 | Launching a JVM... INFO | jvm 1 | 2018/11/22 10:07:48 | OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0 INFO | jvm 1 | 2018/11/22 10:07:50 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org INFO | jvm 1 | 2018/11/22 10:07:50 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved. INFO | jvm 1 | 2018/11/22 10:07:50 | INFO | jvm 1 | 2018/11/22 10:07:57 | MyCAT Server startup successfully. see logs in logs/mycat.log
七、登錄mysql進行數據驗證
mysql -uroot -pmysql -P8066 -h292.168.8.30
mysql> show databases; +----------+ | DATABASE | +----------+ | mycatdb | +----------+ 1 row in set (0.01 sec) mysql> use mycatdb Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-------------------+ | Tables in mycatdb | +-------------------+ | user01 | +-------------------+ 1 row in set (0.01 sec)
因為schema.xml里邊配置了table的屬性,所以登錄MySQL會看到這張表,但是查看不到數據,也無法用drop table直接刪除。
第一次刪除此表需要用命令DROP TABLE IF EXISTS `user01`; 否則會提示找不到此表。
創建表,需要含province字段
mysql> DROP TABLE IF EXISTS `user01`; Query OK, 0 rows affected (0.84 sec) mysql> create table user01(province varchar(40)); Query OK, 0 rows affected (1.74 sec)
向user01表插入測試數據,每個省份插入20條記錄
在node1查看部分數據
mysql> select count(*) ,province from testdb01.user01 group by province; +----------+-----------+ | count(*) | province | +----------+-----------+ | 20 | 北京市 | +----------+-----------+ 1 row in set (0.01 sec) mysql> select count(*) ,province from testdb05.user01 group by province; +----------+-----------+ | count(*) | province | +----------+-----------+ | 20 | 貴州省 | +----------+-----------+ 1 row in set (0.00 sec) mysql> select count(*) ,province from testdb12.user01 group by province; +----------+-----------+ | count(*) | province | +----------+-----------+ | 20 | 安徽省 | +----------+-----------+ 1 row in set (0.00 sec)
在node2查看部分數據
mysql> select count(*) ,province from testdb16.user01 group by province; +----------+-----------+ | count(*) | province | +----------+-----------+ | 20 | 廣西省 | +----------+-----------+ 1 row in set (0.00 sec) mysql> select count(*) ,province from testdb19.user01 group by province; +----------+-----------+ | count(*) | province | +----------+-----------+ | 20 | 江西省 | +----------+-----------+ 1 row in set (0.00 sec) mysql> select count(*) ,province from testdb22.user01 group by province; +----------+-----------+ | count(*) | province | +----------+-----------+ | 20 | 浙江省 | +----------+-----------+ 1 row in set (0.00 sec)
在node3查看部分數據
mysql> select count(*) ,province from testdb25.user01 group by province; +----------+-----------+ | count(*) | province | +----------+-----------+ | 20 | 湖南省 | +----------+-----------+ 1 row in set (0.00 sec) mysql> select count(*) ,province from testdb30.user01 group by province; +----------+-----------+ | count(*) | province | +----------+-----------+ | 20 | 遼寧省 | +----------+-----------+ 1 row in set (0.01 sec) mysql> select count(*) ,province from testdb33.user01 group by province; +----------+----------+ | count(*) | province | +----------+----------+ | 20 | 香港 | +----------+----------+ 1 row in set (0.00 sec)
八、查看各個分片的大小
node1
mysql> select table_schema,table_name as "Tables",ROUND(((data_length + -> index_length) / 1024 ), 2) "Size in KB" -> from information_schema.TABLES -> where TABLE_NAME = "user01" -> order by (data_length + index_length) desc; +--------------+--------+------------+ | table_schema | Tables | Size in KB | +--------------+--------+------------+ | testdb09 | user01 | 16.00 | | testdb08 | user01 | 16.00 | | testdb07 | user01 | 16.00 | | testdb06 | user01 | 16.00 | | testdb05 | user01 | 16.00 | | testdb04 | user01 | 16.00 | | testdb03 | user01 | 16.00 | | testdb02 | user01 | 16.00 | | testdb12 | user01 | 16.00 | | testdb01 | user01 | 16.00 | | testdb11 | user01 | 16.00 | | testdb10 | user01 | 16.00 | +--------------+--------+------------+ 12 rows in set (0.00 sec)
node2
mysql> select table_schema,table_name as "Tables",ROUND(((data_length + -> index_length) / 1024 ), 2) "Size in KB" -> from information_schema.TABLES -> where TABLE_NAME = "user01" -> order by (data_length + index_length) desc; +--------------+--------+------------+ | table_schema | Tables | Size in KB | +--------------+--------+------------+ | testdb18 | user01 | 16.00 | | testdb17 | user01 | 16.00 | | testdb16 | user01 | 16.00 | | testdb15 | user01 | 16.00 | | testdb14 | user01 | 16.00 | | testdb24 | user01 | 16.00 | | testdb13 | user01 | 16.00 | | testdb23 | user01 | 16.00 | | testdb22 | user01 | 16.00 | | testdb21 | user01 | 16.00 | | testdb20 | user01 | 16.00 | | testdb19 | user01 | 16.00 | +--------------+--------+------------+ 12 rows in set (0.00 sec)
node3
mysql> select table_schema,table_name as "Tables",ROUND(((data_length + -> index_length) / 1024 ), 2) "Size in KB" -> from information_schema.TABLES -> where TABLE_NAME = "user01" -> order by (data_length + index_length) desc; +--------------+--------+------------+ | table_schema | Tables | Size in KB | +--------------+--------+------------+ | testdb26 | user01 | 16.00 | | testdb25 | user01 | 16.00 | | testdb35 | user01 | 16.00 | | testdb34 | user01 | 16.00 | | testdb33 | user01 | 16.00 | | testdb32 | user01 | 16.00 | | testdb31 | user01 | 16.00 | | testdb30 | user01 | 16.00 | | testdb29 | user01 | 16.00 | | testdb28 | user01 | 16.00 | | testdb27 | user01 | 16.00 | +--------------+--------+------------+ 11 rows in set (0.00 sec)
到此,分片枚舉結束。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。