1 應用場景
Mycat 有很多數據分庫規則,接下來幾篇就相關覺得常用的規則進行試用與總結。
一般來說,按自然月份來進行數據分片的規則比較適用於商城訂單查詢,類似最近1周、2周、3個月內的數據。或是報表類應用。
這樣的數據放在一個片區內省去了數據合並的時間。
當然按月數據量不要過大就OK。
2 環境說明
Windows 7
本機多數據庫 Mysql 5.5.2
3306 端口下掛有4個庫 : range_db_4、range_db_5、range_db_6、range_db_7
3310 端口下掛有4個庫 : range_db_8、range_db_9、range_db_10、range_db_11
3 參數配置
3.1 數據庫配置
mysql 客戶端分別進入 3306 和 3310 服務,開始建立物理的schema。
CREATE SCHEMA `range_db_4` DEFAULT CHARACTER SET utf8 ;
CREATE SCHEMA `range_db_5` DEFAULT CHARACTER SET utf8 ;
CREATE SCHEMA `range_db_6` DEFAULT CHARACTER SET utf8 ;
CREATE SCHEMA `range_db_7` DEFAULT CHARACTER SET utf8 ;
...
3306 上
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mycat_sync_test |
| mysql |
| performance_schema |
| range_db_4 |
| range_db_5 |
| range_db_6 |
| range_db_7 |
+--------------------+
8 rows in set (0.00 sec)
3310上
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mycat_sync_test |
| mysql |
| performance_schema |
| range_db_10 |
| range_db_11 |
| range_db_8 |
| range_db_9 |
| traveldata_db_1 |
| traveldata_db_2 |
+--------------------+
10 rows in set (0.00 sec)
3.2 server.xml 配置
<!-- 開通test用戶訪問RANGEDB訪問權限 RANGEDB是虛擬schema -->
<user name="test">
<property name="password">test</property>
<property name="schemas">TRDB,RANGEDB</property>
</user>
3.3 schema.xml 配置
<!-- 設定虛擬 schema RANGEDB 信息 -->
<schema name="RANGEDB" checkSQLschema="false" sqlMaxLimit="100">
<!-- 設定虛擬表 t_range_date 對應至數據結點 dn4:dn11 一共8個數據分片,使用 sharding-by-date 分片規則 -->
<table name="t_range_date" dataNode="dn4,dn5,dn6,dn7,dn8,dn9,dn10,dn11" rule="sharding-by-date" />
</schema>
<!-- 設定數據結點dn4:dn7 對應的host為 3306服務 以及對應的物理schema -->
<dataNode name="dn4" dataHost="localhost3306" database="range_db_4" />
<dataNode name="dn5" dataHost="localhost3306" database="range_db_5" />
<dataNode name="dn6" dataHost="localhost3306" database="range_db_6" />
<dataNode name="dn7" dataHost="localhost3306" database="range_db_7" />
<!-- 設定數據結點dn8:dn11 對應的host為 3310 服務 以及對應的物理schema -->
<dataNode name="dn8" dataHost="localhost3310" database="range_db_8" />
<dataNode name="dn9" dataHost="localhost3310" database="range_db_9" />
<dataNode name="dn10" dataHost="localhost3310" database="range_db_10" />
<dataNode name="dn11" dataHost="localhost3310" database="range_db_11" />
<!-- 設定datahost 3306 目前只配了一台物理機,若要做讀寫分離可以參考開場第1、2篇內容進行調整 -->
<dataHost name="localhost3306" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM3306" url="localhost:3306" user="root" password="root123"></writeHost>
</dataHost>
<!-- 設定datahost 3306 -->
<dataHost name="localhost3310" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="2" slaveThreshold="100">
<heartbeat>select user()</heartbeat>
<writeHost host="hostM3310" url="localhost:3310" user="root" password="root123"></writeHost>
</dataHost>
3.4 rule.xml 配置
<!-- 分片字段對應到date_str 分片規則為partbymonth -->
<tableRule name="sharding-by-date">
<rule>
<columns>date_str</columns>
<algorithm>partbymonth</algorithm>
</rule>
</tableRule>
<!-- 分片規則 partbymonth 的配置 從 2015 -01 -01 開始分片 -->
<function name="partbymonth" class="org.opencloudb.route.function.PartitionByMonth">
<property name="dateFormat">yyyy-MM-dd</property>
<property name="sBeginDate">2015-01-01</property>
</function>
3.5 mycat 重新加載配置信息
訪問Mycat 9066 管理口,並重新加載所有參數配置。
D:\bin\mysql\MySQL_3307\bin>mysql -utest -ptest -P 9066
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.8-mycat-1.5-beta-20160111170158 MyCat Server (monitor)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> reload @@config_all;
Query OK, 1 row affected (0.36 sec)
Reload config success
4 數據驗證
4.1 Mycat 建表
進入 Mycat 8066 服務口,選用 RANGEDB 庫,同步create table。
D:\bin\mysql\MySQL_3310\bin>mysql -utest -ptest -P 8066
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.8-mycat-1.5-beta-20160111170158 MyCat Server (OpenCloundDB)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use RANGEDB;
Database changed
mysql> CREATE TABLE `t_range_date` ( `id` INT NOT NULL, `date` DATE NULL, `date_str` VARCHAR(45) NULL, `context` VARCHAR(45) NULL, PRIMARY KEY (`id`));
Query OK, 0 rows affected (0.09 sec)
4.2 數據插入與查詢
由於只建立了8個分片,超出部分就直接拋數組越界異常了。
mysql> insert into t_range_date (id,date_str,context) values(1,'2015-01-01','month-1-str');
insert into t_range_date (id,date_str,context) values(2,'2015-02-01','month-2-str');
insert into t_range_date (id,date_str,context) values(3,'2015-03-01','month-3-str');
insert into t_range_date (id,date_str,context) values(4,'2015-04-01','month-4-str');
insert into t_range_date (id,date_str,context) values(5,'2015-05-01','month-5-str');
insert into t_range_date (id,date_str,context) values(6,'2015-06-01','month-6-str');
insert into t_range_date (id,date_str,context) values(7,'2015-07-01','month-7-str');
insert into t_range_date (id,date_str,context) values(8,'2015-08-01','month-8-str');
insert into t_range_date (id,date_str,context) values(9,'2015-09-01','month-9-str');
insert into t_range_date (id,date_str,context) values(10,'2015-10-01','month-10-str');
insert into t_range_date (id,date_str,context) values(11,'2015-11-01','month-11-str');
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.00 sec)
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.00 sec)
ERROR 1064 (HY000): Index: 8, Size: 8
ERROR 1064 (HY000): Index: 9, Size: 8
ERROR 1064 (HY000): Index: 10, Size: 8
mysql> select * from t_range_date;
+----+------+------------+-------------+
| id | date | date_str | context |
+----+------+------------+-------------+
| 2 | NULL | 2015-02-01 | month-2-str |
| 4 | NULL | 2015-04-01 | month-4-str |
| 5 | NULL | 2015-05-01 | month-5-str |
| 1 | NULL | 2015-01-01 | month-1-str |
| 3 | NULL | 2015-03-01 | month-3-str |
| 6 | NULL | 2015-06-01 | month-6-str |
| 7 | NULL | 2015-07-01 | month-7-str |
| 8 | NULL | 2015-08-01 | month-8-str |
+----+------+------------+-------------+
8 rows in set (0.01 sec)
4.3 物理庫查詢
前 4 個月份的數據進入 3306 服務的物理庫
mysql> select * from range_db_4.t_range_date;
select * from range_db_5.t_range_date;
select * from range_db_6.t_range_date;
select * from range_db_7.t_range_date;
+----+------+------------+-------------+
| id | date | date_str | context |
+----+------+------------+-------------+
| 1 | NULL | 2015-01-01 | month-1-str |
+----+------+------------+-------------+
1 row in set (0.00 sec)
+----+------+------------+-------------+
| id | date | date_str | context |
+----+------+------------+-------------+
| 2 | NULL | 2015-02-01 | month-2-str |
+----+------+------------+-------------+
1 row in set (0.00 sec)
+----+------+------------+-------------+
| id | date | date_str | context |
+----+------+------------+-------------+
| 3 | NULL | 2015-03-01 | month-3-str |
+----+------+------------+-------------+
1 row in set (0.00 sec)
+----+------+------------+-------------+
| id | date | date_str | context |
+----+------+------------+-------------+
| 4 | NULL | 2015-04-01 | month-4-str |
+----+------+------------+-------------+
1 row in set (0.00 sec)
5 優缺點分析
1.可以做簡單的按月分片,如果真要商起來,可以將一個季度的數據配置到相同的datanode 里去。
2.不能按年進行循環配置,如果數據結點不足時需要提前加入,並手動清理歷史數據。
本篇完