All Products
Search
Document Center

ApsaraDB for MongoDB:One-way synchronization of MongoDB instances with MongoShake

Last Updated:Aug 27, 2026

MongoShake, an open-source tool developed by Alibaba Cloud, synchronizes data between MongoDB databases for use cases such as data analytics, disaster recovery, and active-active replication. This topic explains how to configure real-time synchronization between ApsaraDB for MongoDB instances.

MongoShake

MongoShake is a general-purpose service tool developed by Alibaba Cloud in Go. It replicates data from MongoDB by reading its operation logs (oplogs) and replaying them on the destination instance.

MongoShake also provides data subscription and consumption features. It integrates with SDKs, Kafka, and MetaQ to support scenarios such as log subscription, data center synchronization, and asynchronous cache eviction.

Note

For more information, see the MongoShake GitHub repository.

Supported data sources

Source

Destination

Self-managed MongoDB database on an ECS instance

Self-managed MongoDB database on an ECS instance

On-premises self-managed MongoDB database

On-premises self-managed MongoDB database

ApsaraDB for MongoDB instance

ApsaraDB for MongoDB instance

Third-party cloud MongoDB database

Third-party cloud MongoDB database

Notes

Required permissions

Source

Required permissions

Source MongoDB instance

The readAnyDatabase permission, the read permission on the local database, and the readWrite permission on the mongoshake database.

Note

The mongoshake database is automatically created in the source instance by the MongoShake program when an incremental sync starts.

Destination MongoDB instance

The readWriteAnyDatabase permission or the readWrite permission on the destination database.

Note

For information about how to create and authorize MongoDB database users, see Use DMS to manage MongoDB database users or db.createUser command.

Prerequisites

  1. For optimal synchronization performance, ensure that the source MongoDB replica set instance uses a VPC. If it uses the classic network, switch its network type to VPC. For more information, see Switch the network type of an instance from classic network to VPC.

  2. Create a destination MongoDB replica set instance. To minimize network latency, create the destination instance in the same VPC as the source instance. For more information, see Create a replica set instance.

  3. Create an ECS instance to run MongoShake. To minimize network latency, create the ECS instance in the same VPC as the source instance. For more information, see Create an ECS instance.

  4. Add the private IP address of the ECS instance to the whitelists of both the source and destination MongoDB instances, and verify that the ECS instance can connect to both instances. For more information, see Modify a whitelist.

Note

If your network setup does not meet the preceding requirements, you can apply for a public endpoint for both the source and destination MongoDB instances. Then, add the public IP address of the ECS instance to the whitelists of both MongoDB instances to perform synchronization over the public endpoints. For more information, see Apply for a public endpoint and Modify a whitelist.

Procedure

This procedure assumes that MongoShake is installed in the /test/mongoshake directory.

  1. Log on to the ECS instance.

    Note

    You can choose a logon method based on your scenario. For more information, see Overview of logon methods for ECS servers.

  2. Run the following command to download the MongoShake program and rename it to mongoshake.tar.gz.

    wget "https://github.com/alibaba/MongoShake/releases/download/release-v2.8.7-20251218/mongo-shake-v2.8.7.tgz" -O mongoshake.tar.gz
    Note

    The command downloads MongoShake 2.8.7. To download the latest version, see the releases page.

  3. Run the following command to extract the MongoShake package to the /test/mongoshake directory.

    tar zxvf mongoshake.tar.gz && mv mongo-shake-v2.8.7 /test/mongoshake && cd /test/mongoshake
  4. Run the vi collector.conf command to modify the MongoShake configuration file collector.conf. The following table describes the main parameters.

    Parameter

    Description

    Example

    mongo_urls

    The connection string URI of the source MongoDB instance. In the example, the database account is test, which is in the admin database.

    Note

    mongo_urls = mongodb://test:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717

    Note

    The password cannot contain the at sign (@). Otherwise, the connection fails.

    tunnel.address

    The connection string URI of the destination MongoDB instance. In the example, the database account is test, which is in the admin database.

    Note

    tunnel.address = mongodb://test:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717

    Note

    The password cannot contain the at sign (@). Otherwise, the connection fails.

    sync_mode

    The data synchronization mode. Valid values:

    • all: performs a full sync and an incremental sync.

    • full: performs only a full sync.

    • incr: performs only an incremental sync.

    Note

    The default value is incr.

    sync_mode = all

    Note

    For a complete list of parameters in collector.conf, see the "collector.conf parameters" section in Appendix.

  5. Run the following command to start the synchronization task and print logs.

    ./collector.linux -conf=collector.conf -verbose
  6. Observe the log output. When an entry similar to the following appears, it indicates that the full sync is complete and the incremental sync has started.

    [09:38:57 CST 2019/06/20] [INFO] (mongoshake/collector.(*ReplicationCoordinator).Run:80) finish full sync, start incr sync with timestamp: fullBeginTs[1560994443], fullFinishTs[1560994737]

Monitoring status

After the incremental sync starts, open a new terminal window and run the following commands:

cd /test/mongoshake && ./mongoshake-stat --port=9100
Note

mongoshake-stat is a Python script and requires Python 2.7 to be installed. For more information, see the official Python website.

Sample output:

./mongoshake-stat --port=9100

  logs_get/sec  |  logs_repl/sec  |  logs_success/sec  |          lsn.time          |        lsn_ack.time        |       lsn_ckpt.time        |         now.time           |   replset
      none      |      none       |        none        |  2019-06-20 10:34:14       |  2019-06-20 10:34:13       |  2019-06-20 10:33:36       |  2019-06-20 10:34:14       |  mgset-10
      545       |      768        |        768         |  2019-06-20 10:34:15       |  2019-06-20 10:34:15       |  2019-06-20 10:34:14       |  2019-06-20 10:34:15       |  mgset-10
      541       |      511        |        511         |  2019-06-20 10:34:15       |  2019-06-20 10:34:15       |  2019-06-20 10:34:14       |  2019-06-20 10:34:16       |  mgset-10
      639       |      512        |        512         |  2019-06-20 10:34:16       |  2019-06-20 10:34:16       |  2019-06-20 10:34:14       |  2019-06-20 10:34:17       |  mgset-10
      543       |      512        |        512         |  2019-06-20 10:34:17       |  2019-06-20 10:34:17       |  2019-06-20 10:34:14       |  2019-06-20 10:34:18       |  mgset-10
      546       |      768        |        768         |  2019-06-20 10:34:19       |  2019-06-20 10:34:19       |  2019-06-20 10:34:14       |  2019-06-20 10:34:19       |  mgset-10
      538       |      512        |        512         |  2019-06-20 10:34:20       |  2019-06-20 10:34:20       |  2019-06-20 10:34:19       |  2019-06-20 10:34:20       |  mgset-10
      550       |      511        |        511         |  2019-06-20 10:34:21       |  2019-06-20 10:34:21       |  2019-06-20 10:34:19       |  2019-06-20 10:34:21       |  mgset-10
      546       |      512        |        512         |  2019-06-20 10:34:22       |  2019-06-20 10:34:22       |  2019-06-20 10:34:19       |  2019-06-20 10:34:22       |  mgset-10
      531       |      512        |        512         |  2019-06-20 10:34:23       |  2019-06-20 10:34:23       |  2019-06-20 10:34:19       |  2019-06-20 10:34:23       |  mgset-10
      547       |      512        |        512         |  2019-06-20 10:34:24       |  2019-06-20 10:34:24       |  2019-06-20 10:34:19       |  2019-06-20 10:34:24       |  mgset-10
      541       |      512        |        512         |  2019-06-20 10:34:24       |  2019-06-20 10:34:24       |  2019-06-20 10:34:19       |  2019-06-20 10:34:25       |  mgset-10

Parameter descriptions:

Parameter

Description

logs_get/sec

The number of oplogs fetched per second.

logs_repl/sec

The number of oplog replay operations performed per second.

logs_success/sec

The number of successful oplog replay operations per second.

lsn.time

The timestamp of the last oplog sent.

lsn_ack.time

The timestamp when the write operation was confirmed by the destination instance.

lsn_ckpt.time

The timestamp when the checkpoint was persisted.

now.time

The current time.

replset

The name of the source database replica set.

Migrate data from the admin database

MongoDB does not officially recommend storing business data in the admin database because locking behavior and conflicts with internal commands can degrade instance performance.

MongoShake supports synchronizing business data from the admin database to a non-admin database.

To do this, follow the Procedure. In Step 4, when you modify the collector.conf file, add the following configuration items:

filter.pass.special.db = admin

# Migrate all business collections from the admin database to newDB.
transform.namespace = admin:newDB
# Or, migrate the abc collection in the admin database to the def collection in the target database. You can configure multiple rules.
transform.namespace = admin.abc:target.def

Appendix

Table 1. collector.conf parameters

Category

Parameter

Description

Example

N/A

conf.version

The version of the configuration file. Do not modify this value.

conf.version = 4

Global options

id

The ID of the sync task. You can customize this value. It is used for the log file name, the database name for storing checkpoint information, and the name of the database on the destination instance.

id = mongoshake

master_quorum

The high availability option. When primary and standby MongoShake nodes synchronize data from the same source, set this parameter to true on the primary MongoShake node.

Valid values:

  • true: enabled

  • false: disabled

Note

The default value is false.

master_quorum = false

full_sync.http_port

The HTTP port for viewing the full sync status.

Note

The default value is 9101.

full_sync.http_port = 9101

incr_sync.http_port

The HTTP port for viewing the incremental sync status.

Note

The default value is 9100.

incr_sync.http_port = 9100

system_profile_port

The profiling port that is used to view internal stack information.

system_profile_port = 9200

log.level

The log level. Valid values:

  • error: Error-level information.

  • warning: Warning-level information.

  • info: Informational messages about the current system status.

  • debug: Debugging information.

Default value: info.

log.level = info

log.dir

The directory for log files and PID files. If this parameter is not set, the logs directory in the current path is used by default.

Note

You must specify an absolute path for this parameter.

log.dir = ./logs/

log.file

The name of the log file. You can customize this value.

Note

The default value is collector.log.

log.file = collector.log

log.flush

The log refresh frequency on the screen. Valid values:

  • true: prints every log entry immediately. This can affect performance.

  • false: does not guarantee that every log entry is printed immediately. This option improves performance.

Note

The default value is false.

log.flush = false

sync_mode

The data synchronization mode. Valid values:

  • all: performs a full sync and an incremental sync.

  • full: performs only a full sync.

  • incr: performs only an incremental sync.

Note

The default value is incr.

sync_mode = all

mongo_urls

The connection string URI of the source MongoDB instance. In the example, the database account is test, which is in the admin database.

Note

mongo_urls = mongodb://test:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717

mongo_cs_url

If the source is a sharded cluster instance, you must specify the connection string URI of the ConfigServer (CS) node. For information about how to obtain the connection string URI of a ConfigServer node, see Apply for an endpoint for a shard.

In the example, the database account is test, which is in the admin database.

mongo_cs_url = mongodb://test:****@dds-bp19f409d7512****-csxxx.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****-csxxx.mongodb.rds.aliyuncs.com:3717/admin

mongo_s_url

If the source is a sharded cluster instance, you must specify the connection string URI of at least one mongos node. Separate multiple mongos addresses with commas (,). For information about how to obtain the connection string URI of a mongos node, see Apply for an endpoint for a shard.

In the example, the database account is test, which is in the admin database.

mongos_s_url = mongodb://test:****@s-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,s-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717/admin

tunnel

The synchronization channel type. Valid values:

  • direct: synchronizes data directly to the destination MongoDB instance.

  • rpc: synchronizes data by using NET/RPC.

  • tcp: synchronizes data by using TCP.

  • file: synchronizes data by using file transfer.

  • kafka: synchronizes data by using Kafka.

  • mock: for testing purposes only. Data is not written to the channel.

Note

The default value is direct.

tunnel = direct

tunnel.address

The address of the destination. The supported address format varies based on the tunnel type:

  • If tunnel is set to direct, specify the connection string URI of the destination MongoDB instance.

  • If tunnel is set to rpc, specify the RPC receiver address of the destination instance.

  • If tunnel is set to tcp, specify the TCP receiver address of the destination instance.

  • If tunnel is set to file, specify the data file path of the destination instance.

  • If tunnel is set to kafka, specify the Kafka address, such as topic@brokers1,brokers2.

  • If tunnel is set to mock, leave this parameter empty.

In the example, the database account is test, which is in the admin database.

tunnel.address = mongodb://test:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717

tunnel.message

The data type in the channel. This parameter is valid only when tunnel is set to kafka or file. Valid values:

  • raw: the default type. Data is written and read in batches.

  • json: data is written to Kafka in JSON format, which allows users to read it directly.

  • bson: data is written to Kafka in BSON binary format.

Note

The default value is raw.

tunnel.message = raw

mongo_connect_mode

The connection mode of the MongoDB instance. This parameter is valid only when tunnel is set to direct. Valid values:

  • primary: pulls data from the primary node.

  • secondaryPreferred: pulls data from a secondary node.

  • standalone: pulls data from a specified single node.

Note

The default value is secondaryPreferred.

mongo_connect_mode = secondaryPreferred

filter.namespace.black

Specifies a blacklist for data synchronization. The specified namespaces are not synchronized to the destination database. Separate multiple namespaces with semicolons (;).

Note

A namespace is the canonical name of a collection or index in MongoDB. It is a combination of the database name and the collection or index name, such as mongodbtest.customer.

filter.namespace.black = mongodbtest.customer;testdata.test123

filter.namespace.white

Specifies a whitelist for data synchronization. Only the specified namespaces are synchronized to the destination database. Separate multiple namespaces with semicolons (;).

filter.namespace.white = mongodbtest.customer;test123

filter.pass.special.db

Enables synchronization of special databases. By default, databases such as admin, local, mongoshake, config, and system.views are filtered out. You can enable synchronization for these databases for specific requirements. Separate multiple database names with semicolons (;).

filter.pass.special.db = admin;mongoshake

filter.ddl_enable

Specifies whether to enable DDL synchronization. Valid values:

  • true: enabled

  • false: disabled

Note

This feature is not supported when the source is a sharded cluster instance.

filter.ddl_enable = false

checkpoint.storage.url

The storage address for checkpoints, which is used to support resumable synchronization. If this parameter is not configured, the program writes checkpoints to one of the following databases based on the instance type:

  • MongoDB replica set instance: written to the mongoshake database.

  • MongoDB sharded cluster instance: written to the admin database of the ConfigServer node.

In the example, the database account is test, which is in the admin database.

checkpoint.storage.url = mongodb://test:****@dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717,dds-bp19f409d7512****.mongodb.rds.aliyuncs.com:3717

checkpoint.storage.db

The name of the database for storing checkpoints.

Note

The default value is mongoshake.

checkpoint.storage.db = mongoshake

checkpoint.storage.collection

The name of the collection for storing checkpoints. When primary and standby MongoShake nodes synchronize data from the same source, you can change this collection name to prevent conflicts.

Note

The default value is ckpt_default.

checkpoint.storage.collection = ckpt_default

checkpoint.start_position

The start position for the sync. This parameter is used only if no checkpoint is found. The format is YYYY-MM-DDTHH:MM:SSZ.

Note

The default value is 1970-01-01T00:00:00Z.

checkpoint.start_position = 1970-01-01T00:00:00Z

transform.namespace

Renames a source database or collection and synchronizes it to the destination. For example, you can rename the database A.collection B from the source instance to database C.collection D on the destination instance.

transform.namespace = fromA.fromB:toC.toD

Full sync options

full_sync.reader.collection_parallel

The maximum number of collections that MongoShake can pull concurrently at a time.

full_sync.reader.collection_parallel = 6

full_sync.reader.write_document_parallel

The number of concurrent write threads per collection for MongoShake.

full_sync.reader.write_document_parallel = 8

full_sync.reader.document_batch_size

The batch size for writing documents to the destination instance. For example, a value of 128 indicates that 128 documents are batched together before being written.

full_sync.reader.document_batch_size = 128

full_sync.collection_exist_drop

Specifies what to do if a destination collection has the same name as a source collection. Valid values:

  • true: drops the destination collection and then synchronizes the source collection.

    Warning

    This operation deletes the destination collection. Make sure that you have a backup before you proceed.

  • false: reports an error and exits if a collection with the same name is detected in the destination database.

full_sync.collection_exist_drop = true

full_sync.create_index

Specifies whether to create an index after the synchronization is complete. Valid values:

  • foreground: creates a foreground index.

  • background: creates a background index.

  • none: does not create an index.

full_sync.create_index = none

full_sync.executor.insert_on_dup_update

Specifies whether to convert an INSERT statement to an UPDATE statement if the destination database contains a duplicate _id field. Valid values:

  • true: converts the statement.

  • false: does not convert the statement.

full_sync.executor.insert_on_dup_update = false

full_sync.executor.filter.orphan_document

Specifies whether to filter orphaned documents if the source is a sharded cluster instance. Valid values:

  • true: filters orphaned documents.

  • false: does not filter orphaned documents.

full_sync.executor.filter.orphan_document = false

full_sync.executor.majority_enable

Specifies whether to enable majority write on the destination instance. Valid values:

  • true: enables majority write.

  • false: disables majority write.

full_sync.executor.majority_enable = false

Incremental sync options

incr_sync.mongo_fetch_method

The method for pulling incremental data. Valid values:

  • oplog: pulls oplogs from the source database.

  • change_stream: pulls change events from the source database. This is supported only for MongoDB 4.0 and later.

Default value: oplog.

incr_sync.mongo_fetch_method = oplog

incr_sync.oplog.gids

Used to set up bidirectional replication for cloud clusters.

incr_sync.oplog.gids = xxxxxxxxxxxx

incr_sync.shard_key

The internal concurrency method for MongoShake. Do not modify this parameter.

incr_sync.shard_key = collection

incr_sync.worker

The number of concurrent threads for transmitting oplogs. You can increase this value if the host performance is sufficient.

Note

If the source is a sharded cluster instance, the number of threads must be equal to the number of shards.

incr_sync.worker = 8

incr_sync.worker.oplog_compressor

Enables data compression to reduce network bandwidth consumption. Valid values:

  • none: no compression

  • gzip: compresses data in gzip format

  • zlib: compresses data in zlib format

  • deflate: compresses data in deflate format

Note

This parameter is valid only when the tunnel parameter is not set to direct. If the tunnel parameter is set to direct, you must set this parameter to none.

incr_sync.worker.oplog_compressor = none

incr_sync.target_delay

The delay, in seconds, before changes from the source are applied to the destination. You can set this parameter to prevent accidental operations. For example, incr_sync.target_delay = 1800 creates a 30-minute delay.

Note

A value of 0 indicates real-time synchronization.

incr_sync.target_delay = 1800

incr_sync.worker.batch_queue_size

The internal queue configuration parameters of MongoShake. Do not modify these parameters unless necessary.

incr_sync.worker.batch_queue_size = 64

incr_sync.adaptive.batching_max_size

incr_sync.adaptive.batching_max_size = 1024

incr_sync.fetcher.buffer_capacity

incr_sync.fetcher.buffer_capacity = 256

MongoDB sync options (for direct mode only)

incr_sync.executor.upsert

Specifies whether to convert an UPDATE statement to an INSERT statement if a document with the corresponding _id or unique index does not exist. Valid values:

  • true: converts the statement.

  • false: does not convert the statement.

incr_sync.executor.upsert = false

incr_sync.executor.insert_on_dup_update

Specifies whether to convert an INSERT statement to an UPDATE statement if a document with the corresponding _id or unique index already exists. Valid values:

  • true: converts the statement.

  • false: does not convert the statement.

incr_sync.executor.insert_on_dup_update = false

incr_sync.conflict_write_to

Specifies whether to record conflicting documents if a write conflict occurs during synchronization. Valid values:

  • none: does not record conflicting documents.

  • db: records conflict logs in the mongoshake_conflict database.

  • sdk: records conflict logs in the SDK.

incr_sync.conflict_write_to = none

incr_sync.executor.majority_enable

Specifies whether to enable majority write on the destination instance. Valid values:

  • true: enables majority write.

  • false: disables majority write.

Note

Enabling this feature can affect performance.

incr_sync.executor.majority_enable = false

FAQ

Refer to the MongoShake FAQ. If you encounter other issues when you use MongoShake, you can file an issue on GitHub Issues.