How to Set Up Replication Between AWS Aurora and an External MySQL InstanceAlexander Rubin
Amazon RDS Aurora (MySQL) provides its own low latency replication. Nevertheless, there are cases where it can be beneficial to set up replication from Aurora to an external MySQL server, as Amazon RDS Aurora is based on MySQL and supports native MySQL replication. Here are some examples of when replicating from Amazon RDS Aurora to an external MySQL server can make good sense:
- Replicating to another cloud or datacenter (for added redundancy)
- Need to use an independent reporting slave
- Need to have an additional physical backup
- Need to use another MySQL flavor or fork
- Need to failover to another cloud and back
In this blog post I will share simple step by step instructions on how to do it.
Steps to setup MySQL replication from AWS RDS Aurora to MySQL server
- Enable binary logs in the option group in Aurora (Binlog format = mixed). This will require a restart.
- Create a snapshot and restore it (create a new instance from a snapshot). This is only needed to make a consistent copy with mysqldump. As Aurora does not allow “super” privileges, running mysqldump --master-data is not possible. The snapshot is the only way to get a consistent backup with the specific binary log position.
- Get the binary log information from the snapshot. In the console, look for the “Alarms and Recent Events” for the restored snapshot instance. We should see something like:
Shell1Binlog position from crash recovery is mysql-bin-changelog.000708 31278857
- Install MySQL 5.6 (i.e. Percona Server 5.6) on a separate EC2 instance (for Aurora 5.6 – note that you should use MySQL 5.7 for Aurora 5.7). After MySQL is up and running, import the timezones:
Shell1# mysql_tzinfo_to_sql /usr/share/zoneinfo/|mysql
MySQL1234567891011[mysqld]log-bin=log-binlog-slave-updatesbinlog-format=MIXEDserver-id=1000relay-log=relay-bininnodb_log_file_size=1Ginnodb_buffer_pool_size=2Ginnodb_flush_method=O_DIRECTinnodb_flush_log_at_trx_commit=0 # as this is replication slave
- From now on we will make all backups from the restored snapshot. First get all users and import those to the new instance:
Shell1pt-show-grants -h myhost...amazonaws.com -u percona > grants.sql
# check that grants are valid and upload to MySQL
Shell1mysql -f < grants.sql
Make a backup of all schemas except for the “mysql” system tables as Aurora using different format of those (make sure we connect to the snapshot):
Shell12345host="my-snapshot...amazonaws.com"mysqldump --single-transaction -h $host -u percona--triggers --routines--databases `mysql -u percona -h $host -NBe"select group_concat(schema_name separator ' ') from information_schema.schemata where schema_name not in ('mysql', 'information_schema', 'performance_schema')"` > all.sql
- Restore to the local database:
Shell1mysql -h localhost < all.sql
- Restore users again (some users may fail to create where there are missing databases):
Shell1mysql -f < grants.sql
- Download the RDS/Aurora SSL certificate:
Shell123# cd /etc/ssl# wget 'https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem'# chown mysql.mysql rds-combined-ca-bundle.pem
- Configure MySQL replication. Take the values for the binary log name and position from #3 above. Please note: now we connect to the actual instance, not a snapshot:
Shell123456789101112131415# mysql -h localhost...mysql> CHANGE MASTER TOMASTER_HOST='dev01-aws-1...',MASTER_USER='awsreplication',MASTER_PASSWORD='<pass>',MASTER_LOG_FILE = 'mysql-bin-changelog.000708',MASTER_LOG_POS = 31278857,MASTER_SSL_CA = '/etc/ssl/rds-combined-ca-bundle.pem',MASTER_SSL_CAPATH = '',MASTER_SSL_VERIFY_SERVER_CERT=1;mysql> start slave;
- Verify that the slave is working. Optionally add the SQL_Delay option to the CHANGE MASTER TO (or anytime) and specify the slave delay in seconds.
I hope those steps will be helpful for setting up an external MySQL replica.