Help Center> Document Database Service> User Guide> Migrating Data> Migrating Data Using mongodump and mongorestore

Migrating Data Using mongodump and mongorestore

Scenarios

DDS supports access through EIPs by enabling public accessibility. To access DDS from an ECS, you need to create an ECS first and then install mongodump and mongorestore on it.

Precautions

  • You are advised to perform the migration during off-peak hours to avoid the impact of migration on your services.
  • The admin and local system databases cannot be migrated.
  • Ensure that no service set is created in the system databases admin and local in the source database. If service sets already exist, migrate them out of the system databases admin and local before migration.
  • Before importing data, ensure that necessary indexes exist on the source database. That is, delete unnecessary indexes and create necessary indexes before migration.
  • If you choose to migrate a sharded cluster, you must create a set of shards in the destination database and configure sharding. In addition, indexes must be created before migration.
  • If the backup using the mongodump tool fails (for example, an error is reported when the backup progress reaches 97%), you are advised to increase the VM storage space and reserve some redundant space before performing the backup again.

Prerequisites

  1. An ECS or a device that can access DDS is ready for use.
    • To connect to a DDS DB instance through a private network from an ECS, you need to create and log in to the ECS. For details, see Purchasing an ECS and Logging In to an ECS.
    • To bind an EIP to a DB instance:
      1. Bind an EIP to a node in the DB instance. For details about how to bind an EIP to a node, see "Binding an EIP" in the Document Database Service Getting Started.
      2. Ensure that your local device can access the EIP that has been bound to the DB instance.
  2. A migration tool has been installed on the prepared ECS.

    For details on how to install the migration tool, see How Can I Install a MongoDB Client?

    The MongoDB client provides the mongoexport and mongoimport tools.

Backing Up Data

  1. Log in to the ECS or the device that can access DDS.
  2. Back up the source database data using the mongodump tool.

    The SSL connection is used as an example. If you select a common connection, delete --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidCertificates from the following command.

    mongodump --host <DB_HOST> --port <DB_PORT> -u <DB_USER> --authenticationDatabase <AUTH_DB> --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidCertificates

    • DB_HOST indicates the database address.
    • DB_PORT indicates the database port.
    • DB_USER indicates the database user.
    • AUTH_DB indicates the database for storing DB_USER information. Generally, this value is admin.
    • FILE_PATH indicates the path where the root certificate is stored.

    Enter the database administrator password when prompted:

    Enter password:

    After the following example command is executed, the source database data is backed up to the dump folder in the current directory.

    ./mongodump --host 192.168.6.39 --port 8635 -u rwuser --authenticationDatabase admin --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidCertificates

    2019-03-04T18:42:10.687+0800    writing admin.system.users to
    2019-03-04T18:42:10.688+0800    done dumping admin.system.users (1 document)
    2019-03-04T18:42:10.688+0800    writing admin.system.roles to
    2019-03-04T18:42:10.690+0800    done dumping admin.system.roles (0 documents)
    2019-03-04T18:42:10.690+0800    writing admin.system.version to
    2019-03-04T18:42:10.691+0800    done dumping admin.system.version (2 documents)
    2019-03-04T18:42:10.691+0800    writing test.test_collection to
    2019-03-04T18:42:10.691+0800    writing admin.system.profile to
    2019-03-04T18:42:10.692+0800    done dumping admin.system.profile (4 documents)
    2019-03-04T18:42:10.695+0800    done dumping test.test_collection (198 documents)

Importing Data

  1. Log in to the ECS or the device that can access DDS.
  2. Upload the data to be imported to the ECS or the device that can access DDS.

    Select an uploading method based on the OS you are using. In Linux, for example, run the following command:

    scp -r <IDENTITY_DIR> <REMOTE_USER>@<REMOTE_ADDRESS>:<REMOTE_DIR>

    • IDENTITY_DIR indicates the directory that stores the backup file.
    • REMOTE_USER indicates the ECS OS user in 1.
    • REMOTE_ADDRESS indicates the ECS address in 1.
    • REMOTE_DIR indicates the directory of the ECS to which the data is uploaded.

    In Windows, upload the backup directory to the ECS using file transfer tools.

  3. Import the backups in the DDS database.

    The SSL connection is used as an example. If you select a common connection, delete --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidCertificates from the following command.

    ./mongorestore --host <DB_HOST> --port <DB_PORT> -u <DB_USER> --authenticationDatabase <AUTH_DB> <Backup directory> --ssl --sslCAFile <FILE_PATH> --sslAllowInvalidCertificates

    • DB_HOST indicates the database address.
    • DB_PORT indicates the database port.
    • DB_USER indicates the account name of the database administrator. The default value is rwuser.
    • AUTH_DB indicates the database that authenticates DB_USER. Generally, this value is admin.
    • Backup directory: indicates the directory for storing backup files. The default value is dump.
    • FILE_PATH indicates the path where the root certificate is stored.

    Enter the database administrator password when prompted:

    Enter password:

    The following is an example:

    ./mongorestore --host 192.168.6.187 --port 8635 -u rwuser --authenticationDatabase admin dump --ssl --sslCAFile /tmp/ca.crt --sslAllowInvalidCertificates

    2019-03-05T14:19:43.240+0800    preparing collections to restore from
    2019-03-05T14:19:43.243+0800    reading metadata for test.test_collection from dump/test/test_collection.metadata.json
    2019-03-05T14:19:43.263+0800    restoring test.test_collection from dump/test/test_collection.bson
    2019-03-05T14:19:43.271+0800    restoring indexes for collection test.test_collection from metadata
    2019-03-05T14:19:43.273+0800    finished restoring test.test_collection (198 documents)
    2019-03-05T14:19:43.273+0800    restoring users from dump/admin/system.users.bson
    2019-03-05T14:19:43.305+0800    roles file 'dump/admin/system.roles.bson' is empty; skipping roles restoration
    2019-03-05T14:19:43.305+0800    restoring roles from dump/admin/system.roles.bson
    2019-03-05T14:19:43.333+0800    done