Migrating MySQL From EC2 to RDS in AWS

When databases were created in AWS, they were placed on the EC2 instances. As AWS came up with RDS, a dedicated service for databases, it was preferable to maintain databases on RDS rather than on EC2s. As a result, all new databases that were created in AWS are placed in RDS.

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

Based on these 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 using a ‘percona’. Refer to – 
    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 to 
    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. Both DBs should be 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 may be different strategies that can be followed to migrate based on specific scenarios. One piece of advice- ‘Be careful when playing with Databases else you will have sleepless weekends!!’

Leave a Comment