This topic describes how to migrate user-created MongoDB databases to Alibaba Cloud by using mongodump and mongorestore, which are both built in MongoDB for backup and restoration. You can install user-created MongoDB databases on a local server or an ECS instance, and use mongodump and mongorestore to migrate these databases to a replica set instance of ApsaraDB for MongoDB.

To avoid service disruption, we recommend that you use DTS to migrate user-created MongoDB databases to Alibaba Cloud. For more information, see Migrate user-created MongoDB databases to Alibaba Cloud by using DTS.

For more information about data migration and synchronization solutions, see Overview.

Prerequisites

  • mongodump and mongorestore are installed on a different server from the user-created MongoDB databases, but run the same version as the databases. For more information about the installation procedure, visit Install MongoDB at the official MongoDB website.
    Note You can also run the mongodump and mongorestore commands on the server where the user-created MongoDB databases reside.
  • The storage capacity of the destination replica set instance is greater than the occupied storage space of the user-created MongoDB databases. If the storage capacity is insufficient, you can upgrade the instance. For more information, see Configuration change overview.

Precautions

  • This is full data migration. To ensure data consistency, we recommend that you stop writing data to the user-created MongoDB databases before you migrate data.
  • If you have used the mongodump command to back up a user-created MongoDB 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 servers. Do not run these commands in the mongo shell.

Step 1 Back up the user-created MongoDB databases

  1. On the server where the user-created MongoDB databases reside, run the following command to back up all the databases:
    mongodump --host <mongodb_host> --port <port>  -u <username>  --authenticationDatabase  <database>
    Note
    • <mongodb_host>: the address of the server where the user-created MongoDB databases reside. In this case, enter 127.0.0.1.
    • <port>: the service port of the user-created MongoDB databases. The default value is 27017.
    • <username>: the username you use to log on to a user-created MongoDB database.
    • <database>: the name of the authentication database. It is the database where the database user is created.

    Example:

    mongodump --host 127.0.0.1 --port 27017 -u root --authenticationDatabase admin
  2. When Enter password: is displayed, enter the password of the database user and press Enter. The data backup operation starts.
    Note The password you enter is not displayed.

Wait until data backup is complete. The data of the user-created MongoDB databases is backed up to the dump folder of the directory where you run this command.

Step 2 Migrate data to the destination replica set instance

  1. Obtain the public or internal connection string of the primary node in the destination replica set instance. For more information, see Overview of replica set instance connections.
    Note You must apply for a public endpoint manually. For more information, see Apply for a public endpoint for a replica set instance.
  2. Add the IP address of the server where the user-created MongoDB databases reside to a whitelist of the destination replica set instance. For more information, see Configure a whitelist for a replica set instance.
    Note
    • If you want to connect to a replica set instance over an internal network, you must add the private IP address of the ECS instance where the user-created MongoDB databases reside to a whitelist of the replica set instance.
    • If you want to connect to a replica set instance over the Internet, you must add the public IP address of the server where the user-created MongoDB databases reside to a whitelist of the replica set instance.
  3. On the server where the user-created MongoDB databases reside, run the following command to restore all the backup files to the destination replica set instance:
    mongorestore --host <Primary_host>  -u <username> --authenticationDatabase <database> <Backup directory>
    Note
    • <Primary_host>: the connection string of the primary node in the destination replica set instance.
    • <username>: the username you use to log on to a database of the destination replica set instance. The initial username is root.
    • <database>: the name of the authentication database. It is the database where the database user is created. If the database username is root, enter admin.
    • <Backup directory>: the directory where the backup files are stored. The default value is dump.

    Example:

    mongorestore --host dds-bp**********-pub.mongodb.rds.aliyuncs.com:3717 -u root --authenticationDatabase admin dump
  4. When Enter password: is displayed, enter the password of the database user and press Enter. The data restoration operation starts.
    Note

After data restoration is complete, switch over your business to the destination replica set instance. We recommend you perform the switchover during off-peak hours to minimize impact on your business.