Use Data Transmission Service (DTS) to migrate data from a self-managed MongoDB replica set to an ApsaraDB for MongoDB instance with a replica set or sharded cluster architecture. DTS supports schema migration, full data migration, and incremental data migration, enabling migrations with minimal downtime.
Prerequisites
Before you begin, make sure that you have:
-
A destination ApsaraDB for MongoDB instance (replica set or sharded cluster architecture) created. See Create a replica set instance and Create a sharded cluster instance
-
Destination storage space at least 10% larger than the storage space used by the source database
-
Sufficient outbound bandwidth on the server running the source database
-
The oplog feature enabled on the source database, with operation logs retained for at least seven days — or change streams enabled with DTS able to subscribe to data changes from the past seven days (required for incremental data migration)
-
(Sharded cluster destination only) Databases and collections created for sharding, data sharding configured, the balancer enabled, and presharding performed in the destination instance. See Configure data sharding to maximize shard performance and Handle uneven data distribution in a sharded cluster MongoDB instance
Configuring data sharding prevents all data from being routed to a single shard. Enabling the balancer and performing presharding helps prevent data skew.
Use the oplog feature to obtain data changes from the source database. Change streams are only available if the source database runs MongoDB V4.0 or later.
Billing
| Migration type | Instance configuration fee | Internet traffic fee |
|---|---|---|
| Schema migration and full data migration | Free | Charged only when Access Method is set to Public IP Address. See Billing overview. |
| Incremental data migration | Charged. See Billing overview. | — |
Migration types
DTS supports the following migration types, which you can combine to meet your business requirements.
| Migration type | Objects supported | Description |
|---|---|---|
| Schema migration | DATABASE, COLLECTION, INDEX | Migrates schemas from the source to the destination |
| Full data migration | DATABASE, COLLECTION | Migrates all existing data |
| Incremental data migration | — | Continuously migrates changes after full data migration completesOplogIncremental migration does not support databases that are created after the task starts. The following incremental updates are supported:
Change streamThe following incremental updates are supported:
|
To migrate with minimal downtime, select all three types: Schema Migration, Full Data Migration, and Incremental Data Migration.
What incremental migration captures
Using oplog, DTS captures:
-
CREATE COLLECTIONandCREATE INDEX -
DROP DATABASE,DROP COLLECTION, andDROP INDEX -
RENAME COLLECTION -
Insert, update, and delete operations on documents (updates from
$setonly)
DTS does not migrate incremental data from databases created after the migration task starts.
Using change streams, DTS captures:
-
DROP DATABASEandDROP COLLECTION -
RENAME COLLECTION -
Insert, update, and delete operations on documents (updates from
$setonly)
Permissions required
Configure database accounts with the following permissions before starting the migration task.
| Database | Schema migration | Full data migration | Incremental data migration |
|---|---|---|---|
| Source self-managed MongoDB | Read on the database to be migrated and the config database |
Read on the database to be migrated, admin, and local databases |
— |
| Destination ApsaraDB for MongoDB | dbAdminAnyDatabase, readWrite on the destination database, and read on the local database |
— | — |
For instructions on creating accounts:
-
Self-managed MongoDB: db.createUser()
-
ApsaraDB for MongoDB: Manage user permissions on MongoDB databases
Limitations
Review these limitations before configuring the migration task. Skip limitations that do not apply to your destination architecture.
Source database
-
Azure Cosmos DB for MongoDB clusters and Amazon DocumentDB elastic clusters: Only full data migration is supported.
-
Collection requirements: Collections must have PRIMARY KEY or UNIQUE constraints with all fields unique; otherwise, duplicate records may appear in the destination.
-
Collection count: When migrating by selecting individual collections, up to 1,000 collections per task. Exceeding this limit causes a request error. Migrate in batches or select entire databases instead.
-
Document size: A single document cannot exceed 16 MB; exceeding this limit causes the task to fail.
-
TTL indexes: Data inconsistency between source and destination may occur after migration.
-
Schema changes: During schema migration and full data migration, do not modify schemas (including array type updates). Doing so causes task failure or data inconsistency.
-
Writes during full-only migration: If you run only full data migration without incremental migration, do not write to the source database during migration. Writes cause data inconsistency.
-
Primary/secondary switchover: If a primary/secondary switchover occurs on the source during migration, the task fails.
-
Migration latency accuracy: DTS calculates migration latency by comparing the timestamp of the latest migrated data with the current source timestamp. If the source has no recent updates, the displayed latency may be inaccurate. Perform an update on the source to refresh the latency.
If you migrate an entire database, create a heartbeat table. DTS updates the heartbeat table every second, which keeps latency readings accurate.
Sharded cluster destination
-
Orphaned documents: Delete orphaned documents before migration.
_idconflicts between source and destination documents may cause data inconsistency or task failure. -
Shard keys: Add shard keys to data in the source before starting the task. If adding shard keys to the source is not possible, see Migrate data from a MongoDB instance without a sharding key to a MongoDB sharded cluster instance.
-
Inserts during migration: Inserted data must contain shard keys.
-
Updates during migration: Shard keys cannot be modified.
Replica set destination
-
For connections over Express Connect, VPN Gateway, Smart Access Gateway, Public IP Address, or Cloud Enterprise Network (CEN): Set Domain Name or IP and Port Number to the primary node's IP address and port, or configure a high-availability endpoint. See Create a DTS task in which the source or destination database is a high-availability MongoDB database.
-
For connections over Self-managed Database on ECS: Set Port Number to the port of the primary node.
General
-
DTS cannot connect to a MongoDB database over an SRV endpoint.
-
The destination MongoDB version must be the same as or later than the source version. Earlier destination versions may cause compatibility issues.
-
DTS cannot migrate data from the
admin,config, orlocaldatabases. -
Collections with a unique index or
capped: truein the destination support only single-thread writes and do not support concurrent replay during incremental migration. This may increase migration latency. -
Transactions are not retained. During migration, transactions are converted into individual records.
-
If a primary key or unique key conflict occurs, DTS skips the conflicting write operation and retains the existing data in the destination.
-
DTS uses
ROUND(COLUMN, PRECISION)to retrieve FLOAT and DOUBLE values. Default precision: 38 digits for FLOAT, 308 digits for DOUBLE. Verify that these defaults meet your requirements. -
Concurrent writes during full data migration cause fragmentation in destination collections. After full migration, destination storage space may exceed source storage space by 5–10%.
-
DTS retries failed tasks for up to seven days. Before switching workloads to the destination, stop or release any failed tasks. Alternatively, revoke write permissions from the DTS account on the destination database to prevent the source from overwriting the destination after a task resumes.
-
Query row counts on the destination using:
db.$table_name.aggregate([{ $count:"myCount"}]). -
Make sure the destination database does not have the same
_idvalues as the source. If conflicts exist, delete the conflicting data from the destination before starting the task. -
If a DTS instance fails, DTS helpdesk attempts to recover it within 8 hours. Recovery operations may include restarting the instance or adjusting parameters. See Modify instance parameters for parameters that may change.
Configure and run a migration task
Step 1: Go to the Data Migration page
Via the DTS console:
-
Log on to the DTS console.
-
In the left-side navigation pane, click Data Migration.
-
In the upper-left corner, select the region where the migration instance resides.
Via the DMS console:
The actual steps may vary based on the DMS console mode and layout. See Simple mode and Customize the layout and style of the DMS console.
-
Log on to the DMS console.
-
In the top navigation bar, move the pointer over Data + AI > DTS (DTS) > Data Migration.
-
From the drop-down list to the right of Data Migration Tasks, select the region where the migration instance resides.
Step 2: Create the task and configure source and destination databases
-
Click Create Task.
-
Configure the source and destination databases using the parameters in the following tables.
After configuring the source and destination databases, review the Limits displayed at the top of the page. Skipping this step may cause the task to fail or result in data inconsistency.
Source database parameters
| Parameter | Description |
|---|---|
| Task Name | The task name. DTS generates a name automatically. Specify a descriptive name to identify the task easily. Task names do not need to be unique. |
| Select Existing Connection | If the source database is registered with DTS, select it from the drop-down list. DTS populates the remaining parameters automatically. Otherwise, configure the parameters below manually. |
| Database Type | Select MongoDB. |
| Access Method | Select the connection type based on the location of the source database. This topic uses Public IP Address as an example. If you select a different connection type, complete the required preparations. |
| Instance Region | Select the region where the source database resides. If the region is not listed, select the geographically closest region. |
| Architecture | Select Replica Set. |
| Migration Method | Select the method for migrating incremental data: Oplog (recommended) or ChangeStream. Oplog is available when the oplog feature is enabled on the source. ChangeStream is available when change streams are enabled. If the source is an inelastic Amazon DocumentDB cluster, you must select ChangeStream and set the Architecture parameter to Sharded Cluster. If you select Sharded Cluster for the Architecture parameter, you do not need to configure the Shard account and Shard password parameters. |
| Endpoint Type | Select Standalone or Multi-node. Available when Access Method is Express Connect, VPN Gateway, or Smart Access Gateway, Public IP Address, or Cloud Enterprise Network (CEN). |
| Domain Name or IP | The domain name or IP address of the source database. Available when Endpoint Type is Standalone. |
| Port Number | The service port of the source database. Available when Endpoint Type is Standalone. |
| Endpoint | The endpoint of the source database in <IP>:<Port> format. Press Enter to add multiple endpoints. Available when Endpoint Type is Multi-node. |
| Authentication Database | The database that the source account belongs to. Default: admin. |
| Database Account | The account used to connect to the source database. For required permissions, see Permissions required. If Access Method is Self-managed Database on ECS or Database Gateway, enter the account of the shard node. |
| Database Password | The password for the database account. |
| Encryption | The connection encryption method: Non-encrypted, SSL-encrypted, or Mongo Atlas SSL. Available options depend on Access Method and Architecture. SSL-encrypted is unavailable when Architecture is Sharded Cluster and Migration Method is Oplog. If Architecture is Replica Set, Access Method is not Alibaba Cloud Instance, and Encryption is SSL-encrypted, upload a CA certificate to verify the connection. |
Destination database parameters
| Parameter | Description |
|---|---|
| Select Existing Connection | If the destination instance is registered with DTS, select it from the drop-down list. Otherwise, configure the parameters below manually. |
| Database Type | Select MongoDB. |
| Access Method | Select Alibaba Cloud Instance. |
| Instance Region | Select the region where the destination ApsaraDB for MongoDB instance resides. |
| Replicate Data Across Alibaba Cloud Accounts | Select No to use an instance in the current account. |
| Architecture | Select the architecture of the destination instance. |
| Instance ID | Select the ID of the destination instance. |
| Authentication Database | The database that the destination account belongs to. Default: admin. |
| Database Name | The name of the database in the destination instance where migrated objects will be stored. |
| Database Account | The account used to connect to the destination instance. For required permissions, see Permissions required. |
| Database Password | The password for the database account. |
| Encryption | The connection encryption method. SSL-encrypted is unavailable when the destination uses a sharded cluster architecture. |
Step 3: Test connectivity
Click Test Connectivity and Proceed, then click Test Connectivity in the CIDR Blocks of DTS Servers dialog box.
Make sure the CIDR blocks of DTS servers are added to the allowlists of the source and destination databases. See Add DTS server IP addresses to a whitelist.
Step 4: Configure migration objects
On the Configure Objects page, configure the migration objects using the following parameters.
| Parameter | Description |
|---|---|
| Migration Types | Select the migration types: Schema Migration, Full Data Migration, and optionally Incremental Data Migration. For zero-downtime migration, select all three. If you skip Schema Migration, create the target databases and collections in the destination before starting. If you skip Incremental Data Migration, do not write to the source during migration. |
| Processing Mode of Conflicting Tables | Precheck and Report Errors: fails the precheck if the destination contains collections with the same names as the source. Use object name mapping to rename migrated collections if existing collections cannot be deleted. Ignore Errors and Proceed: skips the precheck. DTS does not migrate records with the same primary keys as existing destination records. Data consistency is not guaranteed. |
| Capitalization of object names in destination instance | The capitalization policy for database and collection names in the destination. Default: DTS default policy. See Specify the capitalization of object names in the destination instance. |
| Source Objects | Select databases or collections to migrate. Click the |
| Selected Objects | Right-click an object to rename it in the destination (object name mapping), set the incremental migration mode, or specify WHERE conditions for full migration data filtering. To remove an object, click it and then click the |
Step 5: Configure advanced settings
Click Next: Advanced Settings and configure the following parameters.
| Parameter | Description |
|---|---|
| Dedicated Cluster for Task Scheduling | By default, DTS schedules tasks on a shared cluster. Purchase a dedicated cluster for isolated resources. See What is a DTS dedicated cluster? |
| Retry Time for Failed Connections | The retry duration for connection failures. Valid values: 10–1,440 minutes. Default: 720 minutes. Set this to at least 30 minutes. If different tasks share the same source or destination database, the value set last takes precedence. |
| Retry Time for Other Issues | The retry duration for DDL or DML operation failures. Valid values: 1–1,440 minutes. Default: 10 minutes. Set this to at least 10 minutes. This value must be less than Retry Time for Failed Connections. |
| Enable Throttling for Full Data Migration | Limits read/write load on source and destination during full migration. Configure QPS to the source database, RPS of Full Data Migration, and data migration speed. Available only when Full Data Migration is selected. |
| Only one data type for primary key `_id` in a table of the data to be synchronized | Yes: DTS migrates only one data type of _id per collection without scanning all types. No: DTS scans and migrates all data types of _id. Enable this based on your data. Incorrect configuration may cause data loss. Available only when Full Data Migration is selected. |
| Enable Throttling for Incremental Data Migration | Limits load during incremental migration. Configure RPS of Incremental Data Migration and data migration speed. Available only when Incremental Data Migration is selected. |
| Environment Tag | An optional tag to identify the instance. |
| Configure ETL | Enable the extract, transform, and load (ETL) feature to transform data during migration. See Configure ETL in a data migration or data synchronization task. |
| Monitoring and Alerting | Configure alerts for task failure or high migration latency. If enabled, set the alert threshold and notification contacts. See Configure monitoring and alerting. |
Step 6: Configure data verification (optional)
Click Next Step: Data Verification to set up a data verification task. See Configure a data verification task.
Step 7: Run the precheck
Click Next: Save Task Settings and Precheck.
To preview the API parameters for this task configuration, move the pointer over Next: Save Task Settings and Precheck and click Preview OpenAPI parameters.
DTS runs a precheck before starting the migration. The task starts only after passing the precheck.
-
If a check item fails, click View Details, resolve the issue, then click Precheck Again.
-
If an alert is triggered for a check item that can be ignored, click Confirm Alert Details > Ignore > OK > Precheck Again. Ignoring alerts may result in data inconsistency.
Step 8: Purchase and start the instance
-
Wait for Success Rate to reach 100%, then click Next: Purchase Instance.
-
On the Purchase Instance page, configure the following parameters.
| Parameter | Description |
|---|---|
| Resource Group | The resource group for the migration instance. Default: default resource group. See What is Resource Management? |
| Instance Class | The instance class determines migration speed. Select based on your data volume and timing requirements. See Instance classes of data migration instances. |
-
Read and select the checkbox to agree to Data Transmission Service (Pay-as-you-go) Service Terms.
-
Click Buy and Start, then click OK in the confirmation dialog box.
Monitor migration progress
Track progress on the Data Migration page.
-
Tasks without incremental migration stop automatically when full migration completes. The status changes to Completed.
-
Tasks with incremental migration run continuously. The status remains Running until you stop the task.
Monitor the following key indicators to assess migration health:
| Metric | What it measures |
|---|---|
| Migration latency | The time delay between a change on the source and its application on the destination. A latency near 0 seconds indicates the destination is in sync with the source. |
| Task status | The current state of the migration task (Running, Completed, Failed). |
| Migration speed | The data transfer rate during full migration. If speed drops significantly, check source bandwidth and throttling settings. |
If latency is persistently high with no source updates, perform a write operation on the source to refresh the displayed latency value.
Cut over to the destination
After incremental migration latency stabilizes near 0 seconds, the destination is in sync with the source. Complete the cutover in the following order:
-
Stop writes to the source database.
-
Wait for the migration latency to reach 0 seconds.
-
Stop the DTS migration task.
-
Update your application's connection string to point to the destination ApsaraDB for MongoDB instance.
-
Verify that the application is running correctly on the destination.
Before switching workloads, stop or release the DTS task. If the task is still running after a failure, it may resume and overwrite data in the destination. Alternatively, revoke write permissions from the DTS account on the destination database.