Migrating Data to MongoDB Atlas

Editor's Note: Migrating data to MongoDB Atlas is easier than ever with the Live Import tool. Simply select the destination cluster you'd like to import data to, and follow the instructions to seamlessly pull in an existing MongoDB deployment. Learn more here. MongoDB Atlas was announced at this year's MongoDB World. It's great not just for new applications, but also your existing MongoDB databases running on other platforms. This post will focus on how you migrate your data and applications over to MongoDB Atlas. What is MongoDB Atlas? MongoDB Atlas provides all of the features of MongoDB, without the operational heavy lifting required for any new application. MongoDB Atlas is available on demand through a pay-as-you-go model and billed on an hourly basis, letting you focus on what you do best. It’s easy to get started – use a simple GUI to select the instance size, region, and features you need. MongoDB Atlas provides: Security features to protect access to your data Built in replication for always-on availability, tolerating complete data center failure Backups and point in time recovery to protect against data corruption Fine-grained monitoring to let you know when to scale. Additional instances can be provisioned with the push of a button Automated patching and one-click upgrades for new major versions of the database, enabling you to take advantage of the latest and greatest MongoDB features A choice of cloud providers, regions, and billing options But what if you already have application data held in your own on-prem or cloud-based MongoDB database – is it possible to safely migrate that data to MongoDB Atlas? What if your data is held in a 3rd party hosted MongoDB service such as Compose or mLab? Conversely, is it possible to build your application against MongoDB Atlas and then move the data to a MongoDB database running on another platform in the future? The answer to all of those questions is "yes". In the future you should expect this to be a highly automated process but right now it involves some manual steps – the purpose of this blog post is to describe the process. Moving Your Application Data to MongoDB Atlas The procedure is very straightforward, but if you can't tolerate losing any of your updates then it does involve stopping application writes for a period. That means it's vital that you prepare in advance in order to minimize the impact. Pre-Migration Checklist How long will writes need to be stopped? Perform a dry-run of the mongodump & mongorestore steps but without stopping application writes to answer this. When will the stopping of writes have the smallest impact? What can you change in the application to minimize the impact, e.g. provide a read-only version of the service when it isn't possible to write to the database? Will you warn users of planned maintenance ahead of time? Do you have sufficient storage space to store the dumped data on the machine where you plan to run mongodump ? Once the data has been migrated to MongoDB Atlas, the application will need to switch its database connections to the new address; identify how this will be done. List the IP Addresses of all the machines that will need to connect to MongoDB Atlas – this includes your application nodes as well as the machine where mongorestore will be run. These will need to be added to your MongoDB Atlas group's whitelist. Decide on what MongoDB Atlas instance size to use and, if necessary how many shards will be needed. Decide on which region to use, e.g. co locating the MongoDB Atlas instances with your cloud-based application servers. Execute the Migration Create the MongoDB Atlas cluster . Add the required IP Addresses to the whitelist in your group's security tab. Stop database writes to your existing database; either in your application logic or by blocking them for each of your databases (schemas) in the original MongoDB deployment: laptop> mongo --host=ec2-52-208-185-213.eu-west-1.compute.amazonaws.com \ --eval "db.fsyncLock()" Back up the data from the existing database (writes the data to a directory named dump ): laptop> mongodump --host=ec2-52-208-185-213.eu-west-1.compute.amazonaws.com \ --port=27017 Write the data to MongoDB Atlas (using the connection information provided in the Web UI): mongorestore --ssl --host cluster0-shard-00-00-qfovx.mongodb.net \ --port 27017 -u billy -p XXX dump Switch the application's database connections over to your MongoDB Atlas instance. Want more help? We offer a MongoDB Atlas Migration service to help you properly configure MongoDB Atlas and develop a migration plan. This is especially helpful if you need to minimize downtime for your application, if you have a complex sharded deployment, or if you want to revise your deployment architecture as part of the migration. Contact us to learn more about the MongoDB Atlas Migration service. Moving Your Application Data Out of MongoDB Atlas To migrate data out, you can download a MongoDB Atlas backup and then copy the contents to the receiving MongoDB cluster; the documentation describes how to load the data into the receiving replica set . The backup can be either a periodic snapshot or a point-in-time view of the MongoDB Atlas database. If you can't tolerate lost writes, they must be stopped by the application ( fsyncLock is not available in MongoDB Atlas). Getting the Best Out of MongoDB Atlas While MongoDB Atlas radically simplifies the operation of MongoDB there are still some decisions to take to ensure the best performance and reliability for your application. The MongoDB Atlas Best Practices white paper provides guidance on best practices for deploying, managing, and optimizing the performance of your database with MongoDB Atlas. The guide outlines considerations for achieving performance at scale with MongoDB Atlas across a number of key dimensions, including instance size selection, application patterns, schema design and indexing, and disk I/O. While this guide is broad in scope, it is not exhaustive. Following the recommendations in the guide will provide a solid foundation for ensuring optimal application performance. Download MongoDB Atlas white paper