概述
单库多表的分片策略已经完成,那么怎么才能把多库单表和单库多表结合起来呢?
项目环境
192.168.201.34 mycat
192.168.201.33 node1
192.168.201.35 node2
一、创建测试库
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;
二、配置schema.xml
<schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="test" dataNode="dn$1-24" rule="sharding-by-month-adddate"></table>
</schema>
<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" />
<!-- 物理机的 url -->
<dataHost name="host1" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="server1" url="192.168.201.33:3306" user="root" password="123456"/>
</dataHost>
<dataHost name="host2" maxCon="1000" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
<heartbeat>select user()</heartbeat>
<writeHost host="server2" url="192.168.201.35:3306" user="root" password="123456"/>
</dataHost>
</mycat:schema>
三、配置rule.xml
<tableRule name="sharding-by-month-adddate">
<rule>
<columns>adddate</columns>
<algorithm>partbymonth</algorithm>
</rule>
</tableRule>
<function name="partbymonth" class="io.mycat.route.function.PartitionByMonth">
<property name="dateFormat">yyyy-MM-dd</property> <!-- 日期格式-->
<property name="sBeginDate">2019-01-01</property> <!-- 开始日期-->
</function>
四、启动mycat
/usr/local/mycat/bin/mycat start
五、登录MySQL查看逻辑表
mysql -uroot -pmysql -P8066 -h192.168.8.30
mysql> create table test(
-> id int not null auto_increment,
-> name varchar(64),
-> adddate datetime,
-> primary key(id)
-> );
六、插入测试数据
insert into test(id,name,adddate) values(1,'steven','2019-05-09 12:32:12');
insert into test(id,name,adddate) values(2,'steven','2019-09-09 12:32:12');
insert into test(id,name,adddate) values(3,'steven','2019-12-09 12:32:12');
insert into test(id,name,adddate) values(4,'steven','2020-5-09 12:32:12');
insert into test(id,name,adddate) values(5,'steven','2020-06-09 12:32:12');
insert into test(id,name,adddate) values(6,'steven','2020-07-09 12:32:12');
七、验证数据
1.node1
mysql> select * from testdb01.test;
Empty set (0.00 sec)
mysql> select * from testdb02.test;
Empty set (0.00 sec)
mysql> select * from testdb03.test;
Empty set (0.00 sec)
mysql> select * from testdb04.test;
Empty set (0.01 sec)
mysql> select * from testdb05.test;
+----+--------+---------------------+
| id | name | adddate |
+----+--------+---------------------+
| 1 | steven | 2019-05-09 12:32:12 |
+----+--------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb06.test;
Empty set (0.01 sec)
mysql> select * from testdb07.test;
Empty set (0.01 sec)
mysql> select * from testdb08.test;
Empty set (0.01 sec)
mysql> select * from testdb09.test;
+----+--------+---------------------+
| id | name | adddate |
+----+--------+---------------------+
| 2 | steven | 2016-09-09 12:32:12 |
+----+--------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb10.test;
Empty set (0.00 sec)
mysql> select * from testdb11.test;
Empty set (0.00 sec)
mysql> select * from testdb12.test;
+----+--------+---------------------+
| id | name | adddate |
+----+--------+---------------------+
| 3 | steven | 2019-12-09 12:32:12 |
+----+--------+---------------------+
1 row in set (0.00 sec)
- node2
mysql> select * from testdb13.test;
Empty set (0.00 sec)
mysql> select * from testdb14.test;
Empty set (0.00 sec)
mysql> select * from testdb15.test;
Empty set (0.00 sec)
mysql> select * from testdb16.test;
Empty set (0.00 sec)
mysql> select * from testdb17.test;
+----+--------+---------------------+
| id | name | adddate |
+----+--------+---------------------+
| 4 | steven | 2020-05-09 12:32:12 |
+----+--------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb18.test;
+----+--------+---------------------+
| id | name | adddate |
+----+--------+---------------------+
| 5 | steven | 2020-06-09 12:32:12 |
+----+--------+---------------------+
1 row in set (0.01 sec)
mysql> select * from testdb19.test;
+----+--------+---------------------+
| id | name | adddate |
+----+--------+---------------------+
| 6 | steven | 2020-07-09 12:32:12 |
+----+--------+---------------------+
1 row in set (0.00 sec)
mysql> select * from testdb20.test;
Empty set (0.01 sec)
mysql> select * from testdb21.test;
Empty set (0.00 sec)
mysql> select * from testdb22.test;
Empty set (0.00 sec)
mysql> select * from testdb23.test;
Empty set (0.00 sec)
mysql> select * from testdb24.test;
Empty set (0.01 sec)
网友评论