1. 程式人生 > >Mysql 5.7 從節點配置多執行緒主從複製

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)

最後說一下為什麼需要多執行緒複製?因為主從之間的同步會有延時,多執行緒的目的是為了儘量減少這個延時時間。雖然如何優化主從是一個系統的功能,不同的場景需要不同的解決方案,但是多執行緒至少從基礎上能減少延遲時間。另外根據資料庫的實際情況,能否真正減少延時,以及配置多少執行緒,則需要反覆的測試得出適合自己的資料。