MySQL多线程复制演示

By | 2018年11月20日
目录
[隐藏]

前言

Mysql 采用多线程进行复制是从 Mysql 5.6 开始支持的内容,但是 5.6 版本下有缺陷,虽然支持多线程,但是每个数据库只能一个线程,也就是说如果我们只有一个数据库,则主从复制时也只有一个线程在工作。相当于还是以前的单线程。 从 Mysql 5.7 开始支持同一数据库下并行主从复制。不过默认情况下,还是单数据库单个线程,如果需要使用多线程,需要在从节点进行配置。

Mysql 5.7 对主从复制增加了一种类型,共有两种类型,如下:

  • DATABASE 基于库的并行复制 , 每个数据库对应一个复制线程
  • LOGICAL_CLOCK 基于组提交的并行复制方式,同一个数据库下可以有多个线程

下面的步骤,在从节点上进行配置。其实只需要修改两处变量即可。

 

首先需要搭建出来一个主从复制的架构出来,这里就不再演示。

 

演示:实现MySQL多线程复制

 

1.在从节点查看当前的主从复制的进程数

MySQL [(none)]> show processlist;
+----+-------------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User        | Host               | db   | Command     | Time | State                                                         | Info             |
+----+-------------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
|  5 | ken         | 10.220.5.137:44070 | NULL | Binlog Dump | 4387 | Master has sent all binlog to slave; waiting for more updates | NULL             |
|  6 | system user |                    | NULL | Connect     | 4361 | Waiting for master to send event                              | NULL             |
|  7 | system user |                    | NULL | Connect     | 4084 | Slave has read all relay log; waiting for more updates        | NULL             |
| 10 | root        | localhost          | NULL | Query       |    0 | starting                                                      | show processlist |
+----+-------------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+

从上面看出只有一个主进程在等待同步。

 

2.下面查看复制类型和并行数量配置

MySQL [(none)]> show global variables like '%slave_para%';
+------------------------+----------+
| Variable_name          | Value    |
+------------------------+----------+
| slave_parallel_type    | DATABASE |
| slave_parallel_workers | 0        |
+------------------------+----------+
2 rows in set (0.02 sec)

当前的复制类型是 DATABASE,也就是同一数据库下只有一个线程进行复制,不能并行复制。

 

3.停止从节点的复制

MySQL [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)

 

4.修改复制类型为LOGICAL_CLOCK 

MySQL [(none)]> set global slave_parallel_type=LOGICAL_CLOCK;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> show global variables like '%slave_para%';
+------------------------+---------------+
| Variable_name          | Value         |
+------------------------+---------------+
| slave_parallel_type    | LOGICAL_CLOCK |
| slave_parallel_workers | 0             |
+------------------------+---------------+
2 rows in set (0.00 sec)

 

5.修改并行数量为5

MySQL [(none)]> set global slave_parallel_workers=5;
Query OK, 0 rows affected (0.00 sec)

MySQL [(none)]> show global variables like '%slave_para%';
+------------------------+---------------+
| Variable_name          | Value         |
+------------------------+---------------+
| slave_parallel_type    | LOGICAL_CLOCK |
| slave_parallel_workers | 5             |
+------------------------+---------------+
2 rows in set (0.00 sec)

 

6.启动从节点

MySQL [(none)]> start slave;
Query OK, 0 rows affected (0.04 sec)

 

7.再次查看复制类型和并行数量配置

MySQL [(none)]> show processlist;
+----+-------------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User        | Host               | db   | Command     | Time | State                                                         | Info             |
+----+-------------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
|  5 | ken         | 10.220.5.137:44070 | NULL | Binlog Dump | 4855 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 10 | root        | localhost          | NULL | Query       |    0 | starting                                                      | show processlist |
| 11 | system user |                    | NULL | Connect     |   45 | Waiting for master to send event                              | NULL             |
| 12 | system user |                    | NULL | Connect     |   45 | Slave has read all relay log; waiting for more updates        | NULL             |
| 13 | system user |                    | NULL | Connect     |   45 | Waiting for an event from Coordinator                         | NULL             |
| 14 | system user |                    | NULL | Connect     |   45 | Waiting for an event from Coordinator                         | NULL             |
| 15 | system user |                    | NULL | Connect     |   45 | Waiting for an event from Coordinator                         | NULL             |
| 16 | system user |                    | NULL | Connect     |   45 | Waiting for an event from Coordinator                         | NULL             |
| 17 | system user |                    | NULL | Connect     |   45 | Waiting for an event from Coordinator                         | NULL             |
+----+-------------+--------------------+------+-------------+------+---------------------------------------------------------------+------------------+
9 rows in set (0.00 sec)

 

总结

最后说一下为什么需要多线程复制?因为主从之间的同步会有延时,多线程的目的是为了尽量减少这个延时时间。虽然如何优化主从是一个系统的功能,不同的场景需要不同的解决方案,但是多线程至少从基础上能减少延迟时间。另外根据数据库的实际情况,能否真正减少延时,以及配置多少线程,则需要反复的测试得出适合自己的数据。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注