|
| 1 | +--- |
| 2 | +title: Configure Data-out replication - Azure Database for MySQL Flexible Server |
| 3 | +description: This article describes how to set up Data-in replication for Azure Database for MySQL Flexible Server. |
| 4 | +author: VandhanaMehta |
| 5 | +ms.author: vamehta |
| 6 | +ms.reviewer: maghan |
| 7 | +ms.date: 12/29/2022 |
| 8 | +ms.service: mysql |
| 9 | +ms.subservice: flexible-server |
| 10 | +ms.topic: how-to |
| 11 | +--- |
| 12 | + |
| 13 | +# How to configure Azure Database for MySQL Flexible Server data-out replication |
| 14 | + |
| 15 | +[[!INCLUDE[applies-to-mysql-flexible-server](../includes/applies-to-mysql-flexible-server.md)] |
| 16 | + |
| 17 | +This article describes how to set up Data-out replication in Azure Database for MySQL Flexible Server by configuring the source and replica servers. This article assumes that you have some prior experience with MySQL servers and databases. |
| 18 | + |
| 19 | +For Data-out replication, the source is always Azure Database for MySQL Flexible Server. The replica can be any external MySQL server on other cloud providers, on-premises, or virtual machines. Review the limitations and requirements of Data-out replication before performing the steps in this article. |
| 20 | + |
| 21 | +> [!NOTE] |
| 22 | +> This article references the term slave, a term that Microsoft no longer uses. When the term is removed from the software, we'll remove it from this article. |
| 23 | +
|
| 24 | +## Create an Azure Database for MySQL Flexible Server instance to use as a source. |
| 25 | + |
| 26 | +1. Create a new instance of Azure Database for MySQL Flexible Server (for example, sourceserver.mysql.database.Azure.com). Refer to [Create an Azure Database for MySQL Flexible Server server using the Azure portal for server creation](quickstart-create-server-portal.md). This server is the "source" server for Data-out replication. |
| 27 | + |
| 28 | +1. Create duplicate user accounts and corresponding privileges. |
| 29 | + 1. User accounts aren't replicated from the source server to the replica server. Suppose you plan on providing users with access to the replica server. In that case, you must manually create all accounts and corresponding privileges on this newly created Azure Database for MySQL Flexible Server. |
| 30 | + |
| 31 | +## Configure the source MySQL server |
| 32 | + |
| 33 | +The following steps prepare and configure the Azure Database for MySQL Flexible Server acting as the source. |
| 34 | + |
| 35 | +1. **Networking Requirements** |
| 36 | + |
| 37 | +Ensure that your network settings are established so that source and replica server can communicate seamlessly. |
| 38 | +If the source server is on public access, then ensure that firewall rules allow the replica server IP address. If the replica server is hosted on Azure, please ensure that you select the option of allowing public access from any Azure service from the networking blade in the Azure portal. |
| 39 | +If the source server is on private access, ensure that the replica server can connect to the source through Vnet peering or a VNet-to-VNet VPN gateway connection. |
| 40 | + |
| 41 | +> [!NOTE] |
| 42 | +> For more information - [Networking overview - Azure Database for MySQL Flexible Server](concepts-networking.md). |
| 43 | +
|
| 44 | +1. **Turn on binary logging** |
| 45 | + |
| 46 | +Check to see if binary logging has been enabled on the source by running the following command: |
| 47 | +SHOW VARIABLES LIKE 'log_bin'; |
| 48 | +If the variable log_bin is returned with the value "ON", binary logging is enabled on your server. |
| 49 | + |
| 50 | +1. **Create a new replication role and set up permission** |
| 51 | + |
| 52 | +Create a user account on the configured source server with replication privileges. This can be done through SQL commands or a tool such as MySQL Workbench. Consider whether you plan on replicating with SSL, as this will need to be specified when creating the user. Refer to the MySQL documentation to understand how to add user accounts on your source server. |
| 53 | +In the following commands, the new replication role can access the source from any machine, not just the one that hosts the source itself. This is done by specifying "syncuser@'%'" in the create user command. See the MySQL documentation to learn more about setting account names. |
| 54 | + |
| 55 | +**SQL Command** |
| 56 | + |
| 57 | +Replication with SSL |
| 58 | +To require SSL for all user connections, use the following command to create a user: |
| 59 | +SQLCopy |
| 60 | +CREATE USER 'syncuser'@'%' IDENTIFIED BY 'yourpassword'; |
| 61 | +GRANT REPLICATION SLAVE ON *.* TO ' syncuser'@'%' REQUIRE SSL; |
| 62 | +Replication without SSL |
| 63 | +If SSL isn't required for all connections, use the following command to create a user: |
| 64 | +SQLCopy |
| 65 | +CREATE USER 'syncuser'@'%' IDENTIFIED BY 'yourpassword'; |
| 66 | +GRANT REPLICATION SLAVE ON *.* TO ' syncuser'@'%'; |
| 67 | + |
| 68 | +**MySQL Workbench** |
| 69 | + |
| 70 | +To create the replication role in MySQL Workbench, open the Users and Privileges panel from the Management panel and select Add Account. |
| 71 | + |
| 72 | +:::image type="content" source="media/how-to-data-out-replication/mysql-workbench-add-account.png" alt-text="Screenshot of adding an account."::: |
| 73 | + |
| 74 | +Type the username into the Login Name field. |
| 75 | + |
| 76 | +:::image type="content" source="media/how-to-data-out-replication/mysql-workbench-login.png" alt-text="Screenshot of logging in on MySQL Workbench."::: |
| 77 | + |
| 78 | +Select the Administrative Roles panel and Replication Slave from the list of Global Privileges. Then select Apply to create the replication role. |
| 79 | + |
| 80 | +:::image type="content" source="media/how-to-data-out-replication/mysql-workbench-apply.png" alt-text="Screenshot of adding permissions."::: |
| 81 | + |
| 82 | +1. **Set the source server to read-only mode** |
| 83 | + |
| 84 | +Before starting to dump out the database, the server needs to be placed in read-only mode. While in read-only mode, the source cannot process any write transactions. Evaluate the impact on your business and schedule the read-only window in an off-peak time if necessary. |
| 85 | +FLUSH TABLES WITH READ LOCK; |
| 86 | +SET GLOBAL read_only = ON; |
| 87 | + |
| 88 | +1. **Get binary log file name and offset** |
| 89 | + |
| 90 | +Run the show master status command to determine the current binary log file name and offset. |
| 91 | +show master status; |
| 92 | + |
| 93 | +The results should appear similar to the following. Make sure to note the binary file name for use in later steps. |
| 94 | + |
| 95 | +:::image type="content" source="media/how-to-data-out-replication/mysql-workbench-result.png" alt-text="Screenshot of results."::: |
| 96 | + |
| 97 | +## Dump and restore the source server. |
| 98 | + |
| 99 | +Skip this section if it's a newly created source server with no existing data to migrate to the replica. You can, at this point, unlock the tables: |
| 100 | +SET GLOBAL read_only = OFF; |
| 101 | +UNLOCK TABLES; |
| 102 | + |
| 103 | +Follow the below steps if the source server has existing data to migrate to the replica. |
| 104 | + |
| 105 | +1. Determine which databases and tables you want to replicate into Azure Database for MySQL Flexible Server and perform the dump from the source server. |
| 106 | +You can use mysqldump to dump databases from your primary server. For details, refer to Dump & Restore. It's unnecessary to dump the MySQL library and test library. |
| 107 | + |
| 108 | +1. Set the source server to read/write mode. |
| 109 | +After dumping the database, change the source MySQL server back to read/write mode. |
| 110 | +SQLCopy |
| 111 | +SET GLOBAL read_only = OFF; |
| 112 | +UNLOCK TABLES; |
| 113 | + |
| 114 | +1. Restore dump file to new server. |
| 115 | +Restore the dump file to the server created in the Azure Database for MySQL Flexible Server service. Refer to Dump & Restore for restoring a dump file to a MySQL server. If the dump file is large, upload it to a virtual machine in Azure within the same region as your replica server. Restore it to the Azure Database for MySQL Flexible Server server from the virtual machine. |
| 116 | + |
| 117 | +> [!NOTE] |
| 118 | +> If you want to avoid setting the database to read only when you dump and restore, you can use mydumper/myloader. |
| 119 | +
|
| 120 | +## Configure the replica server to start Data-out replication. |
| 121 | + |
| 122 | +1. Filtering |
| 123 | +Suppose data-out replication is being set up between Azure MySQL and an external MySQL on other cloud providers or on-premises. In that case, you must use the replication filter to filter out Azure custom tables. This can be achieved by setting Replicate_Wild_Ignore_Table = "mysql.\_\_%" to filter the Azure mysql internal tables. To modify this parameter from the Azure portal, navigate to Azure Database for MySQL Flexible server used as source and select on "Server parameters" to view/edit the "Replicate_Wild_Ignore_Table" parameter. Refer to MySQL :: MySQL 5.7 Reference Manual :: 13.4.2.2 CHANGE REPLICATION FILTER Statement for more details on modifying this server parameter. |
| 124 | + |
| 125 | +1. Set the replica server by connecting to it and opening the MySQL shell on the replica server. From the prompt, run the following operation, which configures several MySQL replication settings at the same time: |
| 126 | +CHANGE THE REPLICATION SOURCE TO |
| 127 | +SOURCE_HOST='<master_host>', |
| 128 | +SOURCE_USER='<master_user>', |
| 129 | +SOURCE_PASSWORD='<master_password>', |
| 130 | +SOURCE_LOG_FILE='<master_log_file>, |
| 131 | +SOURCE_LOG_POS=<master_log_pos> |
| 132 | + |
| 133 | +- master_host: hostname of the source server (example – 'source.mysql.database.Azure.com') |
| 134 | +- master_user: username for the source server (example -'syncuser'@'%') |
| 135 | +- master_password: password for the source server |
| 136 | +- master_log_file: binary log file name from running show master status |
| 137 | +- master_log_pos: binary log position from running show master status |
| 138 | + |
| 139 | +> [!NOTE] |
| 140 | +> To use SSL for the connection, add the attribute SOURCE_SSL=1 to the command. For more information about using SSL in replication context, please refer - https://dev.mysql.com/doc/refman/8.0/en/change-replication-source-to.html |
| 141 | +
|
| 142 | +1. Activate the replica server using the following command. |
| 143 | + START REPLICA; |
| 144 | +At this point, the replica instance will begin replicating any changes made to the source server database. You can test this by creating a sample table on your source database and checking whether it gets replicated successfully. |
| 145 | + |
| 146 | +1. Check replication status. |
| 147 | +Call the show slave status\G command on the replica server to view the replication status. |
| 148 | +Show slave status; |
| 149 | +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 indicates how late the replica is. If the value isn't "0", it means that the replica is processing updates. |
| 150 | + |
| 151 | +If the replica server is hosted in an Azure VM, set "Allow access to Azure services" to "ON" on the source to allow the source and replica servers to communicate. This setting can be changed from the Connection security options. For more information, see Manage firewall rules using the portal. |
| 152 | + |
| 153 | +If you used mydumper/myloader to dump the database, you could get the master_log_file and master_log_pos from the /backup/metadata file. |
| 154 | + |
| 155 | +Next steps |
| 156 | +Learn more about Data-out replication (hyperlinked to data-out concepts page) for Azure Database for MySQL Flexible Server. |
0 commit comments