项目环境:
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="
<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="
<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)
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 -h192.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)
到此,分片枚举结束。