[mycat] Common sharding rules

1. Commonly used fragmentation rules

1, Tori model

​ This rule is a modulation operation on the fragmented field. It is also the most commonly used rule for horizontal sub-tables.

2. Shard enumeration

​ Configure sharding by configuring possible enumeration IDs in the configuration file. This rule is suitable for specific scenarios. For example, some businesses need to be saved according to provinces or districts and counties, and provinces, districts and counties across the country are fixed. This type of business Use this rule.

This is achieved as follows:

2.1. Modify the schema.xml configuration file

> [External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-I3bFd6zV-1668606531894)(assets/1657606799331.png)]

The test table is orders_ware_info, which is configured on the dn1 and dn2 nodes. The rule is to add a sharding_by_intfile

2.2. Modify the rule.xml configuration file

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-5Xuivj2H-1668606531895)(assets/1657606924447.png)] [The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-uTGUOEg0-1668606531896)(assets/1657607036319.png)]

2.3. Modify the partition-hash-int.txt configuration file

It means that if the areacode is 110, it will be stored in the first data node, and if it is 120, it will be stored in the second data node.

2.4. Restart Mycat to make the configuration take effect.

Use mycat to create an order ownership area information table:

CREATE TABLE orders_ware_info ( 
	`id` INT AUTO_INCREMENT comment '编号', 
	`order_id` INT comment '订单编号', 
	`address` VARCHAR(200) comment '地址', 
	`areacode` VARCHAR(20) comment '区域编号', 
	PRIMARY KEY(id) 
); 

Insert data:

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'); 

Insert image description here

Direct query through the database:

spdb_pzex:

Insert image description here

spdb_portal_wechat:
Insert image description here

3. Scope Agreement

​ This sharding is suitable for planning in advance which shard a certain range of sharding fields belongs to.

3.1. Modify the schema.xml configuration file

Insert image description here

The above defines the table and the stored data nodes and sharding rules , etc.

3.2. Modify the rule.xml configuration file

Insert image description here

3.3. Modify the autopartition-long.txt configuration file

Insert image description here

3.4. Restart Mycat to make the configuration take effect.

Insert image description here

Create the payment information table payment_info in mycat:

CREATE TABLE payment_info( 
	`id` INT AUTO_INCREMENT comment '编号', 
	`order_id` INT comment '订单编号', 
	`payment_status` INT comment '支付状态', 
	PRIMARY KEY(id)
); 

Insert data:

INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (1,101,0);
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (2,102,1);
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (3,103,0);
INSERT INTO PAYMENT_INFO (id,order_id,payment_status) VALUES (4,104,1); 

Insert image description here

View the data of the two host data nodes separately:

spdb_pzex:
Insert image description here
spdb_portal_wechat:
Insert image description here

4. Segmentation by date (day)

This rule is sharding by day. Set time format and range

4.1. Modify the schema.xml configuration file

Insert image description here

4.2. Modify the rule.xml configuration file

Insert image description here

You need to customize a sharding function shardingByDate:

Insert image description here

4.3. Restart Mycat to make the configuration take effect and restart.

Insert image description here

Create user information table login_info in mycat:

CREATE TABLE login_info( 
	`id` INT AUTO_INCREMENT comment '编号', 
	`user_id` INT comment '用户编号', 
	`login_date` date comment '登录日期', 
	PRIMARY KEY(id)
); 

Insert data:

INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (1,101,'2019-01-01');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (2,102,'2019-01-02');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (3,103,'2019-01-03');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (4,104,'2019-01-04');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (5,103,'2019-01-05');
INSERT INTO LOGIN_INFO(id,user_id,login_date) VALUES (6,104,'2019-01-06'); 

Insert image description here

View the data of the two host data nodes separately:

spdb_pzex:
Insert image description here

spdb_portal_wechat:
> [)(assets/1657608068820.png)]

2. mycat high availability

https://blog.csdn.net/K_520_W/article/details/123781475

Guess you like

Origin blog.csdn.net/qq_60969145/article/details/127894220