This topic describes how to migrate data from a self-managed standalone MongoDB database to an ApsaraDB for MongoDB instance by using Data Transmission Service (DTS). DTS supports full data migration and incremental data migration. When you configure a data migration task, you can select the two migration types to ensure service continuity.

You can also use the built-in commands of MongoDB to migrate self-managed MongoDB databases. For more information, see Migrate self-managed MongoDB databases to standalone instances by using tools provided by MongoDB.

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

Prerequisites

  • The version of the self-managed MongoDB database is 3.0, 3.2, or 3.4.
  • The available storage space of the ApsaraDB for MongoDB instance is larger than the total size of the data in the self-managed MongoDB database.

Precautions

  • To prevent service disruptions, we recommend that you migrate data during off-peak hours.
  • If the source and destination MongoDB databases use different versions or storage engines, make sure that your applications can run on both databases. For more information about the versions and storage engines that are supported by ApsaraDB for MongoDB, see MongoDB versions and storage engines.
  • Before you migrate incremental data from a self-managed standalone MongoDB database, you must enable the oplog feature for the database. For more information, see Preparations before incremental data migration.
  • The admin or local database is not used as the source or destination database.

Billing

Migration type Task configuration fee Internet traffic fee
Full data migration Free of charge. Charged only when data is migrated from Alibaba Cloud over the Internet. For more information, see Pricing.
Incremental data migration Charged. For more information, see Pricing.

Migration types

  • Full data migration: DTS migrates all historical data of the source MongoDB database to the destination MongoDB database.
    Note DTS can migrate the following types of objects: database, collection, and index.
  • Incremental data migration: After full data migration is complete, DTS synchronizes incremental data of the source MongoDB database to the destination MongoDB database.
    Note
    • DTS can synchronize the create and delete operations that are performed on databases, collections, and indexes.
    • DTS can synchronize the create, delete, and update operations that are performed on documents.

Permissions required for database accounts

Database Full data migration Incremental data migration
Self-managed MongoDB database The read permissions on the source database The read permissions on the source database, the admin database, and the local database
ApsaraDB for MongoDB instance The read and write permissions on the destination database The read and write permissions on the destination database

For more information about how to create and authorize a database account, see the following topics:

Preparations before incremental data migration

Before you use DTS to migrate incremental data, enable the oplog feature for the source database. If you perform only full data migration, skip the following steps.

Note When you enable the oplog feature, the MongoDB service is restarted. We recommend that you perform this operation during off-peak hours.
  1. Use Mongo Shell to connect to the self-managed MongoDB database.
  2. Run the following commands to shut down the MongoDB service:
    use admin
    db.shutdownServer()
  3. Run the following command to start the MongoDB service from the backend as a replica set:
    mongod --port 27017 --dbpath /var/lib/mongodb --logpath /var/log/mongodb/mongod.log --replSet rs0 --bind_ip 0.0.0.0 --auth --fork
    Note
    • The database path used by the preceding command is /var/lib/mongodb. The log file path is /var/log/mongodb/mongod.log. You must specify the paths based on your business needs.
    • The preceding command uses 0.0.0.0 as the associated IP address of the MongoDB database. This allows you to access the database by using all IP addresses. After data migration is complete, you can run the kill command to terminate the process, and start the MongoDB service by using the original configuration file.
    • The preceding command enables authentication. You can access the database only after you pass the authentication.
  4. Use Mongo Shell to reconnect to the self-managed MongoDB database.
  5. Run the following commands to initialize the replica set:
    use admin
    rs.initiate()
  6. The role of the current node changes to primary.
    Note You can run the rs.printReplicationInfo() command to view the status of oplog.

Procedure

  1. Log on to the DTS console.
  2. In the left-side navigation pane, click Data Migration.
  3. At the top of the Migration Tasks page, select the region where the ApsaraDB for MongoDB instance resides.
    Select a region
  4. In the upper-right corner of the page, click Create Migration Task.
  5. Configure the source and destination databases.
    MongoDB: Configure the source and destination databases
    Section Parameter Description
    N/A Task Name DTS automatically generates a task name. We recommend that you specify an informative name for easy identification. You do not need to use a unique task name.
    Source Database Instance Type Select an instance type based on the deployment of the source database. In this example, select User-Created Database with Public IP Address.
    Note If you select other instance types, you must deploy the network environment for the source database. For more information, see Preparation overview.
    Instance Region If you select User-Created Database with Public IP Address as the instance type, you do not need to specify the Instance Region parameter.
    Note If a whitelist is configured for the self-managed MongoDB database, you must add the CIDR blocks of DTS servers to the whitelist of the database. You can click Get IP Address Segment of DTS next to Instance Region to obtain the CIDR blocks of DTS servers.
    Database Type Select MongoDB.
    Hostname or IP Address Enter the endpoint that is used to connect to the self-managed MongoDB database. In this example, enter the public IP address.
    Port Number Enter the service port number of the self-managed MongoDB database.
    Note The service port of the self-managed MongoDB database must be accessible over the Internet.
    Database Name Enter the name of the authentication database. The database account is created in this database.
    Database Account Enter the account that is used to log on to the self-managed MongoDB database. For more information about the permissions that are required for the account, see Permissions required for database accounts.
    Database Password Enter the password of the database account.
    Note After you specify the source database parameters, click Test Connectivity next to Database Password to verify whether the specified parameters are valid. If the specified parameters are valid, the Passed message appears. If the Failed message appears, click Check next to Failed. Modify the source database parameters based on the check results.
    Encryption Select Non-encrypted.
    Note You can select SSL-encrypted only when you migrate data from MongoDB Atlas.
    Destination Database Instance Type Select MongoDB Instance.
    Instance Region Select the region where the destination ApsaraDB for MongoDB instance resides.
    MongoDB Instance ID Select the ID of the destination ApsaraDB for MongoDB instance.
    Database Name Enter the name of the authentication database. The database account is created in this database.
    Database Account Enter the database account of the destination ApsaraDB for MongoDB instance. For more information about the permissions that are required for the account, see Permissions required for database accounts.
    Database Password Enter the password of the database account.
    Note After you specify the destination database parameters, click Test Connectivity next to Database Password to verify whether the specified parameters are valid. If the specified parameters are valid, the Passed message appears. If the Failed message appears, click Check next to Failed. Modify the destination database parameters based on the check results.
  6. In the lower-right corner of the page, click Set Whitelist and Next.
    Note DTS adds the CIDR blocks of DTS servers to the whitelist of the destination ApsaraDB for MongoDB instance. This ensures that DTS servers can connect to the destination ApsaraDB for MongoDB instance. After data migration is complete, you can remove these CIDR blocks from the whitelist. For more information, see Configure a whitelist for an ApsaraDB for MongoDB instance.
  7. Select the migration types and the objects to be migrated.
    Select the migration types and the objects to be migrated
    Setting Description
    Select the migration types
    • To perform only full data migration, select Full Data Migration.
    • To ensure service continuity during data migration, select Full Data Migration and Incremental Data Migration.
      Note
      • Before you migrate incremental data from a self-managed standalone MongoDB database, you must enable the oplog feature for the database. For more information, see Preparations before incremental data migration.
      • If Incremental Data Migration is not selected, we recommend that you do not write data to the self-managed MongoDB database during full data migration. This ensures data consistency between the source and destination databases.
    Select the objects to be migrated
    • Select one or more objects from the Available section and click the Right arrow icon to move the objects to the Selected section.
      Note
      • You cannot migrate data from the admin or local database.
      • The config database is an internal database. We recommend that you do not migrate data from the config database.
    • You can select databases, collections, or functions as the objects to be migrated.
    • By default, after an object is migrated to the destination database, the name of the object remains unchanged. You can use the object name mapping feature to change the names of the objects that are migrated to the destination database. For more information, see Object name mapping.
    Specify whether to rename object names You can use the object name mapping feature to change the names of the objects that are migrated to the destination instance. For more information, see Object name mapping.
    Specify the retry time for failed connections to the source or destination database By default, if DTS fails to connect to the source or destination database, DTS retries within the next 12 hours. You can specify the retry time based on your needs. If DTS reconnects to the source and destination databases within the specified time, DTS resumes the data migration task. Otherwise, the data migration task fails.
    Note When DTS retries a connection, you are charged for the DTS instance. We recommend that you specify the retry time based on your business needs. You can also release the DTS instance at your earliest opportunity after the source and destination instances are released.
  8. In the lower-right corner of the page, click Precheck.
    Notice
    • Before you can start the data migration task, a precheck is performed. DTS can migrate data only if the precheck is past.
    • If the precheck fails, click the Prompt icon next to each failed item to view details. Fix the issues as prompted and run the precheck again.
  9. After the task passes the precheck, click Next.
  10. In the Confirm Settings dialog box, specify the Channel Specification parameter and select Data Transmission Service (Pay-As-You-Go) Service Terms.
  11. Click Buy and Start to start the data migration task.
    • Full data migration

      We recommend that you do not manually stop the task during full data migration. Otherwise, the data migrated to the destination database will be incomplete. You can wait until the data migration task automatically stops.

    • Incremental data migration

      The task does not automatically stop during incremental data migration. You must manually stop the task.

      Note We recommend that you select an appropriate time to manually stop the data migration task. For example, you can stop the task during off-peak hours or before you switch your workloads to the ApsaraDB for MongoDB instance.
      1. Wait until Incremental Data Migration and The migration task is not delayed appear in the progress bar of the migration task. Then, stop writing data to the source database for a few minutes. The delay time of incremental data migration may be displayed in the progress bar.
      2. After the status of incremental data migration changes to The migration task is not delayed again, manually stop the migration task. MongoDB: The migration task is not delayed
  12. Switch your workloads to the destination ApsaraDB for MongoDB instance.