对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

mongosync 行为

mongosync 二进制文件是 Mongosync 中使用的主节点 (primary node in the replica set)进程。mongosync 将数据从一个集群迁移到另一个集群。

有关 mongosync 进程的概述,请参阅关于 mongosync

要开始使用 mongosync,请参阅快速入门指南

有关更多详细信息,请参阅最适合您情况的安装连接mongosync页面。

从 1.9 开始,mongosync 包含一个嵌入式验证器,用于对目标集群上的所有支持的集合执行一系列验证检查,以确认它已成功将文档从源集群传输到目标。

当您启动 mongosync进程时,它会提供以下免责声明:

Embedded verification is enabled by default. Verification checks for data
consistency between the source and destination clusters. Verification will
cause mongosync to fail if any inconsistencies are detected, but it does not
check for all possible data inconsistencies. Please see the documentation at
https://www.mongodb.com/zh-cn/docs/cluster-to-cluster-sync/current/reference/verification/embedded
for more details. Verification requires approximately 0.5 GB of memory per 1
million documents on the source cluster and will fail if insufficient memory
is available. Accepting this disclaimer indicates that you understand the
limitations and memory requirements for this tool. To skip this disclaimer
prompt, use –-acceptDisclaimer.
To disable the embedded verifier, specify 'verification: false' when starting
mongosync. Please see https://www.mongodb.com/zh-cn/docs/cluster-to-cluster-sync/current/reference/verification/
for alternative verification methods.
Do you want to continue? (y/n):

如果您已阅读并接受免责声明,则可以使用 选项启动mongosync --acceptDisclaimer,以跳过此通知。

mongosync 在源集群和目标集群之间同步集合数据。mongosync 不同步用户角色。因此,你可以在每个集群上创建具有不同访问权限的用户。

可以在 YAML 配置文件中设立mongosync的选项。 使用--config选项。 示例:

$ mongosync --config /etc/mongosync.conf

有关可用设置的信息,请参阅配置

Mongosync 支持分片的集群之间的复制。mongosync 将单个分片从源集群并行复制到目标集群。但是,mongosync 不会保留源集群的分片配置。

重要

You must always disable the balancer on a sharded destination cluster by using balancerStop. After stopping the balancer, wait fifteen minutes before starting mongosync. This gives the cluster time to finish any in-progress chunk migrations.

如果源集群或目标集群是分片集群,并且您未运行带命名空间筛选功能的mongosync ,则必须通过运行balancerStop 命令并等待15 分钟以使命令完成来禁用源集群的负载均衡器。

如果源集群或目标集群是分片集群,并且您正在运行带有命名空间筛选的 mongosync,则可以全局启用源集群的负载均衡器,但必须为命名空间过滤内的所有集合禁用它。请参阅在 Filtered Sync 中禁用集合的负载均衡器。您还可以完全禁用源集群的负载均衡器。

During migration, do not run the moveChunk or moveRange commands. If you have enabled the source cluster's balancer, but disabled it for collections within the namespace filter, do not run shardCollection on collections within the namespace filter. If you run shardCollection on collections within the namespace filter during the migration, mongosync returns an error and stops, which requires you to start the migration from scratch.

注意

mongosync 不支持迁移到具有预配置的分片区域标签的目标命名空间。在开始迁移之前,从目标上 mongosync 将迁移到的任何命名空间中删除所有分片标签范围或区域。您可以在迁移达到 COMMITTED 状态后重新添加所需的区域范围。

从版本 1.17 开始,如果检测到未禁用负载均衡器,则 mongosync 会在初始化期间禁用源集群和目标集群上的负载负载均衡器。

这仅适用于初始化期间。如果 mongosync 在迁移开始后检测到任一负载负载均衡器已启用,则 mongosync 会失败。

禁用负载均衡器后,mongosync 等待 15 分钟,确保正在进行的数据块迁移完成,然后再继续迁移。

如果迁移不可逆,并且 mongosync 在初始化期间禁用了源负载均衡器或目标负载均衡器,则在成功提交后,mongosync 会重新启用被禁用的负载均衡器。如果迁移是可逆的,mongosync 则不会重新启用任何负载均衡器,以免用户等待 15 分钟。

IMPORTANT: If mongosync disables the balancer for either cluster and then fails before commit, you must re-enable the balancer(s) manually by using the balancerStart database command if you do not plan to run mongosync again.

如果您正在使用命名空间过滤并希望为命名空间过滤之外的集合启用源集群的负载均衡器,请在启动 mongosync 之前按照这些说明进行操作。

1

Before starting mongosync with a namespace filter, enable the balancer for the source cluster by running the sh.startBalancer() method in mongosh.

2

Disable the balancer for each collection within the namespace filter by running the setAllowMigrations command:

db.adminCommand(
{
setAllowMigrations: “<db>.<collection>”,
allowMigrations: false
}
)

对命名空间过滤中的每个集合运行上述命令。

重要

如果启用源集群的负载均衡器但不使用命名空间过滤,或者没有为命名空间过滤内的所有集合禁用负载均衡器,则 mongosync 会失败。

mongosync 同步到分片的目标集群时,它会为目标集群上的分片的集合预分割数据段。对于每个分片的集合,mongosync 尝试创建 90 数据段。

重要

Even if the source cluster is balanced, mongosync doesn't ensure balance of the destination cluster. Because mongosync doesn't support the execution of sharding operations during migration, you must wait until it is safe to accept writes to rebalance the destination cluster. See Sharded Cluster Balancer for guidance on how to rebalance the cluster and sharded cluster limitations for information on sharded cluster limitations in mongosync.

mongosync 不保留从源到目标的数据块分布,即使有多个 mongosync 实例也是如此。 无法在目标集群上重现来自源集群的数据段的特定预分割。

mongosync 从源集群到目标集群保留的唯一分片配置是分片键。 迁移完成后,您可以启用目标集群的负载均衡器,该负载均衡器的文档分发独立于源集群的分布。

当您同步到分分片的目标集群时,mongosync 会通过循环方式向每个数据库分配一个主分片分片。

警告

Running movePrimary on the source or destination cluster during migration may result in a fatal error or require you to restart the migration from the start. For more information, see Sharded Clusters.

从 8.0 开始,MongoDB引入对配置分片集群(也称为嵌入式配置服务器集群)的支持。

mongosync 支持从专用配置服务器分片的片集群到嵌入式配置服务器分片的片集群的同步,反之亦然。此外,mongosync 支持从副本集到配置分片的集群的同步,但反之则不然。

要学习;了解有关嵌入式配置服务器的更多信息,请参阅配置分片。

要将源集群同步到多个目标集群,请为每个目标集群使用一个 mongosync 实例。有关更多信息,请参阅多集群限制

从 1.3.0 开始,Mongosync 支持固定大小集合,但有一些限制。

从 1.20.0 开始,启动 mongosync 时必须传递 --enableCappedCollectionHandling 标志,启用在迁移期间创建新的固定大小集合。

同步期间,源集群上的固定大小集合可正常工作。

同步期间,目标集群上的固定大小集合会出现临时更改:

  • 文档数量没有上限。

  • 最大集合大小为 1PB。

mongosync 在提交期间恢复最大文档数量和最大文档大小的原始值。

mongosync 根据迁移配置在内部管理源集群和目标集群上的写入阻塞:

配置
源写入被阻止
目标写入被阻止

默认

源集群运行 6.0 之前的MongoDB

No

已启用命名空间筛选(在 /start请求中指定了 includeNamespacesexcludeNamespaces

No

已启用预先存在的目标数据(在 /start请求中将 preExistingDestinationData设立为 true

No

No

mongosync 调用/提交后阻止源写入。除非迁移因托管错误而失败,否则源写入将保持阻塞状态。如果 mongosync 在提交期间意外终止,您可以通过运行以下命令来手动解除对源集群的写入阻止:

db.adminCommand(
{
setUserWriteBlockMode: 1,
global: false
}
)

For more information, see setUserWriteBlockMode.

mongosync 在调用/start后阻止目标写入。目标写入保持阻塞状态,直到 /progress 响应中的 canWritetrue

The mongosync user must have a role that includes the setUserWriteBlockMode and bypassWriteBlockingMode ActionTypes.

注意

Write blocking only applies to users that do not have the bypassWriteBlockingMode ActionType. Users who have this ActionType can perform writes.

始终允许对源集群执行读取操作。

/progress 端点报告 canWritetrue 时,源集群和目标集群上的数据是一致的。

如需查看 mongosync 所处的状态,请调用 /progress API 端点。/progress 输出包括一个布尔值 canWrite

  • canWritetrue 时,可安全地写入目标集群。

  • canWritefalse 时,不向目标集群写入。

mongosync 在同步时,您可以安全地写入源集群。除非 canWritetrue,否则不要写入目标集群。

By default, mongosync sets the read concern level to "majority" for reads on the source cluster. For writes on the destination cluster, mongosync sets the write concern level to "majority" with j: true.

有关读关注和写关注配置和行为的更多信息,请参阅读关注写关注。

mongosync requires the primary read preference when connecting to the source and destination clusters. For more information, see Read Preference Options.

mongosync 将传统索引值(如 0 或空字符串)重写为目标上的 1mongosync 还会删除目标上的任何无效索引选项。

mongosync replication is different from replication of data within a Replica Set. mongosync combines and reorders writes from the source to destination cluster during the sync, and also temporarily modifies various collection characteristics.

因此,在同步仍在执行时(包括同步暂停时),不能保证目标在任何点都与源集群匹配。为确保目标集群和源集群在切换前匹配,请调用提交端点。

除非您使用反向功能,否则源集群和目标集群之间的关系在提交时终止。有关中间同步约束的信息,请参阅限制条件的限制。

重要

在您对 commit 调用 mongosynccanWrite 成功返回 true 之前,目标集群上的已迁移集合不能用于接受应用程序读取或写入流量。请勿使用 mongosync 维护用于灾难恢复、分析或其他类似使用案例的从节点(secondary node from replica set)集群。

mongosync 在同步期间临时更改以下集合特征。 原始值会在提交进程中恢复。

更改
说明

Unique Indexes

源集群上的唯一索引将作为目标集群上的非唯一索引进行同步。

TTL Indexes

同步将 expireAfterSeconds 设置为目标集群上的 MAX_INT 的值。

Hidden Indexes

同步会将隐藏索引复制为非隐藏索引。

写入阻塞

mongosync 根据您的迁移配置在内部管理写入阻塞。

To learn more, see Write Blocking.

固定大小集合

同步会将固定大小集合设置为允许的最大大小。

虚拟索引

在某些情况下,同步可能会在目标上创建虚拟索引,以支持对分分片的或排序规则集合的写入。

mongosync does not support rolling index builds during migration. To avoid building indexes in a rolling fashion during migration, use one of the following methods to ensure that your destination indexes match your source indexes:

  • 迁移前在源上构建索引。

  • Build the index on the source during migration with a default index build.

  • 迁移后在目标上构建索引。

mongosync 在迁移期间将其元数据存储在一个或多个数据库中。元数据数据库可以命名为以下任何名称:

  • __mdb_internal_mongosync

  • 任何以 __mdb_internal_mongosync_verifier

成功迁移后,应删除所有元数据数据库。删除元数据后,无法反向迁移。

mongosync supports eventual consistency on the destination cluster. Read consistency is not guaranteed on the destination cluster until commit. Before committing, the source and destination clusters may differ at a given point in time. To learn more, see Mid-sync Considerations.

mongosync进行同步时, mongosync可能会在将写入从源中继到目标时对写入进行重新排序或组合。 对于给定文档,源和目标之间的写入总数可能不同。

事务可能不会自动出现在目标集群上。 可重试写入可能无法在目标集群上重试。

如果在源数据库上启用了分析, MongoDB会创建一个名为 <db>.system.profile 的特殊集合。同步完成后,Mongosync 不会从目标中删除 <db>.system.profile集合,即使稍后删除源数据库也是如此。<db>.system.profile集合不会改变目标上用户数据的准确性。

如果将带视图的数据库拖放到源上,则目标可能会在该数据库中显示空system.views集合。空system.views集合不会改变目标上用户数据的准确性。

Mongosync 不会将系统集合复制到目标集群。

If you issue a dropDatabase command on the source cluster, this change is not directly applied on the destination cluster. Instead, Mongosync drops user collections and views in the database on the destination cluster, but it does not drop system collections on that database.

例如,在目标集群上:

  • The drop operation does not affect a user-created system.js collection.

  • If you enable profiling, the system.profile collection remains.

  • If you create views on the source cluster and then drop the database, replicating the drop removes the views, but leaves an empty system.views collection.

在这些情况下,复制 dropDatabase 时会删除数据库中所有用户创建的集合,但会将其系统集合留在目标集群上。

mongosync creates collections with new UUIDs on the destination cluster. There is no relationship between UUIDs on the source cluster and the destination cluster. If applications contain hard-coded UUIDs (which MongoDB does not recommend), you may need to update those applications before they work properly with the migrated cluster.

mongosync 以未定义的顺序在目标集群上插入文档,这不会保留源集群的自然排序顺序。如果应用程序依赖于文档顺序,但没有定义的排序方法,则您可能需要更新这些应用程序以指定预期的排序顺序,才能使应用程序与迁移的集群正常工作。

MongoDB Atlas和其他云服务将基本数据存储在集群内的专用内部数据库中。这些数据库以 __mdb_internal1开头。从.18 开始,mongosync 忽略所有以__mdb_internal 开头的数据库。

1.19版本新增。

如果源集群运行的是MongoDB6.0 或更高版本,或者您在运行buildIndexes afterDataCopy时将excludeHashedAfterCopy 设立为 或start ,则mongosync 会在数据复制后在目标集群上构建索引。在调用createIndexes 时,它会以编程方式计算适当的批处理大小,以便在MongoDB Atlas上托管的目标集群上构建索引。对于非 Atlas 集群, 会采用默认批处理大小mongosync 2,因为它无法可靠地评估为索引构建分配的内存。

您可以在 mongosync 日志中搜索“createIndexes batch size”以查找计算出的批处理大小。

要显式设立索引构建的批处理大小,请参阅 --createIndexesBatchSize 配置选项。

mongosync 通过同时发出12createIndexes 命令并依赖目标集群的maxNumActiveUserIndexBuilds 参数来限制并发createIndexes 命令的数量,从而自动并行化Atlas目标集群上的索引构建。具有预先存在的数据的非 Atlas 目标集群具有串行索引构建,以避免目标 CPU 过载。如果您有非 Atlas 目标集群,并在调用preExistingDestinationData truestart端点时将 设立为 ,则无法更新索引构建并行度。

To explicitly tune mongosync's index build parallelism, adjust the maxNumActiveUserIndexBuilds setting on the destination cluster. Increasing this value improves overall index build speeds but raises CPU load on the destination. Monitor CPU utilization closely to avoid performance degradation and unintended auto-scaling events, which can occur when the cluster's CPU utilization exceeds these limits.

mongosync 具有弹性,能够处理非致命错误。包含“错误”或“故障”字样的日志并不表明 mongosync 正在发生故障或损坏数据。例如,如果发生网络错误,mongosync 日志可能会包含“错误”字样,但 mongosync 仍能完成同步。如果同步未完成,mongosync 会写入一个致命错误日志条目。

Using DDL operations (operations that act on collections or databases such as db.createCollection() and db.dropDatabase()) during sync increase the risk of migration failure and may negatively impact mongosync performance. For best performance, refrain from performing DDL operations on the source cluster while the sync is in progress.

有关 DDL 操作的更多信息,请参阅待处理 DDL 操作和事务

迁移组件之间的网络延迟或较长的物理距离可能会对同步速度产生负面影响。

mongosync 和目标分片之间的延迟
对于源集群上的每个操作,mongosync 都会两次往返目标服务器。 延迟越大,同步越慢。
目标分片之间的延迟
mongosync 在目标集群上的ACID 事务中运行操作并分批更新自己的元数据。 这可能会导致跨分片事务,如果分片相距较远,则成本可能更高。
源集群或目标集群上任何副本集的节点之间的延迟
mongosync uses "majority" writes and "majority" reads, which require acknowledgement from multiple nodes in a replica set, including shard-backing replica sets. If the majority of these nodes aren't in the same region, there will be negative performance implications.

以下注意事项与 mongosync进程期间的中断有关。

如果 mongosync 在同步过程中遇到错误或变得不可用,您也可以从停止的地方恢复 mongosync 操作。 mongosync 二进制文件是无状态的,用于存储目标集群重启所需的元数据。

要继续同步,请在 mongosync 再次可用后重新启动它,并使用与中断的同步相同的参数。 重新启动 mongosync 后,进程会从停止处恢复。

如果源集群或目标集群发生意外崩溃,您可以恢复迁移,而无需从头开始重新启动。集群重新在线后,使用原始迁移中的相同连接字符串参数重新启动 mongosync binary。mongosync 从目标集群读取其存储状态,并自动恢复同步。

要验证同步已经恢复,请调用 /progress 终结点并确认 state 返回 RUNNING

警告

如果源集群服务中断超过 oplog windowmongosync 将无法恢复。您必须从头开始 重新启动迁移,这样会从目标集群中删除所有已迁移的数据。

如果mongosync 处于 PAUSED 状态,则mongosync 不支持以下操作:

  • 升级源集群或目标集群的MongoDB版本

  • 启用然后禁用负载均衡器

您可以在 mongosync 处于 PAUSED 状态时对其升级。