8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

19.4.10 半同步复制19.4.10 Semisynchronous Replication

除了内置的异步复制外,MySQL 8.4 支持通过插件实现的半同步复制接口。本节讨论什么是半同步复制以及其工作原理。接下来的各节将介绍半同步复制的管理接口,以及如何安装、配置和监控它。In addition to the built-in asynchronous replication, MySQL 8.4 supports an interface to semisynchronous replication that is implemented by plugins. This section discusses what semisynchronous replication is and how it works. The following sections cover the administrative interface to semisynchronous replication and how to install, configure, and monitor it.

MySQL 默认情况下的复制是异步的。源服务器将事件写入其二进制日志,副本在准备好时请求这些事件。源服务器不知道副本是否以及何时已检索并处理了这些事务,并且不能保证任何事件会到达任何副本。在异步复制中,如果源服务器崩溃,它已提交的事务可能尚未传输到任何副本。在这种情况下从源服务器切换到副本可能会导致切换到相对于源服务器缺少事务的服务器。MySQL replication by default is asynchronous. The source writes events to its binary log and replicas request them when they are ready. The source does not know whether or when a replica has retrieved and processed the transactions, and there is no guarantee that any event ever reaches any replica. With asynchronous replication, if the source crashes, transactions that it has committed might not have been transmitted to any replica. Failover from source to replica in this case might result in failover to a server that is missing transactions relative to the source.

在完全同步复制的情况下,当源提交一个事务时,所有副本在源返回执行事务的会话之前也已经提交了该事务。完全同步复制意味着随时可以从源切换到任何副本。完全同步复制的缺点是完成事务可能会有很大的延迟。With fully synchronous replication, when a source commits a transaction, all replicas have also committed the transaction before the source returns to the session that performed the transaction. Fully synchronous replication means failover from the source to any replica is possible at any time. The drawback of fully synchronous replication is that there might be a lot of delay to complete a transaction.

半同步复制介于异步复制和完全同步复制之间。源端会等待至少一个副本接收到并记录事件(所需副本数量是可配置的),然后再提交事务。源端不会等待所有副本确认接收,并且只需要副本的确认,而不要求事件在副本端已经完全执行和提交。因此,半同步复制保证了如果源端崩溃,它已提交的所有事务都至少已传输到一个副本。Semisynchronous replication falls between asynchronous and fully synchronous replication. The source waits until at least one replica has received and logged the events (the required number of replicas is configurable), and then commits the transaction. The source does not wait for all replicas to acknowledge receipt, and it requires only an acknowledgement from the replicas, not that the events have been fully executed and committed on the replica side. Semisynchronous replication therefore guarantees that if the source crashes, all the transactions that it has committed have been transmitted to at least one replica.

与异步复制相比,半同步复制提供了更好的数据完整性,因为当提交成功返回时,可以确认数据至少存在于两个位置。在半同步源从所需数量的副本收到确认之前,事务会被挂起并且不会提交。Compared to asynchronous replication, semisynchronous replication provides improved data integrity, because when a commit returns successfully, it is known that the data exists in at least two places. Until a semisynchronous source receives acknowledgment from the required number of replicas, the transaction is on hold and not committed.

与完全同步复制相比,半同步复制更快,因为它可以配置为在数据完整性(确认收到事务的副本数量)和提交速度之间进行平衡,而提交速度较慢是由于需要等待副本的原因。Compared to fully synchronous replication, semisynchronous replication is faster, because it can be configured to balance your requirements for data integrity (the number of replicas acknowledging receipt of the transaction) with the speed of commits, which are slower due to the need to wait for replicas.

Important

在半同步复制中,如果源服务器崩溃并且进行了到副本的故障转移,则不应将失败的源服务器重新用作复制源,应将其丢弃。它可能有尚未被任何副本确认的事务,因此在故障转移前未提交。With semisynchronous replication, if the source crashes and a failover to a replica is carried out, the failed source should not be reused as the replication source, and should be discarded. It could have transactions that were not acknowledged by any replica, which were therefore not committed before the failover.

如果您的目标是实现一个容错的复制拓扑,其中所有服务器以相同顺序接收相同的事务,并且崩溃的服务器可以重新加入组并自动更新到最新状态,则可以使用组复制来实现。有关信息,请参见 第 20 章,组复制。If your goal is to implement a fault-tolerant replication topology where all the servers receive the same transactions in the same order, and a server that crashes can rejoin the group and be brought up to date automatically, you can use Group Replication to achieve this. For information, see Chapter 20, Group Replication.

与异步复制相比,半同步复制的性能影响是为了提高数据完整性的权衡。慢下来的程度至少是将提交发送到副本并等待副本确认收到的 TCP/IP 往返时间。这意味着半同步复制最适合通过快速网络通信的近距离服务器,而远程服务器通过慢速网络通信时效果最差。半同步复制还通过限制二进制日志事件从源发送到副本的速度,对繁忙会话施加速率限制。当某个用户过于繁忙时,这会减慢速度,这在某些部署情况下可能是有用的。The performance impact of semisynchronous replication compared to asynchronous replication is the tradeoff for increased data integrity. The amount of slowdown is at least the TCP/IP roundtrip time to send the commit to the replica and wait for the acknowledgment of receipt by the replica. This means that semisynchronous replication works best for close servers communicating over fast networks, and worst for distant servers communicating over slow networks. Semisynchronous replication also places a rate limit on busy sessions by constraining the speed at which binary log events can be sent from source to replica. When one user is too busy, this slows it down, which can be useful in some deployment situations.

源和其副本之间的半同步复制操作如下:Semisynchronous replication between a source and its replicas operates as follows:

  • 副本在连接到源时表明其是否支持半同步。A replica indicates whether it is semisynchronous-capable when it connects to the source.

  • 如果在源端启用了半同步复制,且至少存在一个半同步副本,则在源端执行事务提交的线程会阻塞,并等待至少有一个半同步副本确认已收到该事务的所有事件,或者直到发生超时。If semisynchronous replication is enabled on the source side and there is at least one semisynchronous replica, a thread that performs a transaction commit on the source blocks and waits until at least one semisynchronous replica acknowledges that it has received all events for the transaction, or until a timeout occurs.

  • 副本在将事务的事件写入其中继日志并刷新到磁盘后,才会确认收到事务的事件。The replica acknowledges receipt of a transaction's events only after the events have been written to its relay log and flushed to disk.

  • 如果发生超时而没有任何副本确认事务,源端将回退到异步复制。当至少有一个半同步副本赶上时,源端会恢复半同步复制。If a timeout occurs without any replica having acknowledged the transaction, the source reverts to asynchronous replication. When at least one semisynchronous replica catches up, the source returns to semisynchronous replication.

  • 半同步复制必须在源端和副本端都启用。如果在源端禁用了半同步复制,或者源端启用但副本端没有启用,则源端使用异步复制。Semisynchronous replication must be enabled on both the source and replica sides. If semisynchronous replication is disabled on the source, or enabled on the source but on no replicas, the source uses asynchronous replication.

当源正在阻塞(等待副本的确认)时,它不会返回到执行该事务的会话。当阻塞结束时,源会返回到会话,然后会话可以继续执行其他语句。此时,事务已经在源端提交,并且其事件的接收已被至少一个副本确认。源在返回会话之前每个事务必须接收的副本确认数量是可配置的,默认值为一次确认(参见 第 19.4.10.2 节,“配置半同步复制”)。While the source is blocking (waiting for acknowledgment from a replica), it does not return to the session that performed the transaction. When the block ends, the source returns to the session, which then can proceed to execute other statements. At this point, the transaction has committed on the source side, and receipt of its events has been acknowledged by at least one replica. The number of replica acknowledgments the source must receive per transaction before returning to the session is configurable, and defaults to one acknowledgement (see Section 19.4.10.2, “Configuring Semisynchronous Replication”).

在回滚写入二进制日志之后也会发生阻塞,这种情况发生在修改非事务表的事务被回滚时。即使对于事务表回滚后的事务没有任何效果,它仍然会被记录在日志中,因为对非事务表的修改无法回滚,必须发送到副本。Blocking also occurs after rollbacks that are written to the binary log, which occurs when a transaction that modifies nontransactional tables is rolled back. The rolled-back transaction is logged even though it has no effect for transactional tables because the modifications to the nontransactional tables cannot be rolled back and must be sent to replicas.

对于不在事务上下文中发生的语句(也就是说,当没有使用 START TRANSACTION 或 SET autocommit = 0 启动事务时),自动提交处于启用状态,并且每条语句都隐式提交。在半同步复制中,源服务器会对每条这样的语句进行阻塞,就像对显式事务提交一样。For statements that do not occur in transactional context (that is, when no transaction has been started with START TRANSACTION or SET autocommit = 0), autocommit is enabled and each statement commits implicitly. With semisynchronous replication, the source blocks for each such statement, just as it does for explicit transaction commits.

默认情况下,源在将二进制日志同步到磁盘后,但在将事务提交到存储引擎之前,会等待副本对事务收据的确认。作为替代,您可以配置源,使源在将事务提交到存储引擎之后等待副本确认,通过 rpl_semi_sync_source_wait_point 系统变量。此设置会影响复制特性以及客户端在源上可以看到的数据。有关更多信息,请参见 第 19.4.10.2 节,“配置半同步复制”。By default, the source waits for replica acknowledgment of the transaction receipt after syncing the binary log to disk, but before committing the transaction to the storage engine. As an alternative, you can configure the source so that the source waits for replica acknowledgment after committing the transaction to the storage engine, using the rpl_semi_sync_source_wait_point system variable. This setting affects the replication characteristics and the data that clients can see on the source. For more information, see Section 19.4.10.2, “Configuring Semisynchronous Replication”.

你可以通过启用系统变量 replication_sender_observe_commit_only 来提高半同步复制的性能,该变量限制回调;以及 replication_optimize_for_static_plugin_config,它增加了共享锁并避免不必要的锁获取。当副本数量增加时,这些设置有帮助,因为锁争用可能会降低性能。半同步复制的源服务器也可以通过启用这些系统变量获得性能提升,因为它们使用与副本相同的锁机制。You can improve the performance of semisynchronous replication by enabling the system variables replication_sender_observe_commit_only, which limits callbacks, and replication_optimize_for_static_plugin_config, which adds shared locks and avoids unnecessary lock acquisitions. These settings help as the number of replicas increases, because contention for locks can slow down performance. Semisynchronous replication source servers can also get performance benefits from enabling these system variables, because they use the same locking mechanisms as the replicas.