当前位置:网站首页>Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
2022-07-31 02:46:00 【fascinated*】
主从关系
Prepare three different setsip的虚拟机
(第一批)The configuration of the master-slave relationship
主192.168.47.131 配置/etc/my.cnf,在【mysqld】下配置
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
Then configure it here
server-id=1
log-bin=/var/lib/mysql/mysqlbin
read-only=0
binlog-ignore-db=mysql从192.168.47.132 配置/etc/my.cnf,在【mysqld】下配置,master-slave relationshipid不一致(切记)
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
Then configure it here
server-id=2
log-bin=/var/lib/mysql/mysqlbin设置读写分离balance=3,Will read the requestwritehost对应的标签readhost上,The data in the database slave table is inconsistent with the master table,springbootwill read the data from the table Because the slave table is a read operation
Check the master node status on the master terminal
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| mysqlbin.000003 | 2168 | | mysql | |
+-----------------+----------+--------------+------------------+-------------------+Set the master-slave relationship on the slave terminal
mysql> change master to master_host= '192.168.47.131', master_user='root', master_password='123456', master_log_file='mysqlbin.000003', master_log_pos=2168;
Query OK, 0 rows affected, 8 warnings (0.00 sec)运行show slave status\G;
可看到yes yes Both must be Otherwise, the master and slave are not configured

常用命令
开启主从关系 start slave;
关闭主从关系 stop slave;
重置主节点 reset master;
View the status of the master-slave relationship show slave status\G;
Configuration of vertical sub-libraries
To avoid too many virtual machines Stop the master-slave relationship stop slave;
(第一批)Configuration of vertical sub-libraries
scehma.xml

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<!-- schemaLabels that define the library name:The name of the library dataNode:Represents the node name associated with the library -->
<schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1">
<table name="customer" dataNode="dn2"></table>
</schema>
<!-- dataNode:定义节点 name:The node name must be the same as aboveschema的dataNode值保持一致 dataNode:The associated hostname
database:The actual database name associated with it
-->
<dataNode name="dn1" dataHost="host1" database="my_order" />
<dataNode name="dn2" dataHost="host2" database="my_consumer" />
<!-- name:数据主机的名称 -->
<dataHost name="host1" maxCon="1000" minCon="10" balance="0"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<!-- According to the heartbeat mechanism to determine whether the real database is running normally-->
<heartbeat>select user()</heartbeat>
<!-- Configure the information for the master node -->
<writeHost host="hostM1" url="192.168.47.131:3306" user="root"
password="123456">
</writeHost>
<!-- <writeHost host="hostM2" url="localhost:3316" user="root" password="123456"/> -->
</dataHost>
<dataHost name="host2" maxCon="1000" minCon="10" balance="3"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<!-- According to the heartbeat mechanism to determine whether the real database is running normally-->
<heartbeat>select user()</heartbeat>
<!-- Configure the information for the master node -->
<writeHost host="hostM1" url="192.168.47.132:3306" user="root"
password="123456">
</writeHost>
</dataHost>
</mycat:schema>测试垂直分库
mysql> use TESTDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show databases;
+----------+
| DATABASE |
+----------+
| TESTDB |
+----------+
1 row in set (0.00 sec)
mysql> CREATE TABLE customer(
-> id INT AUTO_INCREMENT,
-> NAME VARCHAR(200),
-> PRIMARY KEY(id)
-> );
Query OK, 0 rows affected (0.08 sec)
mysql> CREATE TABLE orders(
-> id INT AUTO_INCREMENT,
-> order_type INT,
-> customer_id INT,
-> amount DECIMAL(10,2),
-> PRIMARY KEY(id)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE orders_detail(
-> id INT AUTO_INCREMENT,
-> detail VARCHAR(2000),
-> order_id INT,
-> PRIMARY KEY(id)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> CREATE TABLE dict_order_type(
-> id INT AUTO_INCREMENT,
-> order_type VARCHAR(200),
-> PRIMARY KEY(id)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> select *from customer;
Empty set (0.05 sec)
mysql> select *from orders;
Empty set (0.00 sec) Sub-table completed 在不同的数据库中
Detailed vertical configuration details

水平分库
mysqlThere is a limit to the number of records stored in a single table 最大达到10000条 那么如何在10000After the bar, continue to continue the data in the table 使用mycat进行水平分表 When the number of entries in a table is limited The number of entries of the same data field table can be continued in other databases Perform horizontal splitting if there is data in the table10000条 Then there is a table in another database when split horizontally5000条 这就是水平拆分
总节点数(有多少个数据库)number of customersid取余 余数为0在一个表 余数为1in another table
在schema.xml中配置

在rule.xml文件中配置
<tableRule name="mod_rule">
<rule>
#以customer_idThe most horizontal split table standard
<columns>customer_id</columns>
<algorithm>mod-long</algorithm>
</rule>
</tableRule><function name="mod-long" class="io.mycat.route.function.PartitionByMod">
<!-- how many data nodes -->
<property name="count">2</property>
#此处count=2It can be changed according to the current business volume
</function>You want to split the data in the table horizontally This database will create a table with the same fields as the split table
Insert statements without fields cannot be made,否则会报错
mysql> INSERT INTO orders VALUES (1,101,100,100100);
ERROR 1064 (HY000): partition table, insert must provide ColumnList正确操作
[[email protected] ~]# mysql -umycat -p123456 -P 8066 -h192.168.47.133
mysql> use TESTDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
+--------------------+
| Tables_in_my_order |
+--------------------+
| customer |
| orders |
| dict_order_type |
| orders_detail |
+--------------------+
4 rows in set (0.01 sec)
mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES (1,101,100,100100);
(id,order_type,customer_id,amount) VALUES(4,101,101,103000);
INSERT INTO orders(id,order_type,customer_id,amount) VALUES(5,102,101,100400);
INSERT INTO orders(id,order_type,customer_id,amount) VALUES(6,102,100,100020);Query OK, 1 row affected (0.08 sec)
mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(2,101,100,100300);
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(3,101,101,120000);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(4,101,101,103000);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(5,102,101,100400);
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO orders(id,order_type,customer_id,amount) VALUES(6,102,100,100020);
Query OK, 1 row affected (0.01 sec)最终结果customer_id 100对2取余为0 in a database table 101对2取余为1in another database table
显然 1 2 6 余数为0 3 4 5 余数为1

The level sub-table is completed
mycat分片join进行联表查询
ordersThe splitting operation has already been performed 那么和它关联的order_detail订单详情表如何进行join查询 Right noworders_detail进行分片操作 Want to correlate queries The one with the foreign key is the child table The primary table has no foreign keys
The records of the child table are placed on the same data shard as the records of the associated parent table
Sharding is the configuration of the associated table
在schema.xml文件中,具体配置如下

at the time of verificationmy_consumerAdd order details table to database
#订单详细表 rows:600万
CREATE TABLE orders_detail(
id INT AUTO_INCREMENT,
detail VARCHAR(2000),
order_id INT,
PRIMARY KEY(id)
);重启mycat 进入mycat终端测试
进入mycat安装目录的bin下 ./mycat console
mysql> use TESTDB;(切记 Don't forget to switch databases 这个非常容易出错 The database is wrong 就连接不上 操作不了)
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
#进行插入数据(订单详情)
mysql> INSERT INTO orders_detail(id,detail,order_id) values(61,'detail61',1);
Query OK, 1 row affected (0.09 sec)
mysql> INSERT INTO orders_detail(id,detail,order_id) VALUES(62,'detail62',2);
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO orders_detail(id,detail,order_id) VALUES(63,'detail63',3);
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO orders_detail(id,detail,order_id) VALUES(64,'detail64',4);
Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO orders_detail(id,detail,order_id) VALUES(65,'detail65',5);
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO orders_detail(id,detail,order_id) VALUES(66,'detail66',6);
Query OK, 1 row affected (0.06 sec)
#进行联表查询 You can see that the query is successful Order details can be seen in the order form
mysql> select * from orders as o inner join orders_detail as od on o.id=od.order_id;
;
+----+------------+-------------+-----------+----+----------+----------+
| id | order_type | customer_id | amount | id | detail | order_id |
+----+------------+-------------+-----------+----+----------+----------+
| 1 | 101 | 100 | 100100.00 | 61 | detail61 | 1 |
| 2 | 101 | 100 | 100300.00 | 62 | detail62 | 2 |
| 6 | 102 | 100 | 100020.00 | 66 | detail66 | 6 |
| 3 | 101 | 101 | 120000.00 | 63 | detail63 | 3 |
| 4 | 101 | 101 | 103000.00 | 64 | detail64 | 4 |
| 5 | 102 | 101 | 100400.00 | 65 | detail65 | 5 |
+----+------------+-------------+-----------+----+----------+----------+
6 rows in set (0.02 sec)
联表查询结果
mysql> select o.*,od.detail from orders o inner join orders_detail od on o.id=od.order_id;
+----+------------+-------------+-----------+----------+
| id | order_type | customer_id | amount | detail |
+----+------------+-------------+-----------+----------+
| 3 | 101 | 101 | 120000.00 | detail63 |
| 4 | 101 | 101 | 103000.00 | detail64 |
| 5 | 102 | 101 | 100400.00 | detail65 |
| 1 | 101 | 100 | 100100.00 | detail61 |
| 2 | 101 | 100 | 100300.00 | detail62 |
| 6 | 102 | 100 | 100020.00 | detail66 |
+----+------------+-------------+-----------+----------+
6 rows in set (0.01 sec)分片完成 The joint table query is completed 完美 说明一下mycat不支持mysql8.0.28 本人亲自踩坑 Experimentation is recommendedmysql5.7系列版本
Provides a table build statement
#客户表 rows:20万
CREATE TABLE customer(
id INT AUTO_INCREMENT,
NAME VARCHAR(200),
PRIMARY KEY(id)
);
#订单表 rows:600万
CREATE TABLE orders(
id INT AUTO_INCREMENT,
order_type INT,
customer_id INT,
amount DECIMAL(10,2),
PRIMARY KEY(id)
);
#订单详细表 rows:600万
CREATE TABLE orders_detail(
id INT AUTO_INCREMENT,
detail VARCHAR(2000),
order_id INT,
PRIMARY KEY(id)
);
#订单状态字典表 rows:20
CREATE TABLE dict_order_type(
id INT AUTO_INCREMENT,
order_type VARCHAR(200),
PRIMARY KEY(id)
);边栏推荐
- SQL注入 Less47(报错注入) 和Less49(时间盲注)
- Manchester City confuses fans with smart scarf that detects emotions
- BAT卖不动「医疗云」:医院逃离、山头林立、行有行规
- Linux下redis7的安装,启动与停止
- TCP/IP四层模型
- AI software development process in medical imaging field
- Chapter 9 SVM实践
- 19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing
- The whole process scheduling, MySQL and Sqoop
- 软件积累 -- 截图软件ScreenToGif
猜你喜欢

Basic learning about Redis related content

STM32CUBEMX develops GD32F303 (11) ---- ADC scans multiple channels in DMA mode

11. Redis implements follow, unfollow, and follow and follower lists

Problems that need to be solved by the tcp framework

4、敏感词过滤(前缀树)

Installation, start and stop of redis7 under Linux

The application of AI in the whole process of medical imaging equipment

Huawei od dice js

1. Non-type template parameters 2. Specialization of templates 3. Explanation of inheritance

There is a problem with the multiplayer-hlap package and the solution cannot be upgraded
随机推荐
STM32CUBEMX develops GD32F303 (11) ---- ADC scans multiple channels in DMA mode
Introduction to flask series 】 【 flask - using SQLAlchemy
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
StringJoiner详解
7. List of private messages
YOLOV5 study notes (3) - detailed explanation of network module
How to build a private yum source
SQL注入 Less54(限制次数的SQL注入+union注入)
基于opencv实现人脸检测
16. Registration Center-consul
golang GUI for nuxui — HelloWorld
6、显示评论和回复
try-catch中含return
4、敏感词过滤(前缀树)
加密公司向盗窃的黑客提供报价:保留一点,把剩下的归还
10. Redis implements likes (Set) and obtains the total number of likes
CMOS和TTL的区别?
Static route analysis (the longest mask matching principle + active and standby routes)
Uninstallation of mysql5.7.37 under CentOS7 [perfect solution]
mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)