- 19.1 配置复制19.1 Configuring Replication
- 19.1.1 基于二进制日志文件位置的复制配置概述19.1.1 Binary Log File Position Based Replication Configuration Overview
- 19.1.2 设置基于二进制日志文件位置的复制19.1.2 Setting Up Binary Log File Position Based Replication
- 19.1.3 使用全局事务标识符进行复制19.1.3 Replication with Global Transaction Identifiers
- 19.1.4 在线服务器上更改 GTID 模式19.1.4 Changing GTID Mode on Online Servers
- 19.1.5 MySQL 多源复制19.1.5 MySQL Multi-Source Replication
- 19.1.6 复制和二进制日志选项与变量19.1.6 Replication and Binary Logging Options and Variables
- 19.1.7 常见的复制管理任务19.1.7 Common Replication Administration Tasks
- 19.2 复制实现19.2 Replication Implementation
- 19.3 复制安全性19.3 Replication Security
- 19.4 复制解决方案19.4 Replication Solutions
- 19.4.1 使用复制进行备份19.4.1 Using Replication for Backups
- 19.4.2 处理副本意外停止19.4.2 Handling an Unexpected Halt of a Replica
- 19.4.3 行级复制监控19.4.3 Monitoring Row-based Replication
- 19.4.4 在不同的源和副本存储引擎中使用复制19.4.4 Using Replication with Different Source and Replica Storage Engines
- 19.4.5 使用复制进行横向扩展19.4.5 Using Replication for Scale-Out
- 19.4.6 将不同的数据库复制到不同的副本19.4.6 Replicating Different Databases to Different Replicas
- 19.4.7 提高复制性能19.4.7 Improving Replication Performance
- 19.4.8 故障切换期间切换源19.4.8 Switching Sources During Failover
- 19.4.9 使用异步连接故障转移切换源和副本19.4.9 Switching Sources and Replicas with Asynchronous Connection Failover
- 19.4.10 半同步复制19.4.10 Semisynchronous Replication
- 19.4.11 延迟复制19.4.11 Delayed Replication
- 19.5 复制笔记和技巧19.5 Replication Notes and Tips
- 19.5.1 复制功能和问题19.5.1 Replication Features and Issues
- 19.5.2 MySQL 版本之间的复制兼容性19.5.2 Replication Compatibility Between MySQL Versions
- 19.5.3 升级或降级复制拓扑19.5.3 Upgrading or Downgrading a Replication Topology
- 19.5.4 复制故障排除19.5.4 Troubleshooting Replication
- 19.5.5 如何报告复制错误或问题19.5.5 How to Report Replication Bugs or Problems
复制使一个 MySQL 数据库服务器(称为源)中的数据能够被复制到一个或多个 MySQL 数据库服务器(称为副本)。复制默认是异步的;副本不需要永久连接即可接收来自源的更新。根据配置,你可以复制所有数据库、选定的数据库,甚至数据库内的选定表。Replication enables data from one MySQL database server (known as a source) to be copied to one or more MySQL database servers (known as replicas). Replication is asynchronous by default; replicas do not need to be connected permanently to receive updates from a source. Depending on the configuration, you can replicate all databases, selected databases, or even selected tables within a database.
MySQL 中复制的优点包括:Advantages of replication in MySQL include:
扩展解决方案 - 将负载分散到多个副本以提高性能。在这种环境中,所有写操作和更新必须在源服务器上进行。然而,读取操作可以在一个或多个副本上进行。这种模式可以提高写操作的性能(因为源服务器专用于更新),同时显著提高随着副本数量增加的读取速度。Scale-out solutions - spreading the load among multiple replicas to improve performance. In this environment, all writes and updates must take place on the source server. Reads, however, may take place on one or more replicas. This model can improve the performance of writes (since the source is dedicated to updates), while dramatically increasing read speed across an increasing number of replicas.
数据安全——由于副本可以暂停复制过程,因此可以在副本上运行备份服务,而不会破坏相应的源数据。Data security - because the replica can pause the replication process, it is possible to run backup services on the replica without corrupting the corresponding source data.
分析——可以在源上创建实时数据,同时在副本上进行信息分析,而不会影响源的性能。Analytics - live data can be created on the source, while the analysis of the information can take place on the replica without affecting the performance of the source.
远程数据分发——可以使用复制为远程站点创建数据的本地副本,而无需持续访问源。Long-distance data distribution - you can use replication to create a local copy of data for a remote site to use, without permanent access to the source.
有关如何在此类场景中使用复制的信息,请参见 第 19.4 节,“复制解决方案”。For information on how to use replication in such scenarios, see Section 19.4, “Replication Solutions”.
MySQL 8.4 支持不同的复制方法。传统方法是基于从源的二进制日志复制事件,并且需要在源和副本之间同步日志文件及其位置。基于 全局事务标识符(GTID)的新方法是事务性的,因此不需要处理日志文件或文件中的位置,这大大简化了许多常见的复制任务。使用 GTID 的复制保证了源和副本之间的一致性,只要源上提交的所有事务也已在副本上应用。有关 GTID 及基于 GTID 的 MySQL 复制的更多信息,请参见 第 19.1.3 节,“使用全局事务标识符进行复制”。有关使用基于二进制日志文件位置的复制的信息,请参见 第 19.1 节,“配置复制”。MySQL 8.4 supports different methods of replication. The traditional method is based on replicating events from the source's binary log, and requires the log files and positions in them to be synchronized between source and replica. The newer method based on global transaction identifiers (GTIDs) is transactional and therefore does not require working with log files or positions within these files, which greatly simplifies many common replication tasks. Replication using GTIDs guarantees consistency between source and replica as long as all transactions committed on the source have also been applied on the replica. For more information about GTIDs and GTID-based replication in MySQL, see Section 19.1.3, “Replication with Global Transaction Identifiers”. For information on using binary log file position based replication, see Section 19.1, “Configuring Replication”.
MySQL 的复制支持不同类型的同步。原始的同步类型是一种单向异步复制,其中一个服务器作为源服务器,而一个或多个其他服务器作为副本。这与 NDB Cluster 的特性——同步复制形成对比(参见第 25 章,MySQL NDB Cluster 8.4)。在 MySQL 8.4 中,除了内置的异步复制,还支持半同步复制。使用半同步复制时,源服务器上的提交操作在返回到执行事务的会话之前会阻塞,直到至少有一个副本确认已接收并记录了该事务的事件;详见第 19.4.10 节,“半同步复制”。MySQL 8.4 还支持延迟复制,使副本故意比源滞后至少指定的时间;详见第 19.4.11 节,“延迟复制”。对于需要同步复制的场景,请使用 NDB Cluster(参见第 25 章,MySQL NDB Cluster 8.4)。Replication in MySQL supports different types of synchronization. The original type of synchronization is one-way, asynchronous replication, in which one server acts as the source, while one or more other servers act as replicas. This is in contrast to the synchronous replication which is a characteristic of NDB Cluster (see Chapter 25, MySQL NDB Cluster 8.4). In MySQL 8.4, semisynchronous replication is supported in addition to the built-in asynchronous replication. With semisynchronous replication, a commit performed on the source blocks before returning to the session that performed the transaction until at least one replica acknowledges that it has received and logged the events for the transaction; see Section 19.4.10, “Semisynchronous Replication”. MySQL 8.4 also supports delayed replication such that a replica deliberately lags behind the source by at least a specified amount of time; see Section 19.4.11, “Delayed Replication”. For scenarios where synchronous replication is required, use NDB Cluster (see Chapter 25, MySQL NDB Cluster 8.4).
设置服务器间复制有多种解决方案可供选择,使用哪种方法最好取决于数据的存在情况以及你使用的引擎类型。有关可用选项的更多信息,请参见 第 19.1.2 节,“设置基于二进制日志文件位置的复制”。There are a number of solutions available for setting up replication between servers, and the best method to use depends on the presence of data and the engine types you are using. For more information on the available options, see Section 19.1.2, “Setting Up Binary Log File Position Based Replication”.
复制格式有两种核心类型:基于语句的复制(SBR),它复制整个 SQL 语句;基于行的复制(RBR),它只复制已更改的行。你还可以使用第三种类型,即混合复制(MBR)。有关不同复制格式的更多信息,请参见 第 19.2.1 节,“复制格式”。There are two core types of replication format, Statement Based Replication (SBR), which replicates entire SQL statements, and Row Based Replication (RBR), which replicates only the changed rows. You can also use a third variety, Mixed Based Replication (MBR). For more information on the different replication formats, see Section 19.2.1, “Replication Formats”.
复制通过多种不同的选项和变量进行控制。有关更多信息,请参见 第 19.1.6 节,“复制和二进制日志选项与变量”。可以对复制拓扑应用额外的安全措施,如 第 19.3 节,“复制安全” 所述。Replication is controlled through a number of different options and variables. For more information, see Section 19.1.6, “Replication and Binary Logging Options and Variables”. Additional security measures can be applied to a replication topology, as described in Section 19.3, “Replication Security”.
您可以使用复制来解决多种不同的问题,包括性能、支持不同数据库的备份,以及作为更大解决方案的一部分以缓解系统故障。有关如何解决这些问题的信息,请参见 第 19.4 节,“复制解决方案”。You can use replication to solve a number of different problems, including performance, supporting the backup of different databases, and as part of a larger solution to alleviate system failures. For information on how to address these issues, see Section 19.4, “Replication Solutions”.
有关在复制过程中不同数据类型和语句的处理方式的说明和提示,包括复制功能、版本兼容性、升级以及潜在问题及其解决方法的详细信息,请参见第 19.5 节,“复制说明和提示”。有关新接触 MySQL 复制的人经常问的一些问题的答案,请参见第 A.14 节,“MySQL 8.4 FAQ: 复制”。For notes and tips on how different data types and statements are treated during replication, including details of replication features, version compatibility, upgrades, and potential problems and their resolution, see Section 19.5, “Replication Notes and Tips”. For answers to some questions often asked by those who are new to MySQL Replication, see Section A.14, “MySQL 8.4 FAQ: Replication”.
有关复制实现、复制工作原理、二进制日志的过程和内容、后台线程以及用于决定如何记录和复制语句的规则的详细信息,请参见第 19.2 节,“复制实现”。For detailed information on the implementation of replication, how replication works, the process and contents of the binary log, background threads and the rules used to decide how statements are recorded and replicated, see Section 19.2, “Replication Implementation”.