Azure Database for MySQL 中的只读副本Read replicas in Azure Database for MySQL

使用只读副本功能可将数据从 Azure Database for MySQL 服务器复制到只读服务器。The read replica feature allows you to replicate data from an Azure Database for MySQL server to a read-only server. 可将主服务器中的数据复制到最多 5 个副本。You can replicate from the master server to up to five replicas. 使用 MySQL 引擎的基于本机二进制日志 (binlog) 文件位置的复制技术以异步方式更新副本。Replicas are updated asynchronously using the MySQL engine's native binary log (binlog) file position-based replication technology. 若要了解有关 binlog 复制的详细信息,请参阅 MySQL binlog 复制概述To learn more about binlog replication, see the MySQL binlog replication overview.


将要查看的是 Azure Database for MySQL 的新服务。You are viewing the new service of Azure Database for MySQL. 若要查看经典 MySQL Database for Azure 的文档,请访问此页To view the documentation for classic MySQL Database for Azure, please visit this page.

副本是新的服务器,可以像管理普通的 Azure Database for MySQL 服务器一样对其进行管理。Replicas are new servers that you manage similar to regular Azure Database for MySQL servers. 每个只读副本按照预配计算资源的 vCore 数量以及每月 GB 存储量计费。For each read replica, you're billed for the provisioned compute in vCores and storage in GB/ month.

如需了解有关 MySQL 复制功能和问题的详细信息,请参阅 MySQL 复制文档To learn more about MySQL replication features and issues, see the MySQL replication documentation.

何时使用只读副本When to use a read replica

只读副本功能可帮助改善读取密集型工作负荷的性能与规模。The read replica feature helps to improve the performance and scale of read-intensive workloads. 读取工作负载可以与副本服务器隔离,而写入工作负载可以定向到主服务器。Read workloads can be isolated to the replicas, while write workloads can be directed to the master.

常见方案是让 BI 和分析工作负载将只读副本用作报告的数据源。A common scenario is to have BI and analytical workloads use the read replica as the data source for reporting.

由于副本是只读的,它们不能直接缓解主服务器上的写入容量负担。Because replicas are read-only, they don't directly reduce write-capacity burdens on the master. 此功能并非面向写入密集型工作负荷。This feature isn't targeted at write-intensive workloads.

只读副本功能使用 MySQL 本机异步复制。The read replica feature uses MySQL asynchronous replication. 该功能不适用于同步复制方案。The feature isn't meant for synchronous replication scenarios. 主服务器与副本之间存在明显的延迟。There will be a measurable delay between the master and the replica. 副本上的数据最终将与主服务器上的数据保持一致。The data on the replica eventually becomes consistent with the data on the master. 对于能够适应这种延迟的工作负荷,可以使用此功能。Use this feature for workloads that can accommodate this delay.

跨区域复制Cross-region replication

可以在与主服务器不同的区域中创建只读副本。You can create a read replica in a different region from your master server. 跨区域复制对于灾难恢复规划或使数据更接近用户等方案非常有用。Cross-region replication can be helpful for scenarios like disaster recovery planning or bringing data closer to your users.

可以在任何 Azure Database for MySQL 区域中设置主服务器。You can have a master server in any Azure Database for MySQL region. 主服务器可以在其配对区域中有一个副本。A master server can have a replica in its paired region.

配对区域Paired regions

可以在主服务器的 Azure 配对区域中创建只读副本。You can create a read replica in the Azure paired region of your master server. 如果你不知道所在区域的配对,可以从 Azure 配对区域一文中了解更多信息。If you don't know your region's pair, you can learn more from the Azure Paired Regions article.

如果你使用跨区域副本进行灾难恢复规划,建议你在配对区域而不是其他某个区域中创建副本。If you are using cross-region replicas for disaster recovery planning, we recommend you create the replica in the paired region instead of one of the other regions. 配对区域可避免同时更新,并优先考虑物理隔离和数据驻留。Paired regions avoid simultaneous updates and prioritize physical isolation and data residency.

创建副本Create a replica


只读副本功能仅适用于“常规用途”或“内存优化”定价层中的 Azure Database for MySQL 服务器。The read replica feature is only available for Azure Database for MySQL servers in the General Purpose or Memory Optimized pricing tiers. 请确保主服务器位于其中一个定价层中。Ensure the master server is in one of these pricing tiers.

如果主服务器没有现有的副本服务器,主服务器会先重启,以自行准备复制。If a master server has no existing replica servers, the master will first restart to prepare itself for replication.

启动“创建副本”工作流时,将创建空白的 Azure Database for MySQL 服务器。When you start the create replica workflow, a blank Azure Database for MySQL server is created. 新服务器中填充了主服务器上的数据。The new server is filled with the data that was on the master server. 创建时间取决于主服务器上的数据量,以及自上次每周完整备份以来所经历的时间。The creation time depends on the amount of data on the master and the time since the last weekly full backup. 具体所需时间从几分钟到几小时不等。The time can range from a few minutes to several hours. 副本服务器始终在与主服务器相同的资源组和订阅中创建。The replica server is always created in the same resource group and same subscription as the master server. 如果要将副本服务器创建到不同的资源组或不同的订阅,可以在创建后移动副本服务器If you want to create a replica server to a different resource group or different subscription, you can move the replica server after creation.

每个副本都启用了存储自动增长Every replica is enabled for storage auto-grow. 自动增长功能允许副本与复制到它的数据保持同步,并防止由于存储空间不足错误而导致的复制中断。The auto-grow feature allows the replica to keep up with the data replicated to it, and prevent an interruption in replication caused by out-of-storage errors.

了解如何在 Azure 门户中创建只读副本Learn how to create a read replica in the Azure portal.

连接到副本Connect to a replica

创建时,副本会继承主服务器的防火墙规则。At creation, a replica inherits the firewall rules of the master server. 之后,这些规则将独立于主服务器。Afterwards, these rules are independent from the the master server.

副本从主服务器继承其管理员帐户。The replica inherits the admin account from the master server. 主服务器上的所有用户帐户将复制到只读副本。All user accounts on the master server are replicated to the read replicas. 只能使用主服务器上可用的用户帐户连接到只读副本。You can only connect to a read replica by using the user accounts that are available on the master server.

可以使用主机名和有效的用户帐户连接到副本,就像在常规的 Azure Database for MySQL 服务器上连接一样。You can connect to the replica by using its hostname and a valid user account, as you would on a regular Azure Database for MySQL server. 对于名称为 myreplica、管理员用户名为 myadmin 的服务器,可以使用 mysql CLI 连接到副本:For a server named myreplica with the admin username myadmin, you can connect to the replica by using the mysql CLI:

mysql -h -u myadmin@myreplica -p

在提示符下,输入用户帐户的密码。At the prompt, enter the password for the user account.

监视复制Monitor replication

Azure Database for MySQL 在 Azure Monitor 中提供“复制滞后时间(秒)”指标。Azure Database for MySQL provides the Replication lag in seconds metric in Azure Monitor. 此指标仅适用于副本。This metric is available for replicas only.

此指标是使用 MySQL 的 SHOW SLAVE STATUS 命令中提供的 seconds_behind_master 指标计算的。This metric is calculated using the seconds_behind_master metric available in MySQL's SHOW SLAVE STATUS command.

请设置警报,以便在复制滞后时间达到工作负荷不可接受的值时收到通知。Set an alert to inform you when the replication lag reaches a value that isn't acceptable for your workload.

停止复制Stop replication

可以停止主服务器与副本之间的复制。You can stop replication between a master and a replica. 在主服务器与只读副本之间停止复制后,副本将成为独立服务器。After replication is stopped between a master server and a read replica, the replica becomes a standalone server. 独立服务器中的数据是启动“停止复制”命令时副本上可用的数据。The data in the standalone server is the data that was available on the replica at the time the stop replication command was started. 独立服务器与主服务器不同步。The standalone server doesn't catch up with the master server.

选择停止复制到副本时,副本会丢失指向其以前的主服务器和其他副本的所有链接。When you choose to stop replication to a replica, it loses all links to its previous master and other replicas. 在主服务器与其副本之间无法自动进行故障转移。There is no automated failover between a master and its replica.


独立服务器不能再次成为副本。The standalone server can't be made into a replica again. 在只读副本上停止复制之前,请确保副本包含所需的全部数据。Before you stop replication on a read replica, ensure the replica has all the data that you require.

了解如何停止复制到副本Learn how to stop replication to a replica.


在主服务器与副本服务器之间无法自动进行故障转移。There is no automated failover between master and replica servers.

由于复制是异步的,因此在主体和副本之间存在延迟。Since replication is asynchronous, there is lag between the master and the replica. 延迟程度受许多因素影响,例如,在主服务器上运行的工作负荷有多大,以及数据中心之间的延迟有多严重。The amount of lag can be influenced by a number of factors like how heavy the workload running on the master server is and the latency between data centers. 大多数情况下,副本验证在几秒钟到几分钟之间。In most cases, replica lag ranges between a few seconds to a couple minutes. 可以使用“副本延迟”指标来跟踪实际的副本延迟,该指标适用于每个副本。You can track your actual replication lag using the metric Replica Lag, which is available for each replica. 该指标显示的是自上次重播事务以来所经历的时间。This metric shows the time since the last replayed transaction. 建议观察一段时间的副本延迟,以便确定平均延迟。We recommend that you identify what your average lag is by observing your replica lag over a period of time. 可以针对副本延迟设置警报,这样,当它超出预期范围时,你就可以采取行动。You can set an alert on replica lag, so that if it goes outside your expected range, you can take action.


如果故障转移到副本,则取消副本与主体之间的链接时遇到的延迟会指示丢失了多少数据。If you failover to the replica, the lag at the time you delink the replica from the master will indicate how much data is lost.

一旦决定要故障转移到某个副本,Once you have decided you want to failover to a replica,

  1. 请停止将数据复制到副本Stop replication to the replica
    此步骤是使副本服务器能够接受写入所必需的。This step is necessary to make the replica server able to accept writes. 在此过程中,副本服务器会取消与主体的链接。As part of this process, the replica server will be delinked from the master. 启动停止复制的操作后,后端进程通常需要大约 2 分钟才能完成。Once you initiate stop replication, the backend process typically takes about 2 minutes to complete. 请参阅本文的停止复制部分,了解此操作的潜在影响。See the stop replication section of this article to understand the implications of this action.

  2. 将应用程序指向(以前的)副本Point your application to the (former) replica
    每个服务器都有唯一的连接字符串。Each server has a unique connection string. 更新应用程序,使之指向(以前的)副本而不是主体。Update your application to point to the (former) replica instead of the master.

如果应用程序成功处理了读取和写入操作,则表明故障转移已完成。Once your application is successfully processing reads and writes, you have completed the failover. 应用程序经历的停机时间取决于何时检测到问题并完成上面的步骤 1 和 2。The amount of downtime your application experiences will depend on when you detect an issue and complete steps 1 and 2 above.

注意事项和限制Considerations and limitations

定价层Pricing tiers

只读副本当前仅适用于“常规用途”和“内存优化”的定价层。Read replicas are currently only available in the General Purpose and Memory Optimized pricing tiers.


运行副本服务器的成本取决于副本服务器的运行区域。The cost of running the replica server is based on the region where the replica server is running.

主服务器重启Master server restart

为没有现有副本的主服务器创建副本时,主服务器将首先重启以便为复制做好准备。When you create a replica for a master that has no existing replicas, the master will first restart to prepare itself for replication. 请考虑这一点并在非高峰期执行这些操作。Take this into consideration and perform these operations during an off-peak period.

新副本New replicas

只读副本创建为新的 Azure Database for MySQL 服务器。A read replica is created as a new Azure Database for MySQL server. 无法将现有的服务器设为副本。An existing server can't be made into a replica. 无法创建另一个只读副本的副本。You can't create a replica of another read replica.

副本配置Replica configuration

副本是使用与主服务器相同的服务器配置创建的。A replica is created by using the same server configuration as the master. 创建副本后,可以独立于主服务器更改多项设置:计算代系、vCore 数、存储和备份保留期。After a replica is created, several settings can be changed independently from the master server: compute generation, vCores, storage, and backup retention period. 定价层也可以独立更改,但“基本”层除外。The pricing tier can also be changed independently, except to or from the Basic tier.


将主服务器的配置更新为新值之前,请将副本配置更新为与这些新值相等或更大的值。Before a master server configuration is updated to new values, update the replica configuration to equal or greater values. 此操作可确保副本与主服务器发生的任何更改保持同步。This action ensures the replica can keep up with any changes made to the master.

创建副本服务器时,防火墙规则和参数设置会从主服务器继承到副本服务器。Firewall rules and parameter settings are inherited from the master server to the replica when the replica is created. 之后,副本的规则便会独立。Afterwards, the replica's rules are independent.

停止的副本Stopped replicas

如果停止主服务器与只读副本之间的复制,已停止的副本将成为可接受读取和写入的独立服务器。If you stop replication between a master server and a read replica, the stopped replica becomes a standalone server that accepts both reads and writes. 独立服务器不能再次成为副本。The standalone server can't be made into a replica again.

删除的主服务器和独立服务器Deleted master and standalone servers

删除主服务器后,将对所有只读副本停止复制。When a master server is deleted, replication is stopped to all read replicas. 这些副本会自动成为独立服务器,并且可以接受读取和写入。These replicas automatically become standalone servers and can accept both reads and writes. 将删除主服务器本身。The master server itself is deleted.

用户帐户User accounts

主服务器上的用户将复制到只读副本。Users on the master server are replicated to the read replicas. 只能使用主服务器上可用的用户帐户连接到只读副本。You can only connect to a read replica using the user accounts available on the master server.

服务器参数Server parameters

为了防止数据不同步并避免潜在的数据丢失或损坏,使用读取副本时,会锁定某些服务器参数以防止其更新。To prevent data from becoming out of sync and to avoid potential data loss or corruption, some server parameters are locked from being updated when using read replicas.

将在主服务器和副本服务器上锁定以下的服务器参数:The following server parameters are locked on both the master and replica servers:

副本服务器上的 event_scheduler 参数处于锁定状态。The event_scheduler parameter is locked on the replica servers.

若要更新主服务器上的上述参数之一,请删除副本服务器,更新主服务器上的参数值,然后重新创建副本。To update one of the above parameters on the master server, please delete replica servers, update the parameter value on the master, and recreate replicas.


  • 不支持全局事务标识符 (GTID)。Global transaction identifiers (GTID) are not supported.
  • 不支持创建副本服务器的副本。Creating a replica of a replica is not supported.
  • 内存中的表可能会导致副本服务器变得不同步。这是 MySQL 复制技术的限制。In-memory tables may cause replicas to become out of sync. This is a limitation of the MySQL replication technology. 有关详细信息,请阅读 MySQL 参考文档中的更多信息。Read more in the MySQL reference documentation for more information.
  • 确保主服务器表具有主键。Ensure the master server tables have primary keys. 缺少主键可能会导致主服务器与副本服务器之间的复制延迟。Lack of primary keys may result in replication latency between the master and replicas.
  • 查看 MySQL 文档中 MySQL 复制限制的完整列表Review the full list of MySQL replication limitations in the MySQL documentation

后续步骤Next steps