On-prem MySQL 8 Replicate to RDS
Scenario
- Version information
- On-premises MySQL 8.0.37 on Ubuntu 24.04 (using default installation method)
- AWS RDS for MySQL 8.0.35
- Replicate on-premises MySQL 8 to AWS RDS MySQL 8 slave database using native replication functionality
- On-premises MySQL user authentication method defaults to
caching_sha2_password - As AWS RDS for MySQL 8 still uses
mysql_native_passwordauthentication by default, replication setup will fail - Need to create account using
mysql_native_passwordmethod, or adjust tomysql_native_passwordauthentication after creation
- On-premises MySQL user authentication method defaults to
- This case simulates on-premises MySQL master database using EC2, replicating database to RDS slave database
- Ensure EC2 can connect to RDS host
Process and Explanation
- Install and configure on-premises Master MySQL 8
- Create Slave RDS for MySQL 8
- Data migration
- Configure RDS replication
- Verify replication status and troubleshooting
Execution Steps
Install and configure on-premises Master MySQL 8
Install Master MySQL 8
Install MySQL 8, which is available by default in Ubuntu 24.04 repo
1sudo apt update2sudo apt install mysql-server -y3sudo systemctl enable mysql4sudo systemctl start mysql5# (Optional) Set password, mandatory in production environments6sudo mysql_secure_installationConfigure Master MySQL 8
Configure MySQL 8 configuration file as Master, modify relevant settings in the configuration file
- Usually choose either binlog_do_db or binlog_ignore_db parameter
- binlog_do_db: Databases to replicate, defaults to all if not specified
- binlog_ignore_db: Databases to ignore for replication, defaults to none if not specified
- Method for recording multiple databases as shown in the example below
1# Only lab1 and lab2 databases will be replicated, others won't sync2binlog_do_db = lab13binlog_do_db = lab24
5# Only lab1 and lab2 databases won't be replicated, others will sync6binlog_ignore_db = lab17binlog_ignore_db = lab2Below is a complete configuration example, adjust as needed
1# * Basic Settings2server-id = 1013auto-increment-increment = 24auto-increment-offset = 25# bind external address6bind-address = 0.0.0.07# log-bin enable8log_bin = /var/log/mysql/mysql-bin.log9expire_logs_days = 310max_binlog_size = 100M11binlog_format = mixed12# Adjust the following as needed13binlog_do_db = dbo14# binlog_ignore_db = lab2After configuration, restart MySQL service
1sudo systemctl restart mysqlYou can see that caching_sha2_password is used for authentication by default
1show variables like 'default_authentication_plugin';2+-------------------------------+-----------------------+3| Variable_name | Value |4+-------------------------------+-----------------------+5| default_authentication_plugin | caching_sha2_password |6+-------------------------------+-----------------------+71 row in set (0.00 sec)Create Master MySQL 8 replication account
- Create a dedicated replication account
- Replace
%with specific source IP for increased security - Replace
replicatorwith another account name - Replace
replicatorpasswith another password - Create account using
mysql_native_passwordauthentication method
- Replace
- Set account replication permissions:
REPLICATION CLIENTandREPLICATION SLAVE
1CREATE USER 'replicator'@'%' IDENTIFIED WITH mysql_native_password BY 'replicatorpass';2GRANT REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'replicator'@'%';3FLUSH PRIVILEGES;Create Slave RDS for MySQL 8
Use AWS GUI interface to create, and ensure EC2 host can connect to RDS host
Data migration
Export the database from EC2. If there’s no data, you can use MySQL official sample database. Before exporting, perform the following operations
Lock Master MySQL database
1FLUSH TABLES WITH READ LOCK;2SET GLOBAL read_only = ON;Check binlog status information
Record the File and Position information for later use, and verify if Binlog_Do_DB is correct
1SHOW MASTER STATUS\G;;2
3# Expected result4*************************** 1. row ***************************5 File: mysql-bin.0000016 Position: 17237 Binlog_Do_DB: dbo8 Binlog_Ignore_DB:9Executed_Gtid_Set:101 row in set (0.00 sec)Data export and import
- Export the database to be synchronized. You can use commands provided by official documentation or other tools for export
- Then import the exported data into RDS for MySQL, using commands or other third-party tools
Unlock Master MySQL database
1SET GLOBAL read_only = OFF;2UNLOCK TABLES;Configure RDS replication
Fill in the information obtained from SHOW MASTER STATUS earlier into the RDS for MySQL command
master_hostis the IP or domain name of the host- For more RDS for MySQL configuration parameters, refer to this official documentation
1CALL mysql.rds_set_external_master ('master_host', 3306, 'replicator', 'replicatorpass', 'mysql-bin.000001', 1723, 0);2CALL mysql.rds_start_replication;3+-----------------------------------------------------+4| Message |5+-----------------------------------------------------+6| Replication started. Slave is now running normally. |7+-----------------------------------------------------+81 row in set (3.02 sec)Verify replication status and troubleshooting
Confirm successful synchronization status
Connect to the Slave database (RDS for MySQL), enter the SHOW SLAVE STATUS\G; command to check synchronization status
- Both
Slave_IO_RunningandSlave_SQL_Runningstatuses should beYes Last_ErrorandLast_IO_Errorshould both be empty- Pay attention to the values of
Read_Master_Log_Pos,Exec_Master_Log_Pos, andRelay_Log_Pos
1SHOW SLAVE STATUS\G;2*************************** 1. row ***************************3 Slave_IO_State: Waiting for master to send event4 Master_Host: 10.250.1.415 Master_User: replicator6 Master_Port: 33067 Connect_Retry: 608 Master_Log_File: mysql-bin.0000019 Read_Master_Log_Pos: 319910 Relay_Log_File: relaylog.00001711 Relay_Log_Pos: 32612 Relay_Master_Log_File: mysql-bin.00000113 Slave_IO_Running: Yes14 Slave_SQL_Running: Yes15 Replicate_Do_DB:16 Replicate_Ignore_DB:17 Replicate_Do_Table:18 Replicate_Ignore_Table: innodb_memcache.cache_policies,innodb_memcache.config_options,mysql.plugin,mysql.rds_configuration,mysql.rds_history,mysql.rds_monitor,mysql.rds_replication_status,mysql.rds_sysinfo,mysql.rds_upgrade_prechecks19 Replicate_Wild_Do_Table:20 Replicate_Wild_Ignore_Table:21 Last_Errno: 022 Last_Error:23 Skip_Counter: 024 Exec_Master_Log_Pos: 319925 Relay_Log_Space: 90126 Until_Condition: None27 Until_Log_File:28 Until_Log_Pos: 029 Master_SSL_Allowed: No30 Master_SSL_CA_File:31 Master_SSL_CA_Path:32 Master_SSL_Cert:33 Master_SSL_Cipher:34 Master_SSL_Key:35 Seconds_Behind_Master: 036Master_SSL_Verify_Server_Cert: No37 Last_IO_Errno: 038 Last_IO_Error:39 Last_SQL_Errno: 040 Last_SQL_Error:41 Replicate_Ignore_Server_Ids:42 Master_Server_Id: 10143 Master_UUID: fbba8d94-4e28-11ef-b9c6-06b99b39a34d44 Master_Info_File: mysql.slave_master_info45 SQL_Delay: 046 SQL_Remaining_Delay: NULL47 Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates48 Master_Retry_Count: 8640049 Master_Bind:50 Last_IO_Error_Timestamp:51 Last_SQL_Error_Timestamp:52 Master_SSL_Crl:53 Master_SSL_Crlpath:54 Retrieved_Gtid_Set:55 Executed_Gtid_Set:56 Auto_Position: 057 Replicate_Rewrite_DB:58 Channel_Name:59 Master_TLS_Version:60 Master_public_key_path:61 Get_master_public_key: 062 Network_Namespace:631 row in set, 1 warning (0.00 sec)- Test by adding some data. In addition to verifying table synchronization, you can enter
SHOW SLAVE STATUS\G;to confirm if synchronization is successful - You will notice that the values of
Read_Master_Log_Pos,Exec_Master_Log_Pos, andRelay_Log_Poswill update accordingly
1mysql> SHOW SLAVE STATUS\G;2*************************** 1. row ***************************3 Slave_IO_State: Waiting for master to send event4 Master_Host: 10.250.1.415 Master_User: replicator6 Master_Port: 33067 Connect_Retry: 608 Master_Log_File: mysql-bin.0000019 Read_Master_Log_Pos: 356710 Relay_Log_File: relaylog.00001711 Relay_Log_Pos: 69412 Relay_Master_Log_File: mysql-bin.00000113 Slave_IO_Running: Yes14 Slave_SQL_Running: Yes15 Replicate_Do_DB:16 Replicate_Ignore_DB:17 Replicate_Do_Table:18 Replicate_Ignore_Table: innodb_memcache.cache_policies,innodb_memcache.config_options,mysql.plugin,mysql.rds_configuration,mysql.rds_history,mysql.rds_monitor,mysql.rds_replication_status,mysql.rds_sysinfo,mysql.rds_upgrade_prechecks19 Replicate_Wild_Do_Table:20 Replicate_Wild_Ignore_Table:21 Last_Errno: 022 Last_Error:23 Skip_Counter: 024 Exec_Master_Log_Pos: 356725 Relay_Log_Space: 126926 Until_Condition: None27 Until_Log_File:28 Until_Log_Pos: 029 Master_SSL_Allowed: No30 Master_SSL_CA_File:31 Master_SSL_CA_Path:32 Master_SSL_Cert:33 Master_SSL_Cipher:34 Master_SSL_Key:35 Seconds_Behind_Master: 036Master_SSL_Verify_Server_Cert: No37 Last_IO_Errno: 038 Last_IO_Error:39 Last_SQL_Errno: 040 Last_SQL_Error:41 Replicate_Ignore_Server_Ids:42 Master_Server_Id: 10143 Master_UUID: fbba8d94-4e28-11ef-b9c6-06b99b39a34d44 Master_Info_File: mysql.slave_master_info45 SQL_Delay: 046 SQL_Remaining_Delay: NULL47 Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates48 Master_Retry_Count: 8640049 Master_Bind:50 Last_IO_Error_Timestamp:51 Last_SQL_Error_Timestamp:52 Master_SSL_Crl:53 Master_SSL_Crlpath:54 Retrieved_Gtid_Set:55 Executed_Gtid_Set:56 Auto_Position: 057 Replicate_Rewrite_DB:58 Channel_Name:59 Master_TLS_Version:60 Master_public_key_path:61 Get_master_public_key: 062 Network_Namespace:631 row in set, 1 warning (0.01 sec)Troubleshooting
Authentication requires secure connection
- Error appears:
Authentication plugin' caching_sha2_password' reported error: Authentication requires secure connection.
1mysql> SHOW SLAVE STATUS\G;2*************************** 1. row ***************************3 Slave_IO_State: Connecting to master4 Master_Host: 10.250.1.415 Master_User: replicator6 Master_Port: 33067 Connect_Retry: 608 Master_Log_File: mysql-bin.0000019 Read_Master_Log_Pos: 172310 Relay_Log_File: relaylog.00000111 Relay_Log_Pos: 412 Relay_Master_Log_File: mysql-bin.00000113 Slave_IO_Running: Connecting14 Slave_SQL_Running: Yes15 Replicate_Do_DB:16 Replicate_Ignore_DB:17 Replicate_Do_Table:18 Replicate_Ignore_Table: innodb_memcache.cache_policies,innodb_memcache.config_options,mysql.plugin,mysql.rds_configuration,mysql.rds_history,mysql.rds_monitor,mysql.rds_replication_status,mysql.rds_sysinfo,mysql.rds_upgrade_prechecks19 Replicate_Wild_Do_Table:20 Replicate_Wild_Ignore_Table:21 Last_Errno: 022 Last_Error:23 Skip_Counter: 024 Exec_Master_Log_Pos: 172325 Relay_Log_Space: 198426 Until_Condition: None27 Until_Log_File:28 Until_Log_Pos: 029 Master_SSL_Allowed: No30 Master_SSL_CA_File:31 Master_SSL_CA_Path:32 Master_SSL_Cert:33 Master_SSL_Cipher:34 Master_SSL_Key:35 Seconds_Behind_Master: NULL36Master_SSL_Verify_Server_Cert: No37 Last_IO_Errno: 206138 Last_IO_Error: Error connecting to source '[email protected]:3306'. This was attempt 1/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.39 Last_SQL_Errno: 040 Last_SQL_Error:41 Replicate_Ignore_Server_Ids:42 Master_Server_Id: 043 Master_UUID:44 Master_Info_File: mysql.slave_master_info45 SQL_Delay: 046 SQL_Remaining_Delay: NULL47 Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates48 Master_Retry_Count: 8640049 Master_Bind:50 Last_IO_Error_Timestamp: 240730 07:11:0951 Last_SQL_Error_Timestamp:52 Master_SSL_Crl:53 Master_SSL_Crlpath:54 Retrieved_Gtid_Set:55 Executed_Gtid_Set:56 Auto_Position: 057 Replicate_Rewrite_DB:58 Channel_Name:59 Master_TLS_Version:60 Master_public_key_path:61 Get_master_public_key: 062 Network_Namespace:631 row in set, 1 warning (0.00 sec)- We can see that the
replicatoraccount usescaching_sha2_passwordauthentication
1SELECT user, host, plugin FROM mysql.user;2+------------------+-----------+-----------------------+3| user | host | plugin |4+------------------+-----------+-----------------------+5| admin | % | caching_sha2_password |6| replicator | % | caching_sha2_password |7| debian-sys-maint | localhost | caching_sha2_password |8| mysql.infoschema | localhost | caching_sha2_password |9| mysql.session | localhost | caching_sha2_password |10| mysql.sys | localhost | caching_sha2_password |11| root | localhost | auth_socket |12+------------------+-----------+-----------------------+137 rows in set (0.00 sec)- Use the following command to adjust the authentication method for the replication account, then confirm the effective status
1ALTER USER 'replicator'@'%' IDENTIFIED WITH mysql_native_password BY 'replicatorpass';2SELECT user, host, plugin FROM mysql.user;3+------------------+-----------+-----------------------+4| user | host | plugin |5+------------------+-----------+-----------------------+6| admin | % | caching_sha2_password |7| replicator | % | mysql_native_password |8| debian-sys-maint | localhost | caching_sha2_password |9| mysql.infoschema | localhost | caching_sha2_password |10| mysql.session | localhost | caching_sha2_password |11| mysql.sys | localhost | caching_sha2_password |12| root | localhost | auth_socket |13+------------------+-----------+-----------------------+147 rows in set (0.00 sec)