如果你的生产库开启了半同步复制,那么对数据的一致性会要求较高,但在MySQL5.5/5.6里,会存在数据不一致的风险。有这么一个场景,客户端提交了一个事务,master把binlog发送给slave,在发送的期间,网络出现波动,此时Binlog Dump线程发送就会卡住,要等待slave把binlog写到本地的relay-log里,然后给master一个反馈,等待的时间以rpl_semi_sync_master_timeout参数为准,默认为10秒。在这等待的10秒钟里,在其他会话里,查看刚才的事务是可以看见的,此时一旦master发生宕机,由于binlog没有发送给slave,前端app切到slave查看,就会发现刚才已提交的事务不见了。

为了解决这种问题,MySQL5.7
改善了半同步复制这个缺陷。通过rpl_semi_sync_master_wait_point这个参数加以控制,默认是AFTER_SYNC,官方推荐用这个,它的工作原理是:master把binlog发送给slave,只有在slave把binlog写到本地的relay-log里,才提交到存储引擎层,然后把请求返回给客户端,客户端才可以看见刚才提交的事务。如果slave未保存到本地的relay-log里,客户端是看不见刚才的事务的,这样就不会造成上述那个场景发生。另一个值是AFTER_COMMIT,这个值是采用老式的MySQL5.5/5.6半同步复制工作。

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.
主库把每一个事务写到二进制日志并保存磁盘上,且发送给从库。主库在等待从库写到自己的relay-log里确认信息。在接到确认信息后,主数据库把事务写到存储引擎里并把相应结果反馈给客户端,客户端将在那时进行处理。

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. 
主库把每一个事务写到二进制日志并保存磁盘上,且发送给从库,并把事务写到存储引擎里。主库在等待从库写到自己的relay-log里确认信息。在接到确认信息后,主库把相应结果反馈给客户端,客户端将在那时进行处理。

The replication characteristics of these settings differ as follows:
这两个参数不同之处在于:

With AFTER_SYNC, all clients see the committed transaction at the same
time: After it has been acknowledged by the slave and committed to the
storage engine on the master.。Thus, all clients see the same data on the
master.
在设置为AFTER_SYNC参数,所有的客户端可以同时看到提交的数据:在得到从库写到自己的relay-log里的确认信息后,并把事务写到存储引擎里。这样,所有的客户端都可以在主库上看到同样的数据。

In the event of master failure, all transactions committed on the master
have been replicated to the slave (saved to its relay log). A crash of
the master and failover to the slave is lossless because the slave is up
to date.
主库报错,所有已经写到从库的事务都已经保存到了relay log里。主库的崩溃,HA切换到从库,不会带来任何损失,因为从库的relay-log的数据是最新的。

With AFTER_COMMIT, the client issuing the transaction gets a return
status only after the server commits to the storage engine and receives
slave acknowledgment. After the commit and before slave acknowledgment,
other clients can see the committed transaction before the committing
client.
在设置为AFTER_COMMIT 参数,发起事务的客户端仅在服务器向存储引擎写入数据并接受从库得到确认之后才返回状态。在写入数据后和得到从库确认之前,其他的客户端可以看到在这一事务。

If something goes wrong such that the slave does not process the
transaction, then in the event of a master crash and failover to the
slave, it is possible that such clients will see a loss of data relative
to what they saw on the master. 
如果出现了某种错误,比如说从库的sql_thread线程没有执行,那么主库崩溃和故障转移给从服务器的前提下,有可能这个客户端会丢失那些他们曾经在主库上看到的信息。

转自

http://blog.itpub.net/15498/viewspace-2140966/

MySQL 5.7半同步复制after sync和after commit详解【转】的更多相关文章

  1. mysql主从之半同步复制和lossless无损复制

    一 MySQL 的三种复制方式 1.1 简介 asynchronous 异步复制 fully synchronous 全同步复制 Semisynchronous 半同步复制 从MySQL5.5 开始, ...

  2. mysql配置为半同步复制

    mysql 半同步插件是由谷歌提供,具体位置/usr/local/mysql/lib/plugin/下,一个是 master用的 semisync_master.so,一个是 slave 用的 sem ...

  3. mysql基础之mysql主从架构半同步复制

    一.概念 1.异步复制(Asynchronous replication) MySQL默认的复制即是异步的,主库在执行完客户端提交的事务后会立即将结果返给给客户端,并不关心从库是否已经接收并处理,这样 ...

  4. Mysql主从复制、半同步复制、并行复制

    MySQL之间数据复制的基础是二进制日志文件(binary log file).一台MySQL数据库一旦启用二进制日志后,其作为master,它的数据库中所有操作都会以"事件"的方 ...

  5. MySQL主从复制、半同步复制和主主复制

    同步,异步,半同步复制的比较: 同步复制:Master提交事务,直到事务在所有的Slave都已提交,此时才会返回客户端,事务执行完毕.缺点:完成一个事务可能会有很大的延迟. 异步复制:当Slave准备 ...

  6. mysql配置完半同步复制之后报错[ERROR] The server quit without updating PID file

    修改配置,MySQL启动报:[ERROR] The server quit without updating PID file [root@localhost mysql]# /etc/init.d/ ...

  7. MySQL主从复制、半同步复制和主主复制概述

    http://www.cnblogs.com/zping/p/5275531.html

  8. Mysql半同步复制模式说明及配置示例 - 运维小结

    MySQL主从复制包括异步模式.半同步模式.GTID模式以及多源复制模式,默认是异步模式 (如之前详细介绍的mysql主从复制).所谓异步模式指的是MySQL 主服务器上I/O thread 线程将二 ...

  9. mysql半同步复制跟无损半同步区别

    mysql半同步复制跟无损半同步复制的区别: 无损复制其实就是对semi sync增加了rpl_semi_sync_master_wait_point参数,来控制半同步模式下主库在返回给会话事务成功之 ...

随机推荐

  1. aliyun阿里云alibabaMaven仓库地址——加速你的maven构建

    在maven的settings.xml 文件里 搜索  mirrors   ,把下面内容添加在其子节点内 <mirror> <id>nexus-aliyun</id> ...

  2. IBatisNet 缓存使用

    参考:http://www.cnblogs.com/xiaogangqq123/archive/2011/06/30/2094905.html <?xml version="1.0&q ...

  3. 【python】vscode python环境配置

    安装python插件:ext install python 配置flake8:pip install flake8 配置yapf:pip install yapf(在VScode中按Alt+Shift ...

  4. 单点登录(十四)-----实战-----cas5.0.x登录mongodb验证方式常规的四种加密的思考和分析

    我们在上一篇文章中已经讲解了cas4.2.X登录启用mongodb验证方式 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 但是密码是明文存储的,也就是 ...

  5. java 操作命令行

    目的:用java进行调用ab压测 window: cmd ab.exe linux: sh  ab.sh 命令: abs -n 500 -c 10 https://www.baidu.com/ > ...

  6. Java中JDK,JRE和JVM之间的关系

    初学JAVA很容易被其中的很多概念弄的傻傻分不清楚,首先从概念上理解一下吧,JDK(Java Development Kit)简单理解就是Java开发工具包,JRE(Java Runtime Envi ...

  7. Jenkins(四)---Jenkins添加密钥对

    一.添加密钥 1.添加git用户和git密码对 ,用于git客户端从gitlab上拉取代码到本地 /** lihaibo 文章内容都是根据自己工作情况实践得出. *版权声明:本博客欢迎转发,但请保留原 ...

  8. 音视频处理之FFmpeg+SDL视频播放器20180409

    一.FFmpeg视频解码器 1.视频解码知识 1).纯净的视频解码流程 压缩编码数据->像素数据. 例如解码H.264,就是“H.264码流->YUV”. 2).一般的视频解码流程 视频码 ...

  9. python学习笔记__反射

    反射 # 通过字符串的形式对对象中的成员进行操作(获取/查找/添加/删除). 操作的内置函数: 1.获取   getattr(object, name) # 去对象object中获取name的内容 c ...

  10. kaggle竞赛

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...