Migrating MySql from EC2 to RDS in AWS

Previously when databases were created in AWS, they were placed on the EC2 instances. Later, as AWS came up with RDS, a dedicated service for databases, it is preferable to maintain databases on RDS rather than on EC2’s anymore. So, all the new databases which are created now in AWS are placed in RDS.

So, what about the databases which are on EC2’s?
They should be migrated to RDS. Simple isn’t it.

Based on the scenarios, the migration plan can vary. Let us discuss a plan on how we can achieve this –

Scenario – You have your active production database on EC2 and you want to migrate it to RDS.

Steps –

  1. Take a snapshot/backup of the database on the EC2. This can be achieved used ‘percona’. Refer
    https://www.percona.com/doc/percona-xtrabackup/2.4/index.html
  2. Note down the binlog position of the backup.
  3. Send the backup to S3, an AWS storage service.
  4. Now, you can ‘Restore a DB‘ to RDS from the backup stored in S3, using the AWS RDS console/CLI. Refer
    https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/MySQL.Procedural.Importing.html
  5. Once the DB is restored to RDS. Login to the DB on RDS and make it a slave to the DB on EC2 by using the binlogs you stored in STEP 2.
  6. Once the RDS is a slave to the DB on EC2, check if the lag time is ‘0’.
  7. So, now both the DB’s are in sync.
  8. Ask your clients for a maintenance window where the application will be temporarily down.
  9. In that maintenance window -Check for ‘0’ lag time, Stop DB on EC2 and turn the traffic to the DB on RDS.
  10. Once your application is UP, all the traffic is now pointed to RDS.
  11. Confirm that the new DB is working properly as planned and shut down the DB on EC2.

Important note –

  1. Don’t shut down the DB on EC2 until you perform all the tests on RDS.
  2. Do this for a non-prod DB and then for a prod DB to eliminate risks.
  3. Choose the RDS parameters carefully based on the DB parameters on EC2.

There can be different strategies that can be followed to do the migration based on the specific scenario. One piece of advice- ‘Be careful when playing with DataBases else you would have a sleepless weekend 😛‘.

Advertisement

Published by Ritesh Kumar Reddy

I(Ritesh) work as a Sr. Cloud Engineer for a living. Learning new technologies has always been my hobby. Why not share it? Here is the brainchild – blogging to share the knowledge. This blog is for those who wish to start or already into the Cloud field. Each article briefly talks about a tool/technology that is used in the Cloud model. Once you read the article, I hope, you get a kick start regarding the specific tool/technology.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: