addShardToZone

On this page本页内容

Definition定义

addShardToZone

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.区域覆盖的块被分配给与区域关联的分片。

Syntax语法

You can only run addShardToZone on the admin database from a mongos instance.您只能从mongos实例在admin数据库上运行addShardToZone

The addShardToZone command has the following syntax:addShardToZone命令具有以下语法:

db.adminCommand(
   {
     addShardToZone: <string>,
     zone: <string>
   }
)

Fields

The command takes the following fields:该命令接受以下字段:

Field字段Type类型Description描述
addShardtoZonestring

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

zonestring

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

mongosh provides the helper method 提供了助手方法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 a sharded cluster.有关分片化集群中的区域的更多信息,请参阅区域手册页面。

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 updateZoneKeyRange database command, or the sh.updateZoneKeyRange() shell helper.要将分片键值范围与区域相关联,请使用updateZoneKeyRange数据库命令或sh.updateZoneKeyRange()shell助手。

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 (available 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 addShardToZone. clusterAdminclusterManager内置角色具有发出addShardToZone的适当权限。See the documentation page for Role-Based Access Control for more information.有关详细信息,请参阅基于角色的访问控制的文档页。

Example示例

The following example associates shard0000 with zone JFK:以下示例将shard0000与区域JFK相关联:

db.adminCommand( { addShardToZone : "shard0000" , zone : "JFK" } )

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

db.adminCommand( { addShardToZone : "shard0000" , zone : "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

←  addShardbalancerCollectionStatus →