1 应用场景
Mycat 其实自带了2个数据范围分片的方案,一个是纯数据范围的分片,比如 1至 10000 号的数据放到分片1 ,10001 至 20000号数据放到分片2里。
另一个是数据常量形式的分片,即 10000号进入分片1 ,20000号进入片分2 。
2 环境说明
参考前一篇文章 《MyCat 学习笔记》第六篇.数据分片 之 按月数据分片 http://www.cnblogs.com/kaye0110/p/5160826.html
3 参数配置
3.1 server.xml 配置
参考前一篇
3.2 schema.xml 配置
<!-- t_range_long 表是根据数据范围进行分片,分片组为 dn4dn5dn6 ,分布规则为 sharding-long -->
<schema name="RANGEDB" checkSQLschema="false" sqlMaxLimit="100">
<table name="t_range_date" dataNode="dn4,dn5,dn6,dn7,dn8,dn9,dn10,dn11" rule="sharding-by-date" />
<table name="t_range_long" dataNode="dn4,dn5,dn6" rule="sharding-long" />
</schema>
3.3 rule.xml 配置
<!-- sharding-long 规则使用rang-long方案,在sharding_id上做分片 -->
<tableRule name="sharding-long">
<rule>
<columns>sharding_id</columns>
<algorithm>rang-long</algorithm>
</rule>
</tableRule>
<!-- 分片方案实现的类 以及默认数据集结点 -->
<function name="rang-long" class="org.opencloudb.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
<property name="defaultNode">0</property>
</function>
#autopartition-long.txt
#第0至500,0000条记录进入dn1,第500,0001至1000,0000记录进入dn2
# range start-end ,data node index
# K=1000,M=10000.
0-500M=0
500M-1000M=1
1000M-1500M=2
4 数据验证
4.1 Mycat 建表
mysql> CREATE TABLE `t_range_long` (
-> `id` INT NOT NULL,
-> `context` VARCHAR(45) NULL,
-> `sharding_id` VARCHAR(40) NULL,
-> PRIMARY KEY (`id`));
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 4
Current database: RANGEDB
Query OK, 0 rows affected (0.03 sec)
4.2 数据插入与查询
mysql> truncate table t_range_long;
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t_range_long (id,context,sharding_id) values (1,'rec 5000000',5000000); --> dn4
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_range_long (id,context,sharding_id) values (2,'rec 5000001',5000001); --> dn5
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_range_long (id,context,sharding_id) values (3,'rec 10000000',10000000); --> dn5
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_range_long (id,context,sharding_id) values (4,'rec 10000001',10000001); --> dn6
Query OK, 1 row affected (0.01 sec)
mysql> insert into t_range_long (id,context,sharding_id) values (5,'rec 15000000',15000000); --> dn6
Query OK, 1 row affected (0.00 sec)
mysql> insert into t_range_long (id,context,sharding_id) values (6,'rec 15000001',15000001); --> dn4
Query OK, 1 row affected (0.00 sec)
mysql> select * from t_range_long;
+----+--------------+-------------+
| id | context | sharding_id |
+----+--------------+-------------+
| 2 | rec 5000001 | 5000001 |
| 3 | rec 10000000 | 10000000 |
| 1 | rec 5000000 | 5000000 |
| 6 | rec 15000001 | 15000001 |
| 4 | rec 10000001 | 10000001 |
| 5 | rec 15000000 | 15000000 |
+----+--------------+-------------+
6 rows in set (0.00 sec)
4.3 物理库查询
分别对应用dn4dn5dn6
mysql> select * from range_db_4.t_range_long;
+----+--------------+-------------+
| id | context | sharding_id |
+----+--------------+-------------+
| 1 | rec 5000000 | 5000000 |
| 6 | rec 15000001 | 15000001 |
+----+--------------+-------------+
2 rows in set (0.00 sec)
mysql> select * from range_db_5.t_range_long;
+----+--------------+-------------+
| id | context | sharding_id |
+----+--------------+-------------+
| 2 | rec 5000001 | 5000001 |
| 3 | rec 10000000 | 10000000 |
+----+--------------+-------------+
2 rows in set (0.00 sec)
mysql> select * from range_db_6.t_range_long;
+----+--------------+-------------+
| id | context | sharding_id |
+----+--------------+-------------+
| 4 | rec 10000001 | 10000001 |
| 5 | rec 15000000 | 15000000 |
+----+--------------+-------------+
2 rows in set (0.00 sec)
5 优缺点分析
用起来还算方便,以后要做数据迁移也相对简单一些,适合做B2B2C系统商户专有数据或是应用租户模式(微商平台)之类。