MySQL5.7 & AliSQL5.6 半同步复制 rpl_semi_sync_master_wait_point参数

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: MySQL5.5 开始支持半同步复制,但会有数据丢失的风险。MySQL5.7 中进行了优化,通过 rpl_semi_sync_master_wait_point 参数来实现。

MySQL5.5 开始支持半同步复制,但实现的不完善,slave ack 是在 master
commit 之后。如果 master 宕机,会有数据丢失的风险。如下图可以看出,主库在 commit 之后宕机,如果从库还未收到 binlog ,主从切换后可能会出现数据丢失的现象。

image

MySQL5.7 & AliSQL5.6 增加了 rpl_semi_sync_master_wait_point 参数来修复这个问题。先来看看这个参数是做什么用的。官方文档解释如下:

The rpl_semi_sync_master_wait_point system variable controls the point at which a semisynchronous replication master waits for slave acknowledgment of transaction receipt before returning a status to the client that committed the transaction.

简单来讲就是控制 master 在哪个环节接收 slave ack,master 接收到 ack 后返回状态给客户端。
此参数一共有两个选项 AFTER_SYNC(default) & AFTER_COMMIT。

我们先来看看旧的逻辑(AFTER_COMMIT):

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.

整个流程: master write binlog -> slave sync binlog -> master commit -> salve ack -> master return result。
这样会有什么问题呢?

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.

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.

  1. 会出现脏读的情况,也就是在事务提交之后,slave 确认之前,客户端A还没有获得结果返回,但是客户端B能够读取A提交的结果;
  2. 可能会出现我们上文提到的数据丢失的问题;

这些问题对于数据一致性要求高的场景是无法满足的,所以 MySQL5.7 提供了 AFTER_SYNC 模式来避免这些问题。

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.

整个流程: master write binlog -> slave sync binlog -> salve ack -> master commit -> master return result。这样就保证 master commit 的事务已经同步到 slave 了,防止数据丢失。

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.

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.

  1. 保证所有客户端查询到的结果都是相同的;
  2. 保证 slave 不会丢数据;
  3. master 宕机虽然不会导致数据丢失,但有一种情况可能会出现,那就是 salve 的数据比 master 多,大家可以回顾下流程,想想为什么,该如何处理。

AliSQL5.6 半同步复制已经实现此功能,并且是整合到主代码里原生支持的,而 MySQL 半同步是通过插件的形式支持

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
SQL 缓存 关系型数据库
Mysql第十四天,Mysql并发参数调整
Mysql第十四天,Mysql并发参数调整
31 0
|
4月前
|
SQL 关系型数据库 MySQL
在云数据仓库AnalyticDB MySQL版中,有几个参数可能影响SELECT查询的执行及其稳定性
在云数据仓库AnalyticDB MySQL版中,有几个参数可能影响SELECT查询的执行及其稳定性【1月更文挑战第16天】【1月更文挑战第80篇】
295 4
|
4月前
|
存储 关系型数据库 MySQL
RDS有哪些参数?都有什么作用?
RDS有哪些参数?都有什么作用?
48 0
|
5月前
|
缓存 关系型数据库 MySQL
MySQL Binlog--事务日志和BINLOG落盘参数对磁盘IO的影响
MySQL Binlog--事务日志和BINLOG落盘参数对磁盘IO的影响
54 0
|
5月前
|
SQL 关系型数据库 MySQL
使用CTAS 把mysql 表同步数据 到hologres ,Flink有什么参数可以使hologres 的字段都小写吗?
使用CTAS 把mysql 表同步数据 到hologres ,Flink有什么参数可以使hologres 的字段都小写吗?
283 0
|
1月前
|
缓存 关系型数据库 MySQL
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
|
2月前
|
SQL 关系型数据库 MySQL
【Mysql】MYSQL参数max_allowed_packet 介绍
【Mysql】MYSQL参数max_allowed_packet 介绍
84 0
|
5月前
|
缓存 关系型数据库 MySQL
MySQL调优之服务器参数优化实践
MySQL调优之服务器参数优化实践
353 0
|
2月前
|
存储 SQL 关系型数据库
MySQL的参数optimizer_switch
`optimizer_switch`是MySQL系统变量,用于控制查询优化器行为。它由键值对组成,如`index_merge=on/off`,用于开启或关闭特定优化功能。要查看当前设置,运行`SHOW VARIABLES LIKE 'optimizer_switch';`,修改则用`SET`命令,如`SET optimizer_switch='index_merge=off';`。
|
2月前
|
SQL 存储 关系型数据库