项目环境:
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;
create database testdb36;
二、配置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="user07" dataNode="dn$1-36" rule="sharding-by-hour-adddate"></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="dn36" dataHost="node3" database="testdb36" />
<!--<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
<tableRule name="sharding-by-hour-adddate">
<rule>
<columns>adddate</columns>
<algorithm>sharding-by-hour</algorithm>
</rule>
</tableRule>
<function name="sharding-by-hour" class="io.mycat.route.function.LatestMonthPartion">
<property name="splitOneDay">24</property>
</function>
<property name="splitOneDay">24</property>每天24份
四、启动mycat
/usr/local/mycat/bin/mycat start
查看mycat日志
STATUS | wrapper | 2018/11/23 12:54:18 | --> Wrapper Started as Daemon
STATUS | wrapper | 2018/11/23 12:54:19 | Launching a JVM...
INFO | jvm 1 | 2018/11/23 12:54:19 | OpenJDK 64-Bit Server VM warning: ignoring option MaxPermSize=64M; support was removed in 8.0
INFO | jvm 1 | 2018/11/23 12:54:23 | Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
INFO | jvm 1 | 2018/11/23 12:54:23 | Copyright 1999-2006 Tanuki Software, Inc. All Rights Reserved.
INFO | jvm 1 | 2018/11/23 12:54:23 |
INFO | jvm 1 | 2018/11/23 12:54:28 | 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.02 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 |
+-------------------+
| user04 |
| user05 |
| user06 |
| user07 |
+-------------------+
4 rows in set (0.01 sec)
mysql> drop table if exists user07;
Query OK, 0 rows affected, 1 warning (0.38 sec)
mysql> create table user07(
-> id int not null,
-> name varchar(64),
-> adddate varchar(10)
-> );
六、插入测试数据
insert into user07(id,name,adddate) values(1,'steven','2018110100');
insert into user07(id,name,adddate) values(1,'steven','2018110101');
insert into user07(id,name,adddate) values(1,'steven','2018110102');
insert into user07(id,name,adddate) values(1,'steven','2018110103');
insert into user07(id,name,adddate) values(1,'steven','2018110104');
insert into user07(id,name,adddate) values(1,'steven','2018110105');
insert into user07(id,name,adddate) values(1,'steven','2018110106');
insert into user07(id,name,adddate) values(1,'steven','2018110107');
insert into user07(id,name,adddate) values(1,'steven','2018110108');
insert into user07(id,name,adddate) values(1,'steven','2018110109');
insert into user07(id,name,adddate) values(1,'steven','2018110110');
insert into user07(id,name,adddate) values(1,'steven','2018110111');
insert into user07(id,name,adddate) values(1,'steven','2018110112');
insert into user07(id,name,adddate) values(1,'steven','2018110113');
insert into user07(id,name,adddate) values(1,'steven','2018110114');
insert into user07(id,name,adddate) values(1,'steven','2018110115');
insert into user07(id,name,adddate) values(1,'steven','2018110116');
insert into user07(id,name,adddate) values(1,'steven','2018110117');
insert into user07(id,name,adddate) values(1,'steven','2018110118');
insert into user07(id,name,adddate) values(1,'steven','2018110119');
insert into user07(id,name,adddate) values(1,'steven','2018110120');
insert into user07(id,name,adddate) values(1,'steven','2018110121');
insert into user07(id,name,adddate) values(1,'steven','2018110122');
insert into user07(id,name,adddate) values(1,'steven','2018110123');
insert into user07(id,name,adddate) values(1,'steven','2018110200');
insert into user07(id,name,adddate) values(1,'steven','2018110201');
insert into user07(id,name,adddate) values(1,'steven','2018110202');
insert into user07(id,name,adddate) values(1,'steven','2018110203');
insert into user07(id,name,adddate) values(1,'steven','2018110204');
insert into user07(id,name,adddate) values(1,'steven','2018110205');
insert into user07(id,name,adddate) values(1,'steven','2018110206');
insert into user07(id,name,adddate) values(1,'steven','2018110207');
insert into user07(id,name,adddate) values(1,'steven','2018110208');
insert into user07(id,name,adddate) values(1,'steven','2018110209');
insert into user07(id,name,adddate) values(1,'steven','2018110210');
insert into user07(id,name,adddate) values(1,'steven','2018110211');
执行两次,每条数据插入两份
七、验证数据
node1
mysql> select * from testdb01.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110100 |
| 1 | steven | 2018110100 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb02.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110101 |
| 1 | steven | 2018110101 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb03.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110102 |
| 1 | steven | 2018110102 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb04.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110103 |
| 1 | steven | 2018110103 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb05.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110104 |
| 1 | steven | 2018110104 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb06.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110105 |
| 1 | steven | 2018110105 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb07.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110106 |
| 1 | steven | 2018110106 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb08.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110107 |
| 1 | steven | 2018110107 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb09.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110108 |
| 1 | steven | 2018110108 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb10.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110109 |
| 1 | steven | 2018110109 |
+----+--------+------------+
2 rows in set (0.01 sec)
mysql> select * from testdb11.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110110 |
| 1 | steven | 2018110110 |
+----+--------+------------+
2 rows in set (0.02 sec)
mysql> select * from testdb12.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110111 |
| 1 | steven | 2018110111 |
+----+--------+------------+
2 rows in set (0.01 sec)
node2
mysql> select * from testdb13.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110112 |
| 1 | steven | 2018110112 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb14.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110113 |
| 1 | steven | 2018110113 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb15.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110114 |
| 1 | steven | 2018110114 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb16.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110115 |
| 1 | steven | 2018110115 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb17.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110116 |
| 1 | steven | 2018110116 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb18.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110117 |
| 1 | steven | 2018110117 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb19.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110118 |
| 1 | steven | 2018110118 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb20.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110119 |
| 1 | steven | 2018110119 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb21.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110120 |
| 1 | steven | 2018110120 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb22.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110121 |
| 1 | steven | 2018110121 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb23.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110122 |
| 1 | steven | 2018110122 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb24.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110123 |
| 1 | steven | 2018110123 |
+----+--------+------------+
2 rows in set (0.00 sec)
node3
mysql> select * from testdb25.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110200 |
| 1 | steven | 2018110200 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb26.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110201 |
| 1 | steven | 2018110201 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb27.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110202 |
| 1 | steven | 2018110202 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb28.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110203 |
| 1 | steven | 2018110203 |
+----+--------+------------+
2 rows in set (0.02 sec)
mysql> select * from testdb29.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110204 |
| 1 | steven | 2018110204 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb30.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110205 |
| 1 | steven | 2018110205 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb31.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110206 |
| 1 | steven | 2018110206 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb32.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110207 |
| 1 | steven | 2018110207 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb33.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110208 |
| 1 | steven | 2018110208 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb34.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110209 |
| 1 | steven | 2018110209 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb35.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110210 |
| 1 | steven | 2018110210 |
+----+--------+------------+
2 rows in set (0.00 sec)
mysql> select * from testdb36.user07;
+----+--------+------------+
| id | name | adddate |
+----+--------+------------+
| 1 | steven | 2018110211 |
| 1 | steven | 2018110211 |
+----+--------+------------+
2 rows in set (0.00 sec)
72条记录,按照时间先后顺序分布在36个分片中,验证完毕。
注意:rule.xml中配置的一天分成24份,一份是1小时,我们共36个分片,所以插入的数据只能限制在每月月初的36小时之内,如2018100100-2018100211