mongosync实用程序在MongoDB集群之间创建连接。 mongosync可以在以下各项之间建立连接:
MongoDB Atlas 托管集群,或
自管理集群和 Atlas 托管集群。
本页提供使用 MongoDB Cluster-to-Cluster Sync 连接 Atlas 集群的说明。 有关连接到 Atlas 集群的更多详细信息,请参阅连接到数据库部署
连接字符串(Connection Strings)
mongosync 使用MongoDB URI 连接string连接Atlas集群:
SRV 连接方案的形式为:
mongodb+srv://[username:password@][host.domain.TLD][:port][/defaultauthdb][?options] 有关如何在 中查找 SRV 连接string Atlas的信息,请参阅 连接到集群。
标准 URI 连接方案的形式如下:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
按照副本集配置中列出的方式指定mongod实例的主机名。
对于分片的集群,请指定mongos实例的主机名,而不是mongod实例的主机名。
注意
mongosync 不需要replicaSet选项。
身份验证
提供有效的身份验证以连接到MongoDB Atlas集群。 如果您还没有Atlas数据库用户,则必须创建一个用户。
角色
mongosync连接字符串中指定的用户必须对源集群和目标集群具有所需的权限。 这些权限会有所不同,具体取决于您的环境以及您要运行写入阻塞还是反向同步。
Atlas 权限包括:
同步类型 | 目标 | 所需权限 |
|---|---|---|
访问 | 源集群 |
|
访问 | 目标集群 |
|
写入阻塞或反转 | 源集群 |
|
写入阻塞或反转 | 目标集群 |
|
有关Atlas角色的详细信息,请参阅: Atlas user角色。
要更新Atlas user权限,请参阅:管理对项目的访问权限。
行为
托管
mongosync实用程序可以托管在靠近源集群或目标集群的自己的硬件上。 它不必与集群中的mongod或mongos实例之一托管在同一服务器上。 这种灵活性允许您将数据推送或拉取到目标集群,同时对在那里运行的mongod或mongos实例的影响最小。
初始状态(Initial State)
当mongosync连接时,它处于IDLE状态。 您必须发出启动命令才能开始同步。
限制
在尝试使用 M10+ Atlas 集群运行 mongosync 之前,请禁用 Require Indexes for All Queries(需要所有查询的索引)选项。
例子
收集连接信息
源集群cluster0托管在以下服务器和端口上:
clusterOne-shard-00-00.abc12.mongodb.net:27017
clusterOne-shard-00-01.abc12.mongodb.net:27017
clusterOne-shard-00-02.abc12.mongodb.net:27017
目标集群cluster1托管在以下服务器和端口上:
clusterTwo-shard-00-00.abc12.mongodb.net:27017
clusterTwo-shard-00-01.abc12.mongodb.net:27017
clusterTwo-shard-00-02.abc12.mongodb.net:27017
每个集群上都配置了一个管理用户clusterAdmin ,密码为superSecret 。
连接源集群和目标集群 mongosync
使用收集的连接信息为cluster0和cluster1创建连接字符串:
cluster0: mongodb+srv://clusterAdmin:superSecret@clusterOne.abc12.mongodb.net cluster1: mongodb+srv://clusterAdmin:superSecret@clusterTwo.abc12.mongodb.net
注意
下面的mongosync命令布局已修改以进行显示。 要使用mongosync将cluster0连接到cluster1 ,请在一行中输入以下命令:
mongosync \ --cluster0 "mongodb+srv://clusterAdmin:superSecret@clusterOne.abc12.mongodb.net" \ --cluster1 "mongodb+srv://clusterAdmin:superSecret@clusterTwo.abc12.mongodb.net"