Mysql 5.7 从节点配置多线程主从复制

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

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

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

下面的步骤,在从节点上进行配置。

查看当前配置

在开始配置之前,我们先看一下当前配置下的主从复制的进程数。

1
2
3
4
5
6
7
8
9
mysql> show processlist;
+----+-------------+-----------+------+---------+-------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time  | State                                                  | Info             |
+----+-------------+-----------+------+---------+-------+--------------------------------------------------------+------------------+
|  1 | system user |           | NULL | Connect | 91749 | Waiting for master to send event                       | NULL             |
|  2 | system user |           | NULL | Connect |   208 | Slave has read all relay log; waiting for more updates | NULL             |
| 37 | root        | localhost | NULL | Query   |     0 | starting                                               | show processlist |
+----+-------------+-----------+------+---------+-------+--------------------------------------------------------+------------------+
3 rows in set (0.00 sec)

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

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

1
2
3
4
5
6
7
mysql> show variables like 'slave_parallel_type';
+---------------------+----------+
| Variable_name       | Value    |
+---------------------+----------+
| slave_parallel_type | DATABASE |
+---------------------+----------+
1 row in set (0.00 sec)

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

1
2
3
4
5
6
7
mysql> show variables like 'slave_parallel_workers';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| slave_parallel_workers | 0     |
+------------------------+-------+
1 row in set (0.01 sec)

当前并行工作的进程数是 0

配置多线程

1、停止从节点复制

1
2
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

2、设置复制类型为 LOGICAL_CLOCK

1
2
3
4
5
6
7
8
9
10
mysql> set global slave_parallel_type='logical_clock';
Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'slave_parallel_type';
+---------------------+---------------+
| Variable_name       | Value         |
+---------------------+---------------+
| slave_parallel_type | LOGICAL_CLOCK |
+---------------------+---------------+
1 row in set (0.01 sec)

3、设置并行数量为 4

1
2
3
4
5
6
7
8
9
10
11
mysql> set global slave_parallel_workers=4;
Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'slave_parallel_workers';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| slave_parallel_workers | 4     |
+------------------------+-------+
1 row in set (0.00 sec)

4、启动从节点复制

1
2
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

5、查看一下当前工作的线程数

1
2
3
4
5
6
7
8
9
10
11
12
13
mysql> show processlist;
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host      | db   | Command | Time | State                                                  | Info             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
| 37 | root        | localhost | NULL | Query   |    0 | starting                                               | show processlist |
| 38 | system user |           | NULL | Connect |    8 | Waiting for master to send event                       | NULL             |
| 39 | system user |           | NULL | Connect |    7 | Slave has read all relay log; waiting for more updates | NULL             |
| 40 | system user |           | NULL | Connect |    8 | Waiting for an event from Coordinator                  | NULL             |
| 41 | system user |           | NULL | Connect |    8 | Waiting for an event from Coordinator                  | NULL             |
| 42 | system user |           | NULL | Connect |    8 | Waiting for an event from Coordinator                  | NULL             |
| 43 | system user |           | NULL | Connect |    8 | Waiting for an event from Coordinator                  | NULL             |
+----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
7 rows in set (0.00 sec)

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

实例配置:

cat backup-my.cnf
# This MySQL options file was generated by innobackupex. # The MySQL server
[mysqld]
port = 3310
#innodb_checksum_algorithm=innodb
#innodb_log_checksum_algorithm=innodb
innodb_data_file_path=ibdata1:200M:autoextend
innodb_log_files_in_group=2
innodb_log_file_size=1572864000
#innodb_fast_checksum=false
#innodb_page_size=16384
#innodb_log_block_size=512
innodb_undo_directory=.
innodb_undo_tablespaces=0
server-id = 1234
binlog-format = ROW
gtid-mode = ON
enforce-gtid-consistency = true
relay_log_recovery = 1
sync-binlog = 0
skip_slave_start = 1 slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=32
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON
#rds_encrypt_data=false
#innodb_encrypt_algorithm=aes_128_ecb

  

mysql> show processlist;
+----+-------------+-----------+------+---------+-------+----------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+-------+----------------------------------------+------------------+
| 2 | root | localhost | NULL | Query | 0 | starting | show processlist |
| 3 | system user | | NULL | Connect | 13 | Queueing master event to the relay log | NULL |
| 4 | system user | | NULL | Connect | 0 | System lock | NULL |
| 5 | system user | | NULL | Connect | 49775 | Waiting for an event from Coordinator | NULL |
| 6 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 7 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 8 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 9 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 10 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 11 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 12 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 13 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 14 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 15 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 16 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 17 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 18 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 19 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 20 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 21 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 22 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 23 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 24 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 25 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 26 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 27 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 28 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 29 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 30 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 31 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 32 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 33 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 34 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 35 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
| 36 | system user | | NULL | Connect | 13 | Waiting for an event from Coordinator | NULL |
+----+-------------+-----------+------+---------+-------+----------------------------------------+------------------+
35 rows in set (0.00 sec) mysql> show global variables like 'slave_parallel_workers';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| slave_parallel_workers | 32 |
+------------------------+-------+
1 row in set (0.00 sec) mysql> show global variables like '%slave_parallel_type%';
+---------------------+---------------+
| Variable_name | Value |
+---------------------+---------------+
| slave_parallel_type | LOGICAL_CLOCK |
+---------------------+---------------+
1 row in set (0.00 sec)

  

上一篇:opencv_形态学结构化元素对形态学图像处理的影响


下一篇:linux中>/dev/null 2>&1和2>&1 > /dev/null