Amazon DocumentDB is a fully managed NoSQL document database service that’s designed to be compatible with MongoDB. This means you can use your existing MongoDB drivers and tools to interact with Amazon DocumentDB. To successfully Migrate MongoDB to DocumentDB, careful planning and execution are crucial, particularly when managing large datasets or sensitive production systems.
In this blog post, we’ll guide you through the complete process How to Migrate MongoDB to Amazon DocumentDB, including setting up the connection securely using SSL certificates (–sslCAFile), and transferring your data using tools like mongodump and mongorestore.
Why Migrate from MongoDB to Amazon DocumentDB?
Before diving into the migration steps, it’s important to understand why you might consider moving from MongoDB to Amazon DocumentDB:
- Fully Managed Service: Amazon DocumentDB is a fully managed service, meaning Amazon takes care of operational tasks such as backups, patching, and scaling, which reduces the administrative burden on your team.
- Scalability: Amazon DocumentDB automatically scales based on your usage, with the ability to easily add or remove instances to handle growing workloads.
- MongoDB Compatibility: Amazon DocumentDB is designed to be compatible with MongoDB, so you can continue using the same MongoDB tools, libraries, and drivers, making the migration process smoother.
- Security and Performance: With built-in encryption, fine-grained access control, and integration with AWS services like CloudWatch, DocumentDB provides robust security and performance monitoring.
Key Considerations Before Migration
- Version Compatibility: Amazon DocumentDB supports MongoDB versions 3.6 and 4.0 APIs. Make sure your MongoDB version aligns with DocumentDB’s supported version to avoid compatibility issues.
- Feature Limitations: While DocumentDB supports a lot of MongoDB’s features, there are certain limitations, particularly around the aggregation framework, sharding, and some advanced MongoDB features. Check Amazon’s DocumentDB documentation for a full list of supported MongoDB features.
- Security: Ensure that your connection to DocumentDB is secure by using SSL/TLS encryption. Amazon DocumentDB requires SSL for all connections, and using the –sslCAFile option ensures the validity of the server’s SSL certificate.
- Data Integrity: Validate your data after migration to ensure nothing has been corrupted during the transfer. Always back up your data before starting the migration process.
Migrate MongoDB to Amazon DocumentDB
Set Up Amazon DocumentDB Cluster
Create an Amazon DocumentDB Cluster:
Log in to the AWS Management Console and navigate to Amazon DocumentDB. Click on Create Cluster to set up your DocumentDB cluster.
Cluster Configuration:
Choose the right instance size based on your expected workload.
Set up a VPC, subnets, and security groups that allow access to the DocumentDB cluster.
Ensure that your DocumentDB cluster is configured to support MongoDB version 3.6 or 4.0 (Amazon DocumentDB is compatible with these versions).
Cluster Endpoint:
After creating the cluster, note down the cluster endpoint, port (default is 27017), username, and password for connecting to your DocumentDB cluster.
Prepare MongoDB Data for Migration
Before migrating, you need to create a backup of your existing MongoDB data.
Backup MongoDB Data Using mongodump:
Use the mongodump tool to create a backup of your MongoDB database. Here’s the command:
mongodump --host <mongodb_hos:27017> --ssl --sslCAFile global-bundle.pem --username <your_username> --password --db <your_database_name> --out <backup_directory>
<mongodb_host>: The hostname or IP address of your MongoDB server.
<mongodb_port>: The port number (default is 27017).
<your_username> and <your_password>: MongoDB authentication credentials.
<backup_directory>: The directory where the backup will be stored.
Ensure Backup Completeness:
Verify that all collections and documents are successfully backed up by inspecting the contents of the backup directory.
Configure SSL Connection to Amazon DocumentDB
Since Amazon DocumentDB requires SSL connections, you must download the global SSL certificate (CA file) to securely connect to the cluster.
Download the SSL Certificate:
Download the Amazon DocumentDB SSL certificate from the following link:
Amazon DocumentDB CA Bundle
Save the Certificate:
Save the certificate file as global-bundle.pem to your local machine. You’ll use this certificate to establish the secure connection between your MongoDB client and DocumentDB.
Verify SSL:
Once the certificate is downloaded, ensure that you reference this CA file in all subsequent MongoDB commands to ensure the connection is encrypted and validated.
Migrate Data Using mongorestore
Now that you have the backup and SSL certificate ready, you can restore the data to your Amazon DocumentDB cluster using mongorestore.
Restore the Backup to DocumentDB:
Here’s the command to restore data using mongorestore, including the –sslCAFile option for SSL encryption:
mongorestore --host <docdb_endpoint> --port 27017 --ssl --sslCAFile /path/to/global-bundle.pem --username <your_username> -d <your_database> <backup_directory>
<docdb_endpoint>: Replace with the endpoint of your Amazon DocumentDB cluster (e.g., docdb-cluster.cluster-xyz123.us-west-2.docdb.amazonaws.com).
/path/to/global-bundle.pem: Replace with the file path to the global-bundle.pem SSL certificate you downloaded earlier.
<your_username> and <your_password>: Provide your DocumentDB credentials.
<your_database>: Replace with the name of the database you’re restoring.
<backup_directory>: The directory where the MongoDB backup is stored.
Verify Data Migration:
After completing the restoration, verify the data transfer by connecting to your Amazon DocumentDB instance.
mongo –host <docdb_endpoint> –port 27017 –ssl –sslCAFile /path/to/global-bundle.pem –username <your_username> –password <your_password> –authenticationDatabase admin
Run MongoDB commands like show dbs, show collections, and query some data to verify the migration was successful.
If you need help to Migrate MongoDB to DocumentDB or face any issues during the process, contact our support team for expert assistance!