定义
addShardToZone将分片与区域关联。 MongoDB 将此分片与给定区域关联。 该区域覆盖的数据块将分配给与该区域关联的分片。
提示
In
mongosh, this command can also be run through thesh.addShardToZone()helper method.Helper methods are convenient for
mongoshusers, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.
You can only run addShardToZone on the admin database from a mongos instance.
兼容性
此命令可用于以下环境中托管的部署:
- MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务
MongoDB Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
语法
该命令具有以下语法:
db.adminCommand( { addShardToZone: <string>, zone: <string> } )
命令字段
该命令接受以下字段:
字段 | 类型 | 说明 |
|---|---|---|
字符串 | The name of the shard to associate with the zone. | |
字符串 | The name of the zone to associate with the shard. |
mongosh provides the helper method sh.addShardToZone().
行为
您可以将一个区域与多个分片关联,一个分片也可以与多个区域关联。
有关分片集群中区域的更多信息,请参阅区域手册页面。
范围
MongoDB 实际上会忽略至少没有一个与之关联的分片键值范围的区域。
要将分片键值范围与区域关联,请使用updateZoneKeyRange数据库命令或sh.updateZoneKeyRange() Shell 助手。
您可以在未分片的集合或不存在的集合上运行 updateZoneKeyRange 数据库命令及其助手 sh.updateZoneKeyRange() 和 sh.addTagRange()。
提示
通过对空集合或不存在的集合进行分片之前定义区域和区域范围,分片集合操作会为定义的区域范围创建数据块以及任何其他数据块,以覆盖分片键值的整个范围,并执行基于区域范围的初始数据块分配。数据块的初始创建和分布可以更快地设置区域分片。在初始分布之后,负载均衡器将管理未来的数据段分布。
有关示例,请参阅为空集合或不存在的集合预先定义区域和区域范围。
安全性
对于强制执行访问控制的分片集群,您必须以具有以下任一权限的用户身份进行身份验证:
针对
update在config数据库的shardscollection的 ;或者,enableSharding集群 资源上的 。
The clusterAdmin or clusterManager built-in roles have the appropriate permissions for issuing addShardToZone. See the documentation page for Role-Based Access Control for more information.
例子
以下示例将shard0000与区域JFK关联:
db.adminCommand( { addShardToZone : "shard0000" , zone : "JFK" } )
一个分片可以与多个区域关联。 以下示例将LGA与shard0000关联:
db.adminCommand( { addShardToZone : "shard0000" , zone : "LGA" } )
shard0000 同时与LGA区域和JFK区域关联。 在均衡集群中,MongoDB 会将任一区域覆盖的读取和写入路由到shard0000 。