mysql5.7.19的半同步复制问题分享
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql5.7.19的半同步复制问题分享相关的知识,希望对你有一定的参考价值。
===
异步复制(Asynchronous replication)
mysql默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给给客户端,并不关心从库是否已经接收并处理,这样就会有一个问题,主如果crash掉了,此时主上已经提交的事务可能并没有传到从上,如果此时,强行将从提升为主,可能导致新主上的数据不完整。
全同步复制(Fully synchronous replication)
指当主库执行完一个事务,所有的从库都执行了该事务才返回给客户端。因为需要等待所有从库执行完该事务才能返回,所以全同步复制的性能必然会收到严重的影响。
半同步复制(Semisynchronous replication)
介于异步复制和全同步复制之间,主库在执行完客户端提交的事务后不是立刻返回给客户端,而是等待至少一个从库接收到并写到relay log中才返回给客户端。相对于异步复制,半同步复制提高了数据的安全性,同时它也造成了一定程度的延迟,这个延迟最少是一个TCP/IP往返的时间。所以,半同步复制最好在低延时的网络中使用。
引用:http://www.cnblogs.com/ivictor/p/5735580.html
AFTER_SYNC (the default): The master writes each transaction to its binary log and the slave, and syncs the binary log to disk. The master waits for slave acknowledgment of transaction receipt after the sync. Upon receiving acknowledgment, the master commits the transaction to the storage engine and returns a result to the client, which then can proceed.
AFTER_COMMIT: The master writes each transaction to its binary log and the slave, syncs the binary log, and commits the transaction to the storage engine. The master waits for slave acknowledgment of transaction receipt after the commit. Upon receiving acknowledgment, the master returns a result to the client, which then can proceed.
====
半同步复制会设置一个超时的时间,如果超过这个时间从服务器没有给主服务器确认信息,主服务器会启用异步复制,当检测到从服务器又好了,主服务器又会采用半同步复制。
配置半同步复制
mysql> show variables like "have_dynamic_loading";
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| have_dynamic_loading | YES |
+----------------------+-------+
1 row in set (0.02 sec)
mysql>
要想使用半同步复制,必须满足以下几个条件:
1. MySQL 5.5及以上版本
2. 变量have_dynamic_loading为YES
3. 异步复制已经存在
首先加载插件
因用户需执行INSTALL PLUGIN, SET GLOBAL, STOP SLAVE和START SLAVE操作,所以用户需有SUPER权限。
主:
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME ‘semisync_master.so‘;
从:
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME ‘semisync_slave.so‘;
然后在主和从上查看插件是否启用
mysql>show plugins;
| rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL |
====
启动半同步复制
在安装完插件后,半同步复制默认是关闭的,这时需设置参数来开启半同步
主:
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
从:
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
以上的启动方式是在命令行操作,也可写在配置文件中。
主:
plugin-load=rpl_semi_sync_master=semisync_master.so
rpl_semi_sync_master_enabled=1
从:
plugin-load=rpl_semi_sync_slave=semisync_slave.so
rpl_semi_sync_slave_enabled=1
查看半同步是否在运行
主:
mysql> show status like ‘Rpl_semi_sync_master_status‘;
+-----------------------------+-------+
| Variable_name | Value |
+-----------------------------+-------+
| Rpl_semi_sync_master_status | ON |
+-----------------------------+-------+
1 row in set (0.00 sec)
从:
mysql> show status like ‘Rpl_semi_sync_slave_status‘;
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.20 sec)
这两个变量常用来监控主从是否运行在半同步复制模式下。
至此,MySQL半同步复制搭建完毕~
====
修改完配置文件后,直接加入加载插件和启用半同步复制,重启刷新mysqld服务后,导致半同步复制没有起来
mysql> show status like ‘Rpl_semi_sync_slave_status‘;
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | OFF |
+----------------------------+-------+
1 row in set (0.02 sec)
mysql>
导致的原因是:主服务器上的二进制日志的文件和位置和从服务器的relay-log.info里的文件名和位置不一致导致
主服务器上的信息
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| MySQL-master-bin.000002 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
从服务器上的信息
mysql> show slave status\G;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 5
Current database: *** NONE ***
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.0.137
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: Mysql-master-bin.000001
Read_Master_Log_Pos: 1508
Relay_Log_File: mysql-slave-relay-bin.000010
Relay_Log_Pos: 4
Relay_Master_Log_File: Mysql-master-bin.000001
Slave_IO_Running: No 启动不了
Slave_SQL_Running: Yes
Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file‘ 错误提醒
[[email protected] mysql]# cat relay-log.info
7
./mysql-slave-relay-bin.000010
4
Mysql-master-bin.000001 ---》文件和位置不一致
1508
0
0
1
[[email protected] mysql]#
解决方法:
mysql> show master status; 查看主服务器上的日志文件和位置
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| MySQL-master-bin.000002 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
从服务器里设置日志文件名和位置
mysql> change master to master_log_file=‘MySQL-master-bin.000002‘,master_log_pos=154;
mysql> show status like ‘Rpl_semi_sync_slave_status‘; 查看
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON |
+----------------------------+-------+
1 row in set (0.02 sec)
mysql>
最后ok了
本文出自 “12745218” 博客,请务必保留此出处http://12755218.blog.51cto.com/12745218/1969388
以上是关于mysql5.7.19的半同步复制问题分享的主要内容,如果未能解决你的问题,请参考以下文章