管理分片标记¶
在集群中,可以使用标记将一段范围内的 shard key 与特定的一个或多个 shard 相关联.
标记一个分片¶
连接到 mongos 之后,使用 sh.addShardTag() 将标记与分片相关联.一个分片可以与多个标记相关联,一个标记也可以关联到多个分片.
示例
下面的示例将标记 NYC 关联到两个分片上,而将标记 SFO 与 NRT 关联到第三个分片上.
sh.addShardTag("shard0000", "NYC")
sh.addShardTag("shard0001", "NYC")
sh.addShardTag("shard0002", "SFO")
sh.addShardTag("shard0002", "NRT")
连接到 mongos 之后,可以使用 sh.removeShardTag() 将分片上的标记删除,以下的例子从一个分片上将标记 NRT 删除:
sh.removeShardTag("shard0002", "NRT")
标记片键范围¶
在连接到 mongos 后使用 sh.addTagRange() 将一段片键范围与一个标记相关联.每个指定的片键范围只能与 一个 标记相关联,定义的范围不能重叠,也不能对同一个片键范围定义两次.
示例
有一个名字为 records 的数据库,其中有一个集合 users 使用 zipcode 做片键,以下的操作将分配:
在 Manhattan 与 Brooklyn 的两个邮编范围与标记 NYC 相关联.
在 San Francisco 的一个邮编范围与标记 SFO 相关联.
sh.addTagRange("records.users", { zipcode: "10001" }, { zipcode: "10281" }, "NYC")
sh.addTagRange("records.users", { zipcode: "11201" }, { zipcode: "11240" }, "NYC")
sh.addTagRange("records.users", { zipcode: "94102" }, { zipcode: "94135" }, "SFO")
注解
标记的片键范围包含了最小值,不包含最大值.
使用片键范围删除相应的标记¶
The mongod does not provide a helper for removing a tag range. You may delete tag assignment from a shard key range by removing the corresponding document from the tags collection of the config database.
Each document in the tags holds the namespace of the sharded collection and a minimum shard key value.
示例
以下示例删除了 Manhattan 中邮编范围相关联的 NYC 标记.
use config
db.tags.remove({ _id: { ns: "records.users", min: { zipcode: "10001" }}, tag: "NYC" })
查看存在的标记¶
sh.status() 显示了每个分片与之关联的标记.分片的标记信息存储在 config database 数据库的 shards 集合中.使用以下方法查找与标记 NYC 关联的所有分片:
use config
db.shards.find({ tags: "NYC" })
在 config 数据库的 tags 集合中存储有所有 namespaces 的标记信息,:method:sh.status()`显示了所有的标记信息.如果想查看所有与 ``NYC` 标记相关联的分片,使用以下操作:
use config
db.tags.find({ tags: "NYC" })