sh.addShardToZone()

On this page本页内容

Definition定义

sh.addShardToZone(shard, zone)

Associates a shard with a zone. 将分片与分区相关联。MongoDB associates this shard with the given zone. MongoDB将此分片与给定区域相关联。Chunks that are covered by the zone are assigned to shards associated with the zone.分区覆盖的区块被指定给与分区关联的分片。

Parameter参数Type类型Description描述
shardstring

The name of the shard to which to associate the zone.要与分区关联的分片的名称。

zonestring

The name of the zone to associate with the shard.要与分片关联的区域的名称。

Only issue sh.addShardToZone() when connected to a mongos instance.仅在连接到mongos实例时发出sh.addShardToZone()

Behavior行为

You can associate a zone with multiple shards, and a shard can associate with multiple zones.您可以将一个分区与多个分片相关联,一个分片可以与多个分区相关联。

See the zone manual page for more information on zones in sharded clusters.有关分片集群中分区的详细信息,请参阅分区手册页面。

Ranges范围

MongoDB effectively ignores zones that do not have at least one range of shard key values associated with it.MongoDB实际上忽略了没有至少一个范围的分片键值与其关联的区域。

To associate a range of shard key values with a zone, use the sh.updateZoneKeyRange() method.要将一系列分片键值与区域关联,请使用sh.updateZoneKeyRange()方法。

Starting in MongoDB 4.0.2, you can run updateZoneKeyRange database command and its helpers sh.updateZoneKeyRange() and sh.addTagRange() on an unsharded collection or a non-existing collection.从MongoDB 4.0.2开始,您可以对未存储的集合或不存在的集合运行updateZoneKeyRange数据库命令及其助手sh.updateZoneKeyRange()sh.addTagRange()

Tip提示

Changed in version 4.0.3.在版本4.0.3中更改

By defining the zones and the zone ranges before sharding an empty or a non-existing collection, the shard collection operation creates chunks for the defined zone ranges as well as any additional chunks to cover the entire range of the shard key values and performs an initial chunk distribution based on the zone ranges. 通过在对空集合或不存在的集合进行分片之前定义区域和区域范围,分片集合操作将为定义的区域范围以及任何其他块创建块,以覆盖分片键值的整个范围,并基于区域范围执行初始块分配。This initial creation and distribution of chunks allows for faster setup of zoned sharding. 块的初始创建和分发允许更快地设置分区分片。After the initial distribution, the balancer manages the chunk distribution going forward.初始分发之后,平衡器将继续管理区块分发。See Pre-Define Zones and Zone Ranges for an Empty or Non-Existing Collection for an example.有关示例,请参阅空集合或不存在集合的预定义分区和分区范围

Security安全

For sharded clusters that enforce access control, you must authenticate as a user whose privileges include either:对于强制访问控制的分片群集,您必须作为具有以下权限之一的用户进行身份验证:

  • update on the shards collection in the config database; or, alternatively,更新config数据库中的shards集合;或者,作为替代方案,
  • enableSharding on the cluster resource (Starting in version 4.2.2, 4.0.14, 3.6.16).集群资源上enableSharding(从版本4.2.2、4.0.14、3.6.16开始)。

The clusterAdmin or clusterManager built-in roles have the appropriate permissions for issuing sh.addShardToZone(). clusterAdminclusterManager内置角色具有发出sh.addShardToZone()的相应权限。See the Role-Based Access Control manual page for more information.有关详细信息,请参阅基于角色的访问控制手册页。

Example示例

The following example adds three zones, NYC, LAX, and NRT, associating each to a shard:以下示例添加了三个分区:NYCLAXNRT,将每个分区与一个分片相关联:

sh.addShardToZone("shard0000", "JFK")
sh.addShardToZone("shard0001", "LAX")
sh.addShardToZone("shard0002", "NRT")

A shard can associate with multiple zones. 分片可以与多个区域关联。The following example associates LGA to shard0000:以下示例将LGAshard0000关联:

sh.addShardToZone("shard0000", "LGA")

shard0000 associates with both the LGA zone and the JFK zone. shard0000LGA区域和JFK区域都关联。In a balanced cluster, MongoDB routes reads and writes covered by either zone to shard0000.在平衡集群中,MongoDB将任一区域覆盖的读写路由到shard0000

Tip提示
See also: 参阅:
←  sh.addShardTag()sh.addTagRange() →