There are two ways to synchronize sharded clusters. You can use either one mongosync or several mongosync instances. For best performance with large or heavily loaded clusters, use one mongosync instance for each shard on the source cluster.
重要
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 实例
To configure a single mongosync, follow the connection instructions for your cluster architecture to connect to the mongos instance in your cluster.
When you connect a single mongosync to a sharded cluster do not use the replicaSet option or the id option.
本页的其余部分将介绍如何使用多个mongosync实例进行集群到集群的同步。
配置多个 mongosync 实例
mongosync实例的数量必须与源集群上的分片数量相匹配。 您必须在所有实例之间使用相同版本的mongosync 。 对于副本集源,您只能使用一个mongosync实例。
当您配置多个mongosync实例在分片集群之间同步时,您必须向每个mongosync实例发送相同的 API 端点命令。
要配置多个mongosync实例:
确定分片 ID
To get the shard IDs, connect to the source cluster mongos and run the listShards command.
db.adminCommand( { listShards: 1 } )
该信息位于shards数组中。
shards: [ { _id: 'shard01', host: 'shard01/localhost:27501,localhost:27502,localhost:27503', state: 1, topologyTime: Timestamp({ t: 1656612236, i: 2 }) }, { _id: 'shard02', host: 'shard02/localhost:27504,localhost:27505,localhost:27506', state: 1, topologyTime: Timestamp({ t: 1656612240, i: 4 }) } ]
连接 mongosync 实例
这些说明使用通用连接字符串。 要修改集群架构的连接字符串,请参阅架构特定的连接详细信息。
提示
单个主机服务器可以运行多个mongosync实例。 要提高性能,请在多个主机服务器上运行mongosync 。
运行第一个mongosync实例:
mongosync \ --cluster0 "mongodb://user:password@cluster0host:27500" \ --cluster1 "mongodb://user:password@cluster1host:27500" \ --id shard01 --port 27601
运行多个mongosync实例时,实例数量必须等于分片数量。 每个mongosync实例都必须使用--id选项或id设置启动,以指定其复制的分片。
为源集群中的每个分片运行新的mongosync实例。 为每个额外的mongosync实例编辑--id和--port字段。
mongosync \ --cluster0 "mongodb://user:password@cluster0host:27500" \ --cluster1 "mongodb://user:password@cluster1host:27500" \ --id shard02 --port 27602
和--cluster0 --cluster1mongos选项的连接字符串应指向实例。在此示例中,它们使用相同的mongos实例。
每个mongosync实例:
连接到源集群中的
mongos实例。连接到目标集群中的
mongos实例。从由
--id选项标识的源集群复制单个分片。指定同步期间使用的唯一端口。请考虑指定范围端口以简化 Mongosync 操作的脚本编写。
mongosync启动多个 实例
使用curl或其他 HTTP 客户端向每个mongosync实例发出启动命令。
curl mongosync01Host:27601/api/v1/start -XPOST --data \ '{ "source": "cluster0", "destination": "cluster1", \ "reversible": false }' curl mongosync02Host:27602/api/v1/start -XPOST --data \ '{ "source": "cluster0", "destination": "cluster1", \ "reversible": false }'
所有mongosync实例的start命令选项必须相同。
检查进度
要查看特定分片的同步进度,请使用curl或其他 HTTP 客户端向同步该分片的mongosync实例发出进度命令。
curl mongosync02Host:27602/api/v1/progress -XGET
此命令检查在mongosync02Host上运行并使用port 27602进行同步的mongosync实例的进度。 要检查其他分片的进度,请更新主机和端口号,然后对每个mongosync实例重复执行 API 调用。
暂停 mongosync 实例
Pause命令将暂时停止单个分片上的同步进程。 它不会暂停可能正在运行的任何其他mongosync实例。 使用curl或其他 HTTP 客户端向mongosync实例发出pause命令。
curl mongosync01Host:27601/api/v1/pause -XPOST --data '{}'
此命令暂停在mongosync01Host上运行并使用port 27601进行同步的mongosync实例。 要暂停其他分片上的同步,请更新主机和端口号,然后对每个mongosync实例重复执行 API 调用。
恢复同步
如果一个或多个mongosync实例暂停,您可以使用恢复命令来恢复同步。 对每个暂停的mongosync实例运行单独的resume命令以继续同步。
使用curl或其他 HTTP 客户端向每个mongosync实例发出恢复命令。
curl mongosync01Host:27601/api/v1/resume -XPOST --data '{}'
此命令可在mongosync01Host上运行并使用port 27601的mongosync实例上恢复同步。 要在其他分片上恢复同步,请更新主机和端口号,然后对每个mongosync实例重复执行 API 调用。
从多个mongosync 实例提交同步
当您想要完成同步时,请发出进度命令并检查canCommit和lagTimeSeconds的值。
为了最大限度地减少源集群上的写入阻塞,应仅在lagTimeSeconds值对于应用程序而言足够小时时运行commit命令。
如果lagTimeSeconds值足够小,并且canCommit为true ,则发出提交命令以提交同步。 对所有mongosync实例重复该过程。
commit操作处于阻塞状态。 直到对每个mongosync实例调用了commit后, commit命令才会返回。
// Check progress curl mongosync01Host:27601/api/v1/progress -XGET // Commit curl mongosync01Host:27601/api/v1/commit -XPOST --data '{}'
这些命令仅检查在mongosync01Host上运行并使用port 27601的mongosync实例的进度和提交同步。 要同步所有分片,请在任何其他可能正在运行的mongosync实例上额外调用progress和commit 。
数据验证
在将应用程序负载从源集群转移到目标集群之前,请检查数据以确保同步成功。
有关详细信息,请参阅验证数据传输。
反转同步方向
要反向同步,使原始目标集群充当源集群:
向每个
mongosync实例发出反向命令。
reverse操作处于阻塞状态。 直到对每个mongosync实例调用了reverse后, reverse命令才会返回。
curl mongosync01Host:27601/api/v1/reverse -XPOST --data '{}'
此命令可反转在mongosync01Host上运行并使用port 27601的mongosync实例的同步。 对任何其他可能正在运行的mongosync实例额外调用reverse 。
注意
只有在启动API启动 时将reversible true设立为mongosync ,才能实现反向同步。