MySQL系列详解八:MySQL多线程复制演示-技术流ken

前言

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

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

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

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

首先需要搭建出来一个主从复制的架构出来,这里就不再演示,请参考上一篇博客《MySQL系列详解六:MySQL主从复制/慢同步演示-技术流ken

演示:实现MySQL多线程复制

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

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

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

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

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

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

3.停止从节点的复制

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

4.修改复制类型为LOGICAL_CLOCK 

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

5.修改并行数量为5

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

6.启动从节点

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

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

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

总结

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

上一篇:Entity Framework with MySQL 学习笔记一(查询)


下一篇:python之raise、assert、with/as环境管理器