在以下部分中,我们提供了关于 MySQL 复制的常见问题的解答。In the following section, we provide answers to questions that are most frequently asked about MySQL Replication.
- A.14.1. 副本必须始终连接到源吗?A.14.1. Must the replica be connected to the source all the time?
- A.14.2. 我必须在源和副本上启用网络才能启用复制吗? A.14.2. Must I enable networking on my source and replica to enable replication?
- A.14.3. 我如何知道副本相比于源延迟了多久?换句话说,我如何知道副本复制的最后一条语句的日期? A.14.3. How do I know how late a replica is compared to the source? In other words, how do I know the date of the last statement replicated by the replica?
- A.14.4. 我如何强制源阻止更新,直到副本赶上? A.14.4. How do I force the source to block updates until the replica catches up?
- A.14.5. 设置双向复制时,我需要注意哪些问题? A.14.5. What issues should I be aware of when setting up two-way replication?
- A.14.6. 我如何使用复制来提高系统性能? A.14.6. How can I use replication to improve performance of my system?
- A.14.7. 我应该如何为自己的应用程序准备客户端代码以使用性能增强型复制? A.14.7. What should I do to prepare client code in my own applications to use performance-enhancing replication?
- A.14.8. 何时以及在多大程度上 MySQL 复制可以提升我的系统性能? A.14.8. When and how much can MySQL replication improve the performance of my system?
- A.14.9. 我如何使用复制来提供冗余或高可用性? A.14.9. How can I use replication to provide redundancy or high availability?
- A.14.10. 我如何判断复制源服务器是使用基于语句还是基于行的二进制日志格式? A.14.10. How do I tell whether a replication source server is using statement-based or row-based binary logging format?
- A.14.11. 我如何告诉从服务器使用基于行的复制? A.14.11. How do I tell a replica to use row-based replication?
- A.14.12. 我如何防止 GRANT 和 REVOKE 语句复制到副本机器? A.14.12. How do I prevent GRANT and REVOKE statements from replicating to replica machines?
- A.14.13. 复制是否在混合操作系统上工作(例如,源运行在 Linux 上,而副本运行在 macOS 和 Windows 上)? A.14.13. Does replication work on mixed operating systems (for example, the source runs on Linux while replicas run on macOS and Windows)?
- A.14.14. 复制是否在混合硬件架构上工作(例如,源运行在 64 位机器上,而副本运行在 32 位机器上)? A.14.14. Does replication work on mixed hardware architectures (for example, the source runs on a 64-bit machine while replicas run on 32-bit machines)?
A.14.1. | 副本必须一直连接到源吗?Must the replica be connected to the source all the time? |
不,并不是这样的。副本可能会下线或者断开连接数小时甚至数天,然后再次连接并追上更新。例如,你可以在拨号连接上建立源/副本关系,在这种情况下,连接只是零星短暂可用。这意味着,在任何给定时间,除非采取特殊措施,否则副本不保证与源保持同步。No, it does not. The replica can go down or stay disconnected for hours or even days, and then reconnect and catch up on updates. For example, you can set up a source/replica relationship over a dial-up link where the link is up only sporadically and for short periods of time. The implication of this is that, at any given time, the replica is not guaranteed to be in synchrony with the source unless you take some special measures. 为了确保已断开连接的副本能够追赶更新,你不能从源中删除包含尚未复制到副本的数据信息的二进制日志文件。只有当副本能够从上次读取事件的位置继续读取二进制日志时,异步复制才能正常工作。To ensure that catchup can occur for a replica that has been disconnected, you must not remove binary log files from the source that contain information that has not yet been replicated to the replicas. Asynchronous replication can work only if the replica is able to continue reading the binary log from the point where it last read events. | |
A.14.2. | 我必须在源和副本上启用网络才能启用复制吗?Must I enable networking on my source and replica to enable replication? |
是的,源服务器和副本服务器都必须启用网络功能。如果未启用网络功能,副本将无法连接到源服务器并传输二进制日志。请检查配置文件中两个服务器的 | |
A.14.3. | 我如何知道副本相比源服务器滞后了多长时间?换句话说,我如何知道副本复制的最后一条语句的日期?How do I know how late a replica is compared to the source? In other words, how do I know the date of the last statement replicated by the replica? |
检查 当复制 SQL 线程执行从源读取的事件时,它会将自身的时间修改为该事件的时间戳。(这就是为什么 | |
A.14.4. | 我如何强制源端阻塞更新,直到副本追上?How do I force the source to block updates until the replica catches up? |
使用以下步骤:Use the following procedure:
| |
A.14.5. | 在设置双向复制时,我应该注意哪些问题?What issues should I be aware of when setting up two-way replication? |
MySQL 复制目前不支持源服务器和副本之间的任何锁定协议来保证分布式(跨服务器)更新的原子性。换句话说,客户端 A 有可能对协同源 1 进行更新,同时在该更新传播到协同源 2 之前,客户端 B 可能会对协同源 2 进行更新,从而使客户端 A 的更新在协同源 2 上的效果与在协同源 1 上的效果不同。因此,当客户端 A 的更新传到协同源 2 时,即使协同源 2 的所有更新也已传播,它产生的表也可能与协同源 1 上的表不同。这意味着,除非你确定你的更新可以安全地以任何顺序发生,或者你在客户端代码中处理了顺序错乱的更新,否则你不应该将两台服务器连接成双向复制关系。MySQL replication currently does not support any locking protocol between source and replica to guarantee the atomicity of a distributed (cross-server) update. In other words, it is possible for client A to make an update to co-source 1, and in the meantime, before it propagates to co-source 2, client B could make an update to co-source 2 that makes the update of client A work differently than it did on co-source 1. Thus, when the update of client A makes it to co-source 2, it produces tables that are different from what you have on co-source 1, even after all the updates from co-source 2 have also propagated. This means that you should not chain two servers together in a two-way replication relationship unless you are sure that your updates can safely happen in any order, or unless you take care of mis-ordered updates somehow in the client code. 你还应该意识到,就更新而言,双向复制实际上并不会大幅提升性能(如果有的话)。每个服务器必须执行相同数量的更新,就像单台服务器执行一样。唯一的区别是,由于源自另一台服务器的更新在一个复制线程中顺序执行,因此锁争用会稍微减少。即便如此,这种好处可能也会被网络延迟抵消。You should also realize that two-way replication actually does not improve performance very much (if at all) as far as updates are concerned. Each server must do the same number of updates, just as you would have a single server do. The only difference is that there is a little less lock contention because the updates originating on another server are serialized in one replication thread. Even this benefit might be offset by network delays. | |
A.14.6. | 我如何使用复制来提升系统性能?How can I use replication to improve performance of my system? |
将一台服务器设置为源服务器,并将所有写操作定向到该服务器。然后根据预算和机架空间配置尽可能多的副本,并在源服务器和副本之间分配读取操作。你还可以使用 | |
A.14.7. | 我应该如何在自己的应用程序中准备客户端代码以使用性能增强型复制?What should I do to prepare client code in my own applications to use performance-enhancing replication? |
请参阅作为扩展解决方案使用复制的指南,第 19.4.5 节,“使用复制进行扩展”。See the guide to using replication as a scale-out solution, Section 19.4.5, “Using Replication for Scale-Out”. | |
A.14.8. | MySQL 复制在什么时候以及能在多大程度上提高我的系统性能?When and how much can MySQL replication improve the performance of my system? |
MySQL 复制对于处理频繁读取和不频繁写入的系统最为有利。理论上,通过使用单源/多副本配置,可以通过增加更多副本来扩展系统,直到网络带宽耗尽,或者更新负载增加到源无法处理的程度。MySQL replication is most beneficial for a system that processes frequent reads and infrequent writes. In theory, by using a single-source/multiple-replica setup, you can scale the system by adding more replicas until you either run out of network bandwidth, or your update load grows to the point that the source cannot handle it. 要确定在额外好处开始趋于平稳之前您可以使用多少副本,以及您可以提高网站性能多少,您必须了解您的查询模式,并通过基准测试经验性地确定典型源和典型副本上读写吞吐量之间的关系。这里的示例展示了一个相当简化的计算,用于说明通过复制在假设系统中可以获得的效果。令 假设系统负载由 10% 写入和 90% 读取组成,并且我们通过基准测试确定
9 *
最后的方程表示,对于 这个分析得出以下结论:This analysis yields the following conclusions:
这些计算假设网络带宽无限,并忽略了其他一些在您的系统上可能很重要的因素。在许多情况下,您可能无法执行类似上述计算那样准确地预测在增加
| |
A.14.9. | 我如何使用复制来提供冗余或高可用性?How can I use replication to provide redundancy or high availability? |
如何实现冗余完全取决于你的应用程序和具体情况。高可用性解决方案(带自动故障切换)需要主动监控,并且需要自定义脚本或第三方工具来提供从原始 MySQL 服务器到副本的故障切换支持。How you implement redundancy is entirely dependent on your application and circumstances. High-availability solutions (with automatic failover) require active monitoring and either custom scripts or third party tools to provide the failover support from the original MySQL server to the replica. 若手动处理此过程,你应该能够通过修改应用程序以连接到新服务器,或通过调整 MySQL 服务器的 DNS 从故障服务器切换到新服务器,从而从故障源切换到预配置的副本。To handle the process manually, you should be able to switch from a failed source to a pre-configured replica by altering your application to talk to the new server or by adjusting the DNS for the MySQL server from the failed server to the new server. 有关更多信息和一些示例解决方案,请参见 第 19.4.8 节,“故障切换期间切换源”。For more information and some example solutions, see Section 19.4.8, “Switching Sources During Failover”. | |
A.14.10. | 我如何判断复制源服务器使用的是基于语句还是基于行的二进制日志格式?How do I tell whether a replication source server is using statement-based or row-based binary logging format? |
检查 mysql> 显示的值始终是 | |
A.14.11. | 如何告诉副本使用基于行的复制?How do I tell a replica to use row-based replication? |
副本会自动知道使用哪种格式。Replicas automatically know which format to use. | |
A.14.12. | 如何防止 |
启动服务器时使用 | |
A.14.13. | 复制能在混合操作系统上工作吗(例如,源运行在 Linux,而从机运行在 macOS 和 Windows 上)?Does replication work on mixed operating systems (for example, the source runs on Linux while replicas run on macOS and Windows)? |
可以。Yes. | |
A.14.14. | 复制能在混合硬件架构上工作吗(例如,源运行在 64 位机器上,而从机运行在 32 位机器上)?Does replication work on mixed hardware architectures (for example, the source runs on a 64-bit machine while replicas run on 32-bit machines)? |
可以。Yes. |