如何配置 Azure Database for MySQL 的数据传入复制How to configure Azure Database for MySQL Data-in Replication

本文介绍如何通过配置主服务器和副本服务器在 Azure Database for MySQL 中设置数据传入复制This article describes how to set up Data-in Replication in Azure Database for MySQL by configuring the master and replica servers. 本文假设读者在 MySQL 服务器和数据库方面有一定的经验。This article assumes that you have some prior experience with MySQL servers and databases.

备注

将要查看的是 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 服务中创建副本,数据传入复制需同步本地 MySQL 主服务器、虚拟机 (VM) 或云数据库服务中的数据。To create a replica in the Azure Database for MySQL service, Data-in Replication synchronizes data from a master MySQL server on-premises, in virtual machines (VMs), or in cloud database services. 复制中数据以基于二进制日志 (binlog) 文件位置的从本机到 MySQL 的复制为基础。Data-in Replication is based on the binary log (binlog) file position-based replication native to MySQL. 若要了解有关 binlog 复制的详细信息,请参阅 MySQL binlog 复制概述To learn more about binlog replication, see the MySQL binlog replication overview.

在执行本文中的步骤之前,请查看数据传入复制的限制和要求Review the limitations and requirements of Data-in replication before performing the steps in this article.

创建将要用作副本的 MySQL 服务器Create a MySQL server to be used as replica

  1. 创建新的 Azure Database for MySQL 服务器Create a new Azure Database for MySQL server

    创建新的 MySQL 服务器(例如Create a new MySQL server (ex. “replica.mysql.database.chinacloudapi.cn”)。"replica.mysql.database.chinacloudapi.cn"). 请参阅使用 Azure 门户创建 Azure Database for MySQL 服务器,了解如何创建服务器。Refer to Create an Azure Database for MySQL server by using the Azure portal for server creation. 在数据传入复制中,此服务器为“副本”服务器。This server is the "replica" server in Data-in Replication.

    重要

    必须在“常规用途”或“内存优化”定价层中创建 Azure Database for MySQL 服务器。The Azure Database for MySQL server must be created in the General Purpose or Memory Optimized pricing tiers.

  2. 创建相同的用户帐户和相应的特权Create same user accounts and corresponding privileges

    用户帐户不会从主服务器复制到副本服务器。User accounts are not replicated from the master server to the replica server. 如果打算为用户提供访问副本服务器的权限,则需在这个新创建的 Azure Database for MySQL 服务器上手动创建所有帐户和相应的特权。If you plan on providing users with access to the replica server, you need to manually create all accounts and corresponding privileges on this newly created Azure Database for MySQL server.

  3. 将主服务器的 IP 地址添加到副本的防火墙规则。Add the master server's IP address to the replica's firewall rules.

    使用 Azure 门户Azure CLI 更新防火墙规则。Update firewall rules using the Azure portal or Azure CLI.

配置主服务器Configure the master server

以下步骤准备并配置本地或虚拟机中托管的 MySQL 服务器或其他云提供程序托管的数据库服务,以便向内复制数据。The following steps prepare and configure the MySQL server hosted on-premises, in a virtual machine, or database service hosted by other cloud providers for Data-in Replication. 此服务器是“数据传入复制”中的“主”服务器。This server is the "master" in Data-in replication.

  1. 请先查看主服务器要求,然后再继续。Review the master server requirements before proceeding.

    例如,请确保主服务器允许端口 3306 上的入站和出站流量,并且主服务器具有公共 IP 地址,DNS 可公开访问或者具有完全限定的域名 (FQDN)。For example, ensure the master server allows both inbound and outbound traffic on port 3306 and that the master server has a public IP address, the DNS is publicly accessible, or has a fully qualified domain name (FQDN).

    尝试从另一台计算机上托管的工具(如 MySQL 命令行)进行连接,以测试与主服务器的连接。Test connectivity to the master server by attempting to connect from a tool such as the MySQL command-line hosted on another machine.

  2. 启用二进制日志记录Turn on binary logging

    运行以下命令以检查是否已在主服务器上启用了二进制日志记录:Check to see if binary logging has been enabled on the master by running the following command:

    SHOW VARIABLES LIKE 'log_bin';
    

    如果返回了值为“ON”的变量 log_bin,则表示已在服务器上启用了二进制日志记录。If the variable log_bin is returned with the value "ON", binary logging is enabled on your server.

    如果返回了值为“OFF”的 log_bin,请将 my.cnf 文件编辑为 log_bin=ON 以启用二进制日志记录,并重启服务器使更改生效。If log_bin is returned with the value "OFF", turn on binary logging by editing your my.cnf file so that log_bin=ON and restart your server for the change to take effect.

  3. 主服务器设置Master server settings

    “数据传入复制”要求参数 lower_case_table_names 在主服务器与副本服务器之间保持一致。Data-in Replication requires parameter lower_case_table_names to be consistent between the master and replica servers. 在 Azure Database for MySQL 中,该参数默认为 1。This parameter is 1 by default in Azure Database for MySQL.

    SET GLOBAL lower_case_table_names = 1;
    
  4. 创建新的复制角色并设置权限Create a new replication role and set up permission

    在主服务器上创建一个配置有复制特权的用户帐户。Create a user account on the master server that is configured with replication privileges. 这可以通过 SQL 命令或 MySQL Workbench 之类的工具来完成。This can be done through SQL commands or a tool like MySQL Workbench. 考虑是否打算使用 SSL 进行复制,因为这需要在创建用户时指定。Consider whether you plan on replicating with SSL as this will need to be specified when creating the user. 请参阅 MySQL 文档以了解如何在主服务器上添加用户帐户Refer to the MySQL documentation to understand how to add user accounts on your master server.

    在以下命令中,创建的新复制角色能够从任何计算机访问主服务器,而不仅仅是从本身托管主服务器的计算机访问主服务器。In the commands below, the new replication role created is able to access the master from any machine, not just the machine that hosts the master itself. 这可以通过在创建用户的命令中指定“syncuser@'%'”来完成。This is done by specifying "syncuser@'%'" in the create user command. 请参阅 MySQL 文档,详细了解如何指定帐户名称See the MySQL documentation to learn more about specifying account names.

    SQL 命令SQL Command

    使用 SSL 复制Replication with SSL

    如果所有用户连接都要求 SSL,请使用以下命令来创建用户:To require SSL for all user connections, use the following command to create a user:

    CREATE USER 'syncuser'@'%' IDENTIFIED BY 'yourpassword';
    GRANT REPLICATION SLAVE ON *.* TO ' syncuser'@'%' REQUIRE SSL;
    

    不使用 SSL 复制Replication without SSL

    如果所有用户连接都不要求 SSL,请使用以下命令来创建用户:If SSL is not required for all connections, use the following command to create a user:

    CREATE USER 'syncuser'@'%' IDENTIFIED BY 'yourpassword';
    GRANT REPLICATION SLAVE ON *.* TO ' syncuser'@'%';
    

    MySQL WorkbenchMySQL Workbench

    若要在 MySQL Workbench 中创建复制角色,请在“管理”面板中打开“用户和特权”面板。 To create the replication role in MySQL Workbench, open the Users and Privileges panel from the Management panel. 然后单击“添加帐户”。Then click on Add Account.

    用户和特权

    在“登录名称”字段中键入用户名。Type in the username into the Login Name field.

    同步用户

    单击“管理角色”面板,然后从“全局特权”列表中选择“复制从属实例”。 Click on the Administrative Roles panel and then select Replication Slave from the list of Global Privileges. 然后单击“应用”,创建复制角色。Then click on Apply to create the replication role.

    复制从属实例

  5. 将主服务器设置为只读模式Set the master server to read-only mode

    在开始转储数据库之前,需将服务器置于只读模式。Before starting to dump out the database, the server needs to be placed in read-only mode. 在只读模式下,主服务器无法处理任何写入事务。While in read-only mode, the master will be unable to process any write transactions. 评估对业务的影响,根据需要在非高峰时间计划只读窗口。Evaluate the impact to your business and schedule the read-only window in an off-peak time if necessary.

    FLUSH TABLES WITH READ LOCK;
    SET GLOBAL read_only = ON;
    
  6. 获取二进制日志文件名和偏移量Get binary log file name and offset

    运行 show master status 命令,确定当前的二进制日志文件名和偏移量。Run the show master status command to determine the current binary log file name and offset.

    show master status;
    

    结果应如下所示。The results should be like following. 确保记下此二进制文件名,因为在后面的步骤中会用到它。Make sure to note the binary file name as it will be used in later steps.

    主机状态结果

转储并还原主服务器Dump and restore master server

  1. 确定要将哪些数据库和表复制到 Azure Database for MySQL 并从主服务器执行转储。Determine which databases and tables you want to replicate into Azure Database for MySQL and perform the dump from the master server.

    可以使用 mysqldump 从主服务器转储数据库。You can use mysqldump to dump databases from your master. 有关详细信息,请参阅转储和还原For details, refer to Dump & Restore. 不需转储 MySQL 库和测试库。It is unnecessary to dump MySQL library and test library.

  2. 将主服务器设置为读/写模式Set master server to read/write mode

    转储数据库后,将 MySQL 主服务器改回读/写模式。Once the database has been dumped, change the master MySQL server back to read/write mode.

    SET GLOBAL read_only = OFF;
    UNLOCK TABLES;
    
  3. 将转储文件还原到新服务器Restore dump file to new server

    将转储文件还原到在 Azure Database for MySQL 服务中创建的服务器。Restore the dump file to the server created in the Azure Database for MySQL service. 请参阅转储和还原,了解如何将转储文件还原到 MySQL 服务器。Refer to Dump & Restore for how to restore a dump file to a MySQL server. 如果转储文件较大,请将它上传到副本服务器所在区域的 Azure 中的虚拟机。If the dump file is large, upload it to a virtual machine in Azure within the same region as your replica server. 将它从虚拟机还原到 Azure Database for MySQL 服务器。Restore it to the Azure Database for MySQL server from the virtual machine.

  1. 设置主服务器Set master server

    所有数据传入复制功能都是通过存储过程完成的。All Data-in Replication functions are done by stored procedures. 可以在数据传入复制存储过程中找到所有过程。You can find all procedures at Data-in Replication Stored Procedures. 这些存储过程可以在 MySQL shell 或 MySQL Workbench 中运行。The stored procedures can be run in the MySQL shell or MySQL Workbench.

    若要链接两个服务器并启动复制,请在 Azure DB for MySQL 服务中登录到目标副本服务器,并将外部实例设置为主服务器。To link two servers and start replication, login to the target replica server in the Azure DB for MySQL service and set the external instance as the master server. 为此,可在 Azure DB for MySQL 服务器上使用 mysql.az_replication_change_master 存储过程。This is done by using the mysql.az_replication_change_master stored procedure on the Azure DB for MySQL server.

    CALL mysql.az_replication_change_master('<master_host>', '<master_user>', '<master_password>', 3306, '<master_log_file>', <master_log_pos>, '<master_ssl_ca>');
    
    • master_host:主服务器的主机名master_host: hostname of the master server
    • master_user:主服务器的用户名master_user: username for the master server
    • master_password:主服务器的密码master_password: password for the master server
    • master_log_file:正在运行的 show master status 中的二进制日志文件名master_log_file: binary log file name from running show master status
    • master_log_pos:正在运行的 show master status 中的二进制日志位置master_log_pos: binary log position from running show master status
    • master_ssl_ca:CA 证书的上下文。master_ssl_ca: CA certificate's context. 如果不使用 SSL,请传入空字符串。If not using SSL, pass in empty string.
      • 建议以变量形式传入此参数。It is recommended to pass this parameter in as a variable. 有关详细信息,请参阅以下示例。See the following examples for more information.

    备注

    如果主服务器托管在 Azure VM 中,请将“允许访问 Azure 服务”设置为“启用”,以允许主服务器和副本服务器相互通信。If the master server is hosted in an Azure VM, set "Allow access to Azure services" to "ON" to allow the master and replica servers to communicate with each other. 从“连接安全性”选项可更改此设置。This setting can be changed from the Connection security options. 请参阅使用门户管理防火墙规则获取详细信息。Refer to manage firewall rules using portal for more information.

    示例Examples

    使用 SSL 复制Replication with SSL

    运行以下 MySQL 命令创建变量 @certThe variable @cert is created by running the following MySQL commands:

    SET @cert = '-----BEGIN CERTIFICATE-----
    PLACE YOUR PUBLIC KEY CERTIFICATE'`S CONTEXT HERE
    -----END CERTIFICATE-----'
    

    在域“companya.com”中托管的主服务器与 Azure Database for MySQL 中托管的副本服务器之间设置了使用 SSL 进行复制的功能。Replication with SSL is set up between a master server hosted in the domain "companya.com" and a replica server hosted in Azure Database for MySQL. 将在副本上运行此存储过程。This stored procedure is run on the replica.

    CALL mysql.az_replication_change_master('master.companya.com', 'syncuser', 'P@ssword!', 3306, 'mysql-bin.000002', 120, @cert);
    

    不使用 SSL 复制Replication without SSL

    在域“companya.com”中托管的主服务器与 Azure Database for MySQL 中托管的副本服务器之间设置了不使用 SSL 进行复制的功能。Replication without SSL is set up between a master server hosted in the domain "companya.com" and a replica server hosted in Azure Database for MySQL. 将在副本上运行此存储过程。This stored procedure is run on the replica.

    CALL mysql.az_replication_change_master('master.companya.com', 'syncuser', 'P@ssword!', 3306, 'mysql-bin.000002', 120, '');
    
  2. FilteringFiltering

    如果要跳过从主副本复制某些表的操作,请更新副本服务器上的 replicate_wild_ignore_table 服务器参数。If you want to skip replicating some tables from your master, update the replicate_wild_ignore_table server parameter on your replica server. 可以使用逗号分隔的列表提供多个表模式。You can provide more than one table pattern using a comma-separated list.

    查看 MySQL 文档详细了解此参数。Review the MySQL documentation to learn more about this parameter.

    若要更新该参数,可以使用 Azure 门户Azure CLITo update the parameter, you can use the Azure portal or Azure CLI.

  3. 启动复制Start replication

    调用 mysql.az_replication_start 存储过程来启动复制。Call the mysql.az_replication_start stored procedure to initiate replication.

    CALL mysql.az_replication_start;
    
  4. 检查复制状态Check replication status

    在副本服务器上调用 show slave status 命令查看复制状态。Call the show slave status command on the replica server to view the replication status.

    show slave status;
    

    如果 Slave_IO_RunningSlave_SQL_Running 状态为“yes”,并且 Seconds_Behind_Master 的值为“0”,则表示复制正常运行。If the state of Slave_IO_Running and Slave_SQL_Running are "yes" and the value of Seconds_Behind_Master is "0", replication is working well. Seconds_Behind_Master 指示副本的陈旧状态。Seconds_Behind_Master indicates how late the replica is. 如果其值不为“0”,则表示副本正在处理更新。If the value is not "0", it means that the replica is processing updates.

其他存储过程Other stored procedures

停止复制Stop replication

若要停止主服务器与副本服务器之间的复制,请使用以下存储过程:To stop replication between the master and replica server, use the following stored procedure:

CALL mysql.az_replication_stop;

删除复制关系Remove replication relationship

若要删除主服务器与副本服务器之间的关系,请使用以下存储过程:To remove the relationship between master and replica server, use the following stored procedure:

CALL mysql.az_replication_remove_master;

跳过复制错误Skip replication error

若要跳过复制错误并允许复制继续,请使用以下存储过程:To skip a replication error and allow replication to proceed, use the following stored procedure:

CALL mysql.az_replication_skip_counter;

后续步骤Next steps