All Products
Search
Document Center

ApsaraDB for MongoDB:Migrate a self-managed MongoDB database to an ApsaraDB for MongoDB standalone instance by using mongodump and mongorestore

Last Updated:Mar 18, 2024

This topic describes how to migrate a self-managed MongoDB database to ApsaraDB for MongoDB by using mongodump and mongorestore that are built in MongoDB for data backup and restoration. You can install the MongoDB database on an on-premises device or Elastic Compute Service (ECS) instance and use mongodump and mongorestore to migrate a self-managed MongoDB database to an ApsaraDB for MongoDB instance.

To avoid service disruption, we recommend that you use Data Transmission Service (DTS) to migrate a self-managed MongoDB database that uses the standalone architecture to ApsaraDB for MongoDB. For more information, see Migrate data from a standalone ApsaraDB for MongoDB instance to another ApsaraDB for MongoDB instance.

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

Prerequisites

  • mongodump and mongorestore are installed on a different server from the self-managed MongoDB database, but run the same version as the database. For more information about the installation procedure, visit Install MongoDB at the MongoDB official website.

    Note

    You can also run the mongodump and mongorestore commands on the server where the self-managed MongoDB database resides.

  • To ensure compatibility, the version of the self-managed MongoDB database is 3.0 or later.

    Note

    If the source self-managed MongoDB database and the destination ApsaraDB for MongoDB instance run different database versions or storage engines, make sure that no compatibility issues occur 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.

  • The storage space of the ApsaraDB for MongoDB standalone instance is larger than that occupied by the self-managed MongoDB database. If the storage space is insufficient, you can expand the storage space. For more information, see Overview.

Usage notes

  • This is full data migration. To ensure data consistency, we recommend that you stop writing data to the self-managed MongoDB database before you migrate data.

  • If you have run the mongodump command to back up a self-managed 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 the database.

  • Run the mongodump and mongorestore commands on the server where the self-managed MongoDB database resides. Do not run them in the mongo shell.

Step 1: Back up the self-managed MongoDB database

  1. On the server where the self-managed MongoDB database resides, run the following command to back up all databases:

    mongodump --host <mongodb_host> --port <port>  -u <username>  --authenticationDatabase  <database>
    Note
    • <mongodb_host>: the address of the server where the self-managed MongoDB database resides. In this case, enter 127.0.0.1.

    • <port>: the service port of the self-managed MongoDB database. The default value is 27017.

    • <username>: the account used to log on to the self-managed MongoDB database. In examples of this topic, the test account is used.

    • <database>: the name of the database corresponding to the username for which authentication is enabled.

    Example:

    mongodump --host 127.0.0.1 --port 27017 -u test --authenticationDatabase admin
  2. Enter the password of the database account when Enter password is displayed and press the Enter key. The data backup operation starts.

    Note

    The password characters are masked when you enter the password.

Wait till the data backup is complete. The data of the self-managed MongoDB database is backed up to the dump folder of the directory where you run this command.

Step 2: Migrate data to the destination instance

  1. Obtain the connection address of the primary node of the ApsaraDB for MongoDB instance.

    1. Log on to the ApsaraDB for MongoDB console.

    2. In the upper-left corner of the page, select the region where the instance is deployed.

    3. In the left-side navigation pane, click Replica Set Instances.

    4. Find the desired instance and click the instance ID.

    5. In the left-side navigation pane, click Database Connections to view the database connection details.

      MongoDB单节点查看连接信息

      Table 1. Connection addresses

      Endpoint type

      Description

      Scenario

      VPC endpoint

      A virtual private cloud (VPC) is an isolated network that provides higher security and performance than the classic network.

      The self-managed MongoDB database is deployed on the ECS instance.

      Note

      The ECS instance and ApsaraDB for MongoDB instance must be located in the same region and VPC.

      Public endpoint

      By default, ApsaraDB for MongoDB instances do not provide public connection addresses. You must apply for a public endpoint if necessary. For more information, see Apply for a public endpoint for an ApsaraDB for MongoDB standalone instance.

      The self-managed MongoDB database is deployed on an on-premises device.

  2. Add the IP address of the server where the self-managed database resides to a whitelist of the ApsaraDB for MongoDB instance. For more information, see Configure a whitelist for an ApsaraDB for MongoDB instance.

    Note
    • When you connect to an ApsaraDB for MongoDB instance over a VPC, you must add the internal IP address of the ECS instance where the self-managed database is deployed to a whitelist of the ApsaraDB for MongoDB instance.

    • When you connect to an ApsaraDB for MongoDB instance over the Internet, you must add the public IP address of the local server where the self-managed database resides to a whitelist of the ApsaraDB for MongoDB instance.

  3. Run the following command on the server where the self-managed database resides to migrate full data to the ApsaraDB for MongoDB instance:

    mongorestore --host <Primary_host>  -u <username> --authenticationDatabase <database> <Backup directory>
    Note
    • <Primary_host>: the connection address of the primary node in the ApsaraDB for MongoDB instance.

    • <username>: the database account of the ApsaraDB for MongoDB instance. The initial account is root.

    • <database>: the name of the database corresponding to the username for which authentication is enabled. If the username is root, enter admin.

    • <Backup directory>: the directory where the backup files are stored. The default value is dump.

    In the following sample command, the username is test and the database is admin.

    mongorestore --host dds-bp19f409d7512****-pub.mongodb.rds.aliyuncs.com:3717 -u test --authenticationDatabase admin dump
  4. Enter the account password of the ApsaraDB for MongoDB instance when Enter password is displayed and press the Enter key. The data backup operation starts.

    Note
    • The password characters are masked when you enter the password.

    • If you forget the password of the root account, you can reset it. For more information, see (Optional) Reset a password.

Wait until the data migration is complete. Switch your business to the ApsaraDB for MongoDB instance during off-peak hours.

What to do next

After the database is migrated to the ApsaraDB for MongoDB instance, you can connect to the database and manage the database and database account.