一、取模
此規則是對分片字段求模運算。也就是水準分表裡最常用的規則。例如 Mycat 之 水準拆分(分表) 主表【orders】采用的規則。
二、分片枚舉
通過在配置檔案中配置可能的枚舉id,自己配置分片,本規則适用于特定的場景,比如有些業務需要按照省份或區縣來儲存,而全國省份區縣固定的
1、配置檔案
vim /usr/local/mycal/conf/schema.xml
<table name="orders_ware_info" dataNode="dn1,dn2" rule="sharding_by_intfile"></table>
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuEDOwMzM1cTMwITMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
在rule配置檔案裡新增分片規則sharding_by_intfile
vim /usr/local/mycal/conf/rule.xml
<tableRule name="sharding_by_intfile">
<rule>
<columns>areacode</columns>
<algorithm>hash-int</algorithm>
</rule>
</tableRule>
……
<function name="hash-int"
class="io.mycat.route.function.PartitionByFileMap">
<property name="mapFile">partition-hash-int.txt</property>
<property name="type">1</property>
<property name="defaultNode">0</property>
</function>
<tableRule>中的
columns:分片字段;
algorithm:分片函數
<property>中的
mapFile:辨別配置檔案名稱;
type:0為int型,非0為string;
defaultNode:預設節點。小于0表示不設定預設節點,大于等于0表示設定預設節點,設定預設節點如果碰不到識别的枚舉值,就讓它路由到預設節點,如不設定不識别就報錯。
vim /usr/local/mycal/conf/partition-hash-int.txt
110=0
120=1
2、重新開機mycat服務
mycat console
3、在mycat建立表,并插入資料
#訂單歸屬區域資訊表
CREATE TABLE `orders_ware_info` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '編号',
`order_id` int(11) DEFAULT NULL COMMENT '訂單編号',
`address` varchar(200) DEFAULT NULL COMMENT '位址',
`areacode` varchar(20) DEFAULT NULL COMMENT '區域編号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#插入資料
INSERT INTO ORDERS_WARE_INFO(id, order_id,address,areacode) VALUES (1,1,'北京','110');
INSERT INTO ORDERS_WARE_INFO(id, order_id,address,areacode) VALUES (2,2,'天津','120');
錯誤:
1)、執行insert語句時,報錯
ERROR 1146 (HY000): Table 'orders.orders_ware_info' doesn't exist
原因:mycat建立的表名是大寫的
解決方法:
INSERT INTO ORDERS_WARE_INFO(id, order_id,address,areacode) VALUES (1,1,'北京','110');
INSERT INTO ORDERS_WARE_INFO(id, order_id,address,areacode) VALUES (2,2,'天津','120');
2)、ERROR 1366 (HY000): Incorrect string value: '\xE5\x8C\x97\xE4\xBA\xAC' for column 'ADDRESS' at row 1
原因:建立表時為規定charset=utf8
解決方法:
建立表的時候規定好charset
4、在mycat、主機1、主機2裡檢視資料分片效果
三、範圍約定
适用于提前規劃好分片字段某個範圍屬于哪個分片。
1、配置檔案
vim /usr/local/mycal/conf/schema.xml
<table name="payment_info" dataNode="dn1,dn2" rule="auto_sharding_long" ></table>
在rule配置檔案裡新增分片規則auto_sharding_long
vim /usr/local/mycal/conf/rule.xml
<tableRule name="auto_sharding_long">
<rule>
<columns>order_id</columns>
<algorithm>rang-long</algorithm>
</rule>
</tableRule>
<function name="rang-long"
class="io.mycat.route.function.AutoPartitionByLong">
<property name="mapFile">autopartition-long.txt</property>
<property name="defaultNode">0</property>
</function>
<tableRule>中的
columns:分片字段;
algorithm:分片函數
<property>中的
mapFile:辨別配置檔案名稱;
defaultNode:預設節點。小于0表示不設定預設節點,大于等于0表示設定預設節點,設定預設節點如果碰不到識别的枚舉值,就讓它路由到預設節點,如不設定不識别就報錯。
vim /usr/local/mycal/conf/autopartition-long.txt
0-100=0
101-200=1
2、重新開機mycat服務
mycat console
3、在mycat建立表,并插入資料
#支付資訊表
CREATE TABLE payment_info
(
`id` INT AUTO_INCREMENT comment '編号',
`order_id` INT comment '訂單編号',
`payment_status` INT comment '支付狀态',
PRIMARY KEY(id)
) DEFAULT CHARSET=utf8;
#插入資料
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (1,99,0);
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (2,100,1);
INSERT INTO PAYMENT_INFO (id,order_id ,payment_status) VALUES (3,101,0);
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (4,102,1);
4、在mycat、主機1、主機2裡檢視資料分片效果
四、按日期(天)分片
按天分片,設定時間格式、範圍。
1、配置檔案
vim /usr/local/mycal/conf/schema.xml
<table name="login_info" dataNode="dn1,dn2" rule="sharding_by_date" ></table>
在rule配置檔案裡新增分片規則sharding_by_date
vim /usr/local/mycal/conf/rule.xml
<tableRule name="sharding_by_date">
<rule>
<columns>login_date</columns>
<algorithm>shardingByDate</algorithm>
</rule>
</tableRule>
<function name="shardingByDate"
class="io.mycat.route.function.PartitionByDate">
<property name="dateFormat">yyyy-MM-dd</property>
<property name="sBeginDate">2020-11-21</property>
<property name="sEndDate">2020-11-24</property>
<property name="sPartionDay">2</property>
</function>
<tableRule>中的
columns:分片字段;
algorithm:分片函數
<property>中的
dateFormat:日期格式;
sBeginDate:開始日期。
sEndDate:結束日期,則代表資料達到了這個日期的分片後循環開始分片插入
sPartionDay:分區天數,即預設從開始日期算起,分隔2天一個分區
2、重新開機mycat服務
mycat console
3、在mycat建立表,并插入資料
#使用者資訊表
CREATE TABLE login_info
(
`id` INT AUTO_INCREMENT comment '編号',
`user_id` INT comment '使用者編号',
`login_date` date comment '登入日期',
PRIMARY KEY(id)
)DEFAULT CHARSET=utf8;
#插入資料
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (1,101,'2020-11-21');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (2,102,'2020-11-22');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (3,103,'2020-11-23');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (4,104,'2020-11-24');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (5,103,'2020-11-25');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (6,104,'2020-11-26');
4、在mycat、主機1、主機2裡檢視資料分片效果