This topic describes how to migrate data from MongoDB Atlas to ApsaraDB for MongoDB by using mongodump and mongorestore, which are built in open source MongoDB for backup and restoration. You can directly install mongodump and mongorestore (or create a MongoDB Atlas instance) on an on-premises server or an ECS instance to migrate data from the MongoDB Atlas instance to an ApsaraDB for MongoDB instance.

Note In the following procedure, an on-premises server is used as an example.

Precautions

  • 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, see Install MongoDB.
  • 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 process is a full data migration. To ensure data consistency, we recommend that you do not write data to the source databases before you migrate data.
  • If you run the mongodump command, the historical backup files in the dump folder are overwritten. 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.
  • Run the mongodump and mongorestore commands on the on-premises 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 databases
Destination ApsaraDB for MongoDB instance Read/write permissions on the destination databases

Preparations

In the ApsaraDB for MongoDB console:

  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 for an ApsaraDB for MongoDB instance.
    Note If you have set the password when you create the instance, skip this step.

On the on-premises server:

  1. Install MongoDB databases. For more information, see Install MongoDB.
    Note
    • This server functions as an intermediary platform for data backup and restoration. It 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 databases.
  2. Add the public IP address of the on-premises server to a whitelist of the ApsaraDB for MongoDB instance. For more information, see Configure a whitelist for an ApsaraDB for MongoDB instance.

Procedure

  1. Log on to the MongoDB Atlas console.
  2. Add the public IP address of the on-premises server to a whitelist of the MongoDB Atlas instance.Configure a whitelist for MongoDB Atlas
  3. On the Clusters page, find the target cluster and click its 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 databases.Copy the mongodump command with connection information
  5. On the on-premises server, back up the source MongoDB Atlas databases.
    1. On the on-premises server, paste the mongodump command that contains the connection information of the source MongoDB Atlas databases.
    2. Replace <PASSWORD> with the password of the root user and replace <DATABASE> with the names of the source MongoDB Atlas databases.
    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 manually apply for a public endpoint. For more information, see Apply for a public endpoint for an ApsaraDB for MongoDB instance.
  7. On the on-premises server, run the following command to import the backup files to the 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 that 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.