如果你的生产库开启了半同步复制,那么对数据的一致性会要求较高,但在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. Cryptography Reloaded UVALive - 4353(BigInteger)

    写写式子就出来了方程.. 然后解方程..不过数很大..用Java就好啦.. 就不贴呃的代码了...贴别人的..https://blog.csdn.net/qq_15714857/article/det ...

  2. 洛谷 P2022 有趣的数 解题报告

    P2022 有趣的数 题目描述 让我们来考虑1到N的正整数集合.让我们把集合中的元素按照字典序排列,例如当N=11时,其顺序应该为:1,10,11,2,3,4,5,6,7,8,9. 定义K在N个数中的 ...

  3. 解题:ZJOI 2013 K大数查询

    题面 树套树,权值线段树套序列线段树,每次在在权值线段树上的每棵子树上做区间加,查询的时候左右子树二分 本来想两个都动态开点的,这样能体现树套树在线的优越性.但是常数太大惹,所以外层直接固定建树了QA ...

  4. bzoj 3611: [Heoi2014]大工程 && bzoj 2286: [Sdoi2011消耗战

    放波建虚树的模板. 大概是用一个栈维护根节点到当前关键点的一条链,把其他深度大于lca的都弹出去. 每次做完记得复原. 还有sort的时候一定要加cmp!!! bzoj 3611 #include&l ...

  5. 【数学】【P5150】 生日礼物

    Description 给定 \(n\),求 \[\sum_{i}~\sum_j~[lcm(i,j)~=~n]\] input 一行一个整数代表 \(n\) Output 一行一个整数代表答案 Hin ...

  6. js字符串替换(时间转换)

    转: js中字符串全部替换 废话不多说,直接发结果 在js中字符串全部替换可以用以下方法: str.replace(/需要替换的字符串/g,"新字符串") 比如: "yy ...

  7. BP神经网络人口预测程序(matlab实现)

    自己测试人口预测的matlab实现: x=[54167    55196    56300    57482    58796    60266    61465    62828    64653  ...

  8. 题解【bzoj2733 [HNOI2012]永无乡】

    Descriprition 两种操作 把两个集合并起来 求一个集合中的第 \(k\) 大(的编号) \(n \leq 10^5\) Solution 平衡树的板子题之一 维护两个点连不连通直接并查集 ...

  9. C++下实现同接口下多个类作为参数的调用和传参

    /* 实现同接口下不同类的对象的转移 定义类的接口 定义多个继承该接口的类 定义管理类,把接口当作类型, 传入该接口下各种类的对象,进行操作 */ #include<iostream> # ...

  10. ural 2029 Towers of Hanoi Strike Back (数学找规律)

    ural 2029 Towers of Hanoi Strike Back 链接:http://acm.timus.ru/problem.aspx?space=1&num=2029 题意:汉诺 ...