How to Set Up Replication Between AWS Aurora and an External MySQL Instance
How to Set Up Replication Between AWS Aurora and an External MySQL Instance
In this article, I will share simple step-by-step instructions on how to set up a replication between AWS Aurora and an external MySQL instance.
Join the DZone community and get the full member experience.
Join For FreeDownload the Scale-Out and High Availability whitepaper. Learn why leading enterprises choose the Couchbase NoSQL database over MongoDB™ after evaluating side by side.
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 article, 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:
- 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:
- From now on we will make all backups from the restored snapshot. First get all users and import those to the new instance:
- Restore to the local database:
- Restore users again (some users may fail to create where there are missing databases):
- Download the RDS/Aurora SSL certificate:
- 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:
- 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.
Binlog position from crash recovery is mysql-bin-changelog.000708 31278857
# mysql_tzinfo_to_sql /usr/share/zoneinfo/|mysql
Sample config:
[mysqld]
log-bin=log-bin
log-slave-updates
binlog-format=MIXED
server-id=1000
relay-log=relay-bin
innodb_log_file_size=1G
innodb_buffer_pool_size=2G
innodb_flush_method=O_DIRECT
innodb_flush_log_at_trx_commit=0 # as this is replication slave
pt-show-grants -h myhost...amazonaws.com -u percona > grants.sql
# check that grants are valid and upload to MySQL
mysql -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):
host="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
mysql -h localhost < all.sql
mysql -f < grants.sql
# cd /etc/ssl
# wget 'https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem'
# chown mysql.mysql rds-combined-ca-bundle.pem
# mysql -h localhost
...
mysql> CHANGE MASTER TO
MASTER_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;
I hope those steps will be helpful for setting up an external MySQL replica.
Learn how to scale enterprise applications easily, efficiently, and reliably with NoSQL. See why Couchbase beats MongoDB™ for scale-out and high availability.
Published at DZone with permission of Alexander Rubin , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}