- 19.1.2.1 设置复制源配置19.1.2.1 Setting the Replication Source Configuration
- 19.1.2.2 设置副本配置19.1.2.2 Setting the Replica Configuration
- 19.1.2.3 创建用于复制的用户19.1.2.3 Creating a User for Replication
- 19.1.2.4 获取复制源二进制日志坐标19.1.2.4 Obtaining the Replication Source Binary Log Coordinates
- 19.1.2.5 选择数据快照的方法19.1.2.5 Choosing a Method for Data Snapshots
- 19.1.2.6 设置副本19.1.2.6 Setting Up Replicas
- 19.1.2.7 在副本上设置源配置19.1.2.7 Setting the Source Configuration on the Replica
- 19.1.2.8 向复制环境添加副本19.1.2.8 Adding Replicas to a Replication Environment
本节介绍如何设置 MySQL 服务器以使用基于二进制日志文件位置的复制。设置复制的方法有多种,具体使用哪种方法取决于您如何设置复制,以及源数据库中是否已有您希望复制的数据。This section describes how to set up a MySQL server to use binary log file position based replication. There are a number of different methods for setting up replication, and the exact method to use depends on how you are setting up replication, and whether you already have data in the database on the source that you want to replicate.
要部署多个 MySQL 实例,您可以使用 InnoDB 集群,它使您能够在 MySQL Shell 中轻松管理一组 MySQL 服务器实例。InnoDB 集群在编程环境中封装了 MySQL 群组复制,使您可以轻松部署 MySQL 实例集群以实现高可用性。此外,InnoDB 集群可以与 MySQL Router 无缝接口,使您的应用程序无需编写自己的故障转移流程即可连接到集群。然而,对于不需要高可用性的类似用例,您可以使用 InnoDB 副本集。MySQL Shell 的安装说明可以在 这里 找到。To deploy multiple instances of MySQL, you can use InnoDB Cluster which enables you to easily administer a group of MySQL server instances in MySQL Shell. InnoDB Cluster wraps MySQL Group Replication in a programmatic environment that enables you easily deploy a cluster of MySQL instances to achieve high availability. In addition, InnoDB Cluster interfaces seamlessly with MySQL Router, which enables your applications to connect to the cluster without writing your own failover process. For similar use cases that do not require high availability, however, you can use InnoDB ReplicaSet. Installation instructions for MySQL Shell can be found here.
有一些通用任务适用于所有配置:There are some generic tasks that are common to all setups:
在源端,您必须确保启用了二进制日志,并配置一个唯一的服务器 ID。这可能需要重启服务器。请参见 第 19.1.2.1 节,“设置复制源配置”。On the source, you must ensure that binary logging is enabled, and configure a unique server ID. This might require a server restart. See Section 19.1.2.1, “Setting the Replication Source Configuration”.
在每个要连接到源端的副本上,您必须配置一个唯一的服务器 ID。这可能需要重启服务器。请参见 第 19.1.2.2 节,“设置副本配置”。On each replica that you want to connect to the source, you must configure a unique server ID. This might require a server restart. See Section 19.1.2.2, “Setting the Replica Configuration”.
可选地,为你的副本创建一个单独的用户,以便在读取用于复制的二进制日志时与源进行身份验证。请参见第 19.1.2.3 节,“创建用于复制的用户”。Optionally, create a separate user for your replicas to use during authentication with the source when reading the binary log for replication. See Section 19.1.2.3, “Creating a User for Replication”.
在创建数据快照或启动复制过程之前,应在源端记录二进制日志的当前位置。在配置副本时需要这些信息,以便副本知道从二进制日志中的什么位置开始执行事件。请参见第 19.1.2.4 节,“获取复制源二进制日志坐标”。Before creating a data snapshot or starting the replication process, on the source you should record the current position in the binary log. You need this information when configuring the replica so that the replica knows where within the binary log to start executing events. See Section 19.1.2.4, “Obtaining the Replication Source Binary Log Coordinates”.
如果你已经有源数据并希望使用它来同步副本,则需要创建数据快照以将数据复制到副本。你使用的存储引擎会影响创建快照的方式。当你使用
MyISAM时,你必须停止源上的语句处理以获取读锁,然后获取其当前的二进制日志坐标并转储其数据,之后才允许源继续执行语句。如果你不停止语句执行,数据转储和源状态信息就会不匹配,从而导致副本上的数据库不一致或损坏。有关复制MyISAM源的更多信息,请参见 第 19.1.2.4 节,“获取复制源二进制日志坐标”。如果你使用InnoDB,则不需要读锁,只需要一个足够长的事务来传输数据快照即可。有关更多信息,请参见 第 17.19 节,“InnoDB 和 MySQL 复制”。If you already have data on the source and want to use it to synchronize the replica, you need to create a data snapshot to copy the data to the replica. The storage engine you are using has an impact on how you create the snapshot. When you are usingMyISAM, you must stop processing statements on the source to obtain a read-lock, then obtain its current binary log coordinates and dump its data, before permitting the source to continue executing statements. If you do not stop the execution of statements, the data dump and the source status information become mismatched, resulting in inconsistent or corrupted databases on the replicas. For more information on replicating aMyISAMsource, see Section 19.1.2.4, “Obtaining the Replication Source Binary Log Coordinates”. If you are usingInnoDB, you do not need a read-lock and a transaction that is long enough to transfer the data snapshot is sufficient. For more information, see Section 17.19, “InnoDB and MySQL Replication”.使用用于连接到源的设置配置副本,例如主机名、登录凭据以及二进制日志文件名和位置。参见 第 19.1.2.7 节,“在副本上设置源配置”。Configure the replica with settings for connecting to the source, such as the host name, login credentials, and binary log file name and position. See Section 19.1.2.7, “Setting the Source Configuration on the Replica”.
根据系统的需要,在源和副本上实施特定的复制安全措施。参见 第 19.3 节,“复制安全”。Implement replication-specific security measures on the sources and replicas as appropriate for your system. See Section 19.3, “Replication Security”.
设置过程中的某些步骤需要 SUPER 权限。如果您没有此权限,可能无法启用复制。Certain steps within the setup process require the
SUPER privilege. If you do not
have this privilege, it might not be possible to enable
replication.
配置基本选项后,选择您的场景:After configuring the basic options, select your scenario:
要为一个包含无数据的源和副本的新安装设置复制,请参阅 第 19.1.2.6.1 节,“使用新源和副本设置复制”。To set up replication for a fresh installation of a source and replicas that contain no data, see Section 19.1.2.6.1, “Setting Up Replication with New Source and Replicas”.
要使用现有 MySQL 服务器中的数据设置新源的复制,请参阅 第 19.1.2.6.2 节,“使用现有数据设置复制”。To set up replication of a new source using the data from an existing MySQL server, see Section 19.1.2.6.2, “Setting Up Replication with Existing Data”.
要向现有复制环境中添加副本,请参阅 第 19.1.2.8 节,“向复制环境添加副本”。To add replicas to an existing replication environment, see Section 19.1.2.8, “Adding Replicas to a Replication Environment”.
在管理 MySQL 复制服务器之前,请阅读本章的全部内容,并尝试 第 15.4.1 节,“用于控制源服务器的 SQL 语句” 和 第 15.4.2 节,“用于控制副本服务器的 SQL 语句” 中提到的所有语句。同时熟悉 第 19.1.6 节,“复制及二进制日志选项和变量” 中描述的复制启动选项。Before administering MySQL replication servers, read this entire chapter and try all statements mentioned in Section 15.4.1, “SQL Statements for Controlling Source Servers”, and Section 15.4.2, “SQL Statements for Controlling Replica Servers”. Also familiarize yourself with the replication startup options described in Section 19.1.6, “Replication and Binary Logging Options and Variables”.