AWS DMS Database Migration Example: A Step-by-Step Guide

AWS Database Migration Service (DMS) simplifies migrating databases to AWS by minimizing downtime and enabling secure data transfer. With AWS DMS, you can migrate databases between different engines or replicate data for analytics and other use cases.

This article provides a practical example of using AWS DMS to migrate a MySQL database to Amazon RDS for MySQL.


What Is AWS DMS?

AWS DMS enables:

  • Database Migration: Migrate databases between on-premises, AWS, and different database engines.
  • Low Downtime: Allows migrations while keeping the source database operational.
  • Replication: Supports ongoing replication for analytics or disaster recovery.

AWS DMS Database Migration Example

Objective

We’ll migrate a MySQL database hosted on-premises or EC2 to an Amazon RDS for MySQL instance.


Step 1: Prepare the Source and Target Databases

Source Database

  1. Ensure you have a MySQL database running on-premises or on an EC2 instance.
  2. Confirm the database is accessible from the AWS DMS replication instance.

Create a user with replication privileges:

CREATE USER 'dms_user'@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'dms_user'@'%';
FLUSH PRIVILEGES;

Target Database

  1. Create an Amazon RDS for MySQL instance:
    • Use the AWS Management Console to launch an RDS instance with MySQL as the engine.
    • Note the endpoint, username, and password for the target database.
  2. Prepare the target schema:
    • Manually create the schema and tables in the target database, or allow AWS DMS to create them during migration.

Step 2: Create a DMS Replication Instance

  1. Navigate to the AWS DMS Console:
    • Go to AWS Management ConsoleDatabase Migration ServiceReplication Instances.
  2. Create Replication Instance:
    • Name: Provide a descriptive name (e.g., dms-replication-instance).
    • Instance Class: Choose an instance type (e.g., dms.t3.medium for small workloads).
    • VPC: Select the VPC where your source and target databases reside.
    • Create the instance and wait for it to become available.

Step 3: Configure Source and Target Endpoints

Source Endpoint

  1. Navigate to EndpointsCreate Endpoint.
  2. Endpoint Type: Source.
  3. Database Engine: MySQL.
  4. Connection Information:
    • Provide the source database endpoint, port, username, and password.
    • Test the connection to verify access.

Target Endpoint

  1. Create another endpoint for the target database.
  2. Endpoint Type: Target.
  3. Database Engine: MySQL.
  4. Connection Information:
    • Provide the RDS endpoint, port, username, and password.
    • Test the connection to ensure connectivity.

Step 4: Create a Migration Task

  1. Navigate to TasksCreate Task.
  2. Task Settings:
    • Task Name: Provide a name (e.g., mysql-to-rds-task).
    • Source Endpoint: Select the source endpoint created earlier.
    • Target Endpoint: Select the target endpoint.
    • Migration Type: Choose Migrate existing data and replicate ongoing changes.
  3. Table Mappings:
  4. Start Task:
    • Enable the task to start immediately.
    • Review and create the task.

Specify the tables to migrate. For all tables, use:

{
  "rules": [
    {
      "rule-type": "selection",
      "rule-id": "1",
      "rule-name": "includeAllTables",
      "object-locator": {
        "schema-name": "%",
        "table-name": "%"
      },
      "rule-action": "include"
    }
  ]
}

Step 5: Monitor the Migration

  1. Task Progress:
    • View the task status in the DMS Console.
    • Monitor the progress of data migration and replication.
  2. CloudWatch Metrics:
    • Use CloudWatch to monitor replication instance performance, data transfer rates, and errors.

Step 6: Verify the Migration

  1. Validate schema and data consistency between the source and target databases.

Query the target database to ensure data is migrated successfully:

SELECT COUNT(*) FROM <table_name>;

Step 7: Clean Up Resources

  • Stop the DMS task and replication instance if no longer needed.
  • Delete the DMS resources to avoid additional costs.

Best Practices for AWS DMS

  1. Use Pre-Migration Assessments:
    • Assess your database for compatibility issues using AWS Schema Conversion Tool.
  2. Enable Logging:
    • Enable CloudWatch logging to troubleshoot migration issues.
  3. Optimize Instance Size:
    • Choose an appropriately sized replication instance based on your workload.
  4. Test Migration:
    • Perform test migrations on non-production databases to validate configurations.

Conclusion

AWS DMS provides an efficient and reliable way to migrate databases with minimal downtime. This example demonstrated how to migrate a MySQL database to Amazon RDS for MySQL using DMS. By following this process, you can streamline database migrations and leverage AWS’s managed database services to optimize performance and scalability.