This topic describes how to migrate data from MongoDB Atlas to ApsaraDB for MongoDB by using mongodump and mongorestore, which are both built in MongoDB for backup and restoration. You can install a MongoDB database on a local server or in an ECS instance as an intermediary, and then use mongodump and mongorestore to migrate data from a MongoDB Atlas instance to an ApsaraDB for MongoDB instance.

Prerequisites

  • An ApsaraDB for MongoDB instance is created. For more information, see Create an instance or Create a sharded cluster instance.
  • mongodump and mongorestore are installed, and their version is consistent with the database version of the MongoDB Atlas instance. For more information about the installation procedure, visit Install MongoDB.

Precautions

  • If the source and destination instances run different database versions or storage engines, ensure there are no compatibility issues between them before you start migration. For more information about the database versions and storage engines supported by ApsaraDB for MongoDB, see MongoDB versions and storage engines.
  • This is full data migration. To ensure data consistency, we recommend that you do not write data to the source database before you migrate data.
  • If you have used the mongodump command to back up a MongoDB Atlas database, move the backup files in the dump folder to another directory and make sure that the dump folder is empty. If it is not empty, its historical backup files are overwritten the next time you back up a database.
  • Run the mongodump and mongorestore commands on the local server. Do not run these commands in the mongo shell.

Required database account permissions

Data source Account permission
Source MongoDB Atlas instance Read permissions on the source database
Destination ApsaraDB for MongoDB instance Read/write permissions on the destination database

Preparation 1

  1. Create an ApsaraDB for MongoDB instance. For more information, see Create a replica set instance or Create a sharded cluster instance.
    Note
    • The storage capacity of the ApsaraDB for MongoDB instance must be greater than that of the MongoDB Atlas instance.
    • If you migrate data to a sharded cluster instance of ApsaraDB for MongoDB, we recommend that you configure sharding to store data. For more information, see Configure sharding to maximize the performance of shards.
  2. Set the password of the root user for the ApsaraDB for MongoDB instance. For more information, see Reset the password
    Note If you set the password when you created the instance, skip this step.

Preparation 2

  1. Install a MongoDB database on the local server. For more information, visit Install MongoDB.
    Note
    • This server plays an intermediary role in both data backup and restoration. The server is no longer required after the migration is complete.
    • The available storage space of the partition where the backup directory is located must be greater than the occupied storage space of the source MongoDB Atlas database.
  2. Add the public IP address of the local server to a whitelist of the ApsaraDB for MongoDB instance. For more information, see Configure a whitelist.

Procedure

  1. Log on to the MongoDB Atlas console.
  2. Add the public IP address of the local server to a whitelist of the MongoDB Atlas instance.Configure a whitelist for MongoDB Atlas
  3. On the Clusters page, click the cluster name.Clusters
  4. On the Command Line Tools tab, click COPY next to the mongodump command to copy this command with the connection information of the source MongoDB Atlas database.Copy the mongodump command with connection information
  5. On the local server, back up the source MongoDB Atlas database.
    1. On the local server, paste the mongodump command that contains the connection information of the source MongoDB Atlas database.
    2. Replace <PASSWORD> with the password of the root user and replace <DATABASE> with the name of the source MongoDB Atlas database.
    3. Run this command and wait until data backup is complete.

    The following figure shows an example.

    mongodump example
  6. Log on to the ApsaraDB for MongoDB console to obtain the public endpoint of the ApsaraDB for MongoDB instance.
    Note You must apply for a public endpoint manually. For more information, see Apply for a public address.
  7. On the local server, run the following command to import the backup files to an ApsaraDB for MongoDB database:
    mongorestore --host <mongodb_host>:3717 --authenticationDatabase admin -u <username> -d <database> <database_backupfile_directory>
    Note
    • <mongodb_host>: the public endpoint of the primary node in the replica set instance of ApsaraDB for MongoDB, or the public endpoint of a mongos in the sharded cluster instance of ApsaraDB for MongoDB.
    • <username>: the username of the destination database in the ApsaraDB for MongoDB instance.
    • <database>: the source database you want to restore. If the backup files contain data from more than one database, repeat this step to restore all the databases.
    • <database_backupfile_directory>: the directory of the backup files.

    Examples:

    Restore the mongodbtest database.
    mongorestore --host dds-bp**********-pub.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u root -d mongodbtest /dump/mongodbtest
    Restore the test123 database.
    mongorestore --host dds-bp**********-pub.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u root -d test123 /dump/test123
  8. When Enter password: is displayed, enter the password of the ApsaraDB for MongoDB database user and press Enter.

After data restoration is complete, the data of the source MongoDB Atlas instance is migrated to the destination ApsaraDB for MongoDB instance.