《MYSQL教程Mysql 5.7从节点配置多线程主从复制的方法详解》要点:
本文介绍了MYSQL教程Mysql 5.7从节点配置多线程主从复制的方法详解,希望对您有用。如果有疑问,可以联系我们。
MYSQL实例前言
MYSQL实例Mysql 采用多线程进行复制是从 Mysql 5.6 开始支持的内容,但是 5.6 版本下有缺陷,虽然支持多线程,但是每个数据库只能一个线程,也就是说如果我们只有一个数据库,则主从复制时也只有一个线程在工作.相当于还是以前的单线程. 从 Mysql 5.7 开始支持同一数据库下并行主从复制.不过默认情况下,还是单数据库单个线程,如果需要使用多线程,需要在从节点进行配置.
MYSQL实例Mysql 5.7 对主从复制增加了一种类型,共有两种类型,如下:
MYSQL实例下面的步骤,在从节点上进行配置.
MYSQL实例查看当前配置
MYSQL实例在开始配置之前,我们先看一下当前配置下的主从复制的进程数.
MYSQL实例
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)
MYSQL实例从上面看出只有一个主进程在等待同步.
MYSQL实例下面查看复制类型和并行数量配置
MYSQL实例
mysql> show variables like 'slave_parallel_type';
+---------------------+----------+
| Variable_name | Value |
+---------------------+----------+
| slave_parallel_type | DATABASE |
+---------------------+----------+
1 row in set (0.00 sec)
MYSQL实例当前的复制类型是 DATABASE,也就是统一数据库下只有一个线程进行复制,不能并行复制.
MYSQL实例
mysql> show variables like 'slave_parallel_workers';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| slave_parallel_workers | 0 |
+------------------------+-------+
1 row in set (0.01 sec)
MYSQL实例当前并行工作的进程数是 0
MYSQL实例配置多线程
MYSQL实例1、停止从节点复制
MYSQL实例
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
MYSQL实例2、设置复制类型为 LOGICAL_CLOCK
MYSQL实例
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)
MYSQL实例3、设置并行数量为 4
MYSQL实例
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)
MYSQL实例4、启动从节点复制
MYSQL实例
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
MYSQL实例5、查看一下当前工作的线程数
MYSQL实例
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)
MYSQL实例最后说一下为什么需要多线程复制?因为主从之间的同步会有延时,多线程的目的是为了尽量减少这个延时时间.虽然如何优化主从是一个系统的功能,不同的场景需要不同的解决方案,但是多线程至少从基础上能减少延迟时间.另外根据数据库的实际情况,能否真正减少延时,以及配置多少线程,则需要反复的测试得出适合自己的数据.
MYSQL实例总结
MYSQL实例以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对维易PHP的支持.
转载请注明本页网址:
http://www.vephp.com/jiaocheng/4051.html