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

本文介绍了MySQL5.7及AliSQL5.6中半同步复制的改进,通过新增的rpl_semi_sync_master_wait_point参数,可在AFTER_SYNC模式下确保主库提交的事务已同步至从库,有效避免数据丢失风险。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 半同步是通过插件的形式支持

<think>好的,用户现在问的是关于MySQL主从复制中半同步复制的配置问题,具体是要知道如何修改主库和从库的配置文件。用户之前已经询问过启动主库容器时忘记密码的问题,现在转向配置问题,说明他们可能在搭建MySQL集群,特别是在配置半同步复制。 首先,我需要确认用户对MySQL配置文件的了解程度。他们可能知道Docker的使用,但不太清楚MySQL配置的具体位置和方法。用户提供的配置片段包括主库和从库的插件加载和参数设置,需要指导他们如何正确地将这些配置应用到Docker容器中。 接下来,我需要考虑用户的实际操作环境。他们使用的是Docker,并且之前已经通过-v参数挂载了宿主机的目录到容器的/etc/mysql/conf.d。这意味着用户需要在宿主机的挂载目录下创建配置文件,如my.cnf或conf.d目录中的文件。需要明确告诉用户配置文件的路径和格式。 然后,用户可能不了解半同步复制的插件加载方式。在MySQL 8.0中,可能需要使用plugin_load_add来动态加载插件,或者在配置文件中使用不同的语法。需要注意版本差异,比如MySQL 8.0是否默认启用了这些插件,或者是否需要显式加载。 另外,用户可能在配置主库和从库时混淆了配置的位置。需要区分主库和从库各自的配置文件,特别是在Docker环境中,每个容器可能有自己的挂载目录,例如主库的配置在/data/mysql-master/conf,从库在/data/mysql-slave/conf。需要提醒用户根据不同的容器挂载点进行配置。 还需要考虑配置的生效方式。修改配置文件后,必须重启MySQL服务才能使配置生效。在Docker中,可能需要重启容器,或者进入容器内部重启MySQL服务。需要指导用户正确的重启步骤,并验证配置是否生效。 此外,用户可能遇到的问题包括配置文件权限问题、路径错误、配置语法错误等,可能需要提醒他们检查这些方面。例如,确保配置文件有正确的权限,避免容器无法读取;检查配置文件中的参数是否正确,特别是半同步相关的参数名称是否正确。 最后,应该给出验证配置是否成功的方法,比如通过SQL命令检查变量状态,确保半同步插件已加载,相关参数已启用。这可以帮助用户确认他们的配置是否正确应用,避免后续出现主从同步的问题。 总结来说,用户需要明确的步骤指导,包括配置文件的位置、内容、主从库的区别、重启服务的方法以及验证步骤。同时,需要预见到他们可能遇到的常见问题,并给出解决方案,确保他们能够顺利完成配置。</think>--- ### **配置文件的存放位置与修改方法** 根据您的 `docker run` 命令中 `-v /data/mysql-master/conf:/etc/mysql/conf.d` 的挂载配置,**需在宿主机 `/data/mysql-master/conf` 目录下创建配置文件**。以下是具体操作步骤: --- ### **步骤 1:为主库/从库创建配置文件** #### **主库配置**(`mysql-master`) 1. **创建配置文件** 在宿主机挂载目录中新建文件(例如 `semi-sync-master.cnf`): ```bash # 进入主库配置目录 mkdir -p /data/mysql-master/conf vim /data/mysql-master/conf/semi-sync-master.cnf ``` 2. **写入配置内容** ```ini [mysqld] # 半同步主库配置 plugin_load_add = 'rpl_semi_sync_master.so' rpl_semi_sync_master_enabled = 1 ``` #### **从库配置**(假设容器名为 `mysql-slave`) 1. **创建配置文件** 若从库挂载目录为 `/data/mysql-slave/conf`: ```bash mkdir -p /data/mysql-slave/conf vim /data/mysql-slave/conf/semi-sync-slave.cnf ``` 2. **写入配置内容** ```ini [mysqld] # 半同步从库配置 plugin_load_add = 'rpl_semi_sync_slave.so' rpl_semi_sync_slave_enabled = 1 ``` --- ### **步骤 2:重启MySQL容器使配置生效** #### **主库重启** ```bash docker restart mysql-master ``` #### **从库重启** ```bash # 假设从库容器名为 mysql-slave docker restart mysql-slave ``` --- ### **步骤 3:验证配置是否生效** #### **在主库中验证** ```bash # 进入主库容器 docker exec -it mysql-master mysql -uroot -p -- 检查半同步主库状态 SHOW VARIABLES LIKE 'rpl_semi_sync%'; -- 预期输出应包含: -- rpl_semi_sync_master_enabled = ON -- rpl_semi_sync_master_timeout = 10000(默认值) ``` #### **在从库中验证** ```bash # 进入从库容器 docker exec -it mysql-slave mysql -uroot -p -- 检查半同步从库状态 SHOW VARIABLES LIKE 'rpl_semi_sync%'; -- 预期输出应包含: -- rpl_semi_sync_slave_enabled = ON ``` --- ### **关键注意事项** 1. **配置文件命名规则** - MySQL会自动加载 `/etc/mysql/conf.d` 目录下以 `.cnf` 结尾的文件。 - 建议文件名明确用途(如 `semi-sync-master.cnf`),避免冲突。 2. **MySQL 8.0的特殊性** - 若使用 MySQL 8.0,`rpl_semi_sync_master` 参数已更名为 `semisync_master`,但**镜像可能已适配别名**,建议通过 `SHOW VARIABLES` 实际验证。 3. **容器启动顺序** - 先启动主库并完成配置,再启动从库,避免从库因找不到主库而连接失败。 4. **权限问题** - 确保宿主机配置文件权限允许容器读取: ```bash chmod 644 /data/mysql-master/conf/*.cnf ``` --- ### **配置未生效的排查方法** 1. **检查容器日志** ```bash docker logs mysql-master | grep semi-sync ``` - 若出现 `Unknown variable` 错误,说明参数名称或格式错误。 2. **确认文件挂载** ```bash # 进入容器查看配置文件是否加载 docker exec -it mysql-master ls /etc/mysql/conf.d ``` --- 通过以上步骤,您可以在 Docker 环境中正确配置 MySQL 主从半同步复制。若需动态调整参数(如超时时间),可直接修改配置文件并重启容器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值