对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

removeShard(数据库命令)

removeShard

Removes a shard from a sharded cluster. When you run removeShard, MongoDB drains the shard by using the balancer to move the shard's chunks to other shards in the cluster. Once the shard is drained, MongoDB removes the shard from the cluster.

注意

如果要将已删除的分片重新添加到分片集群,则必须清除该分片的所有节点的storage.dbPath 以删除该分片的文件,然后才能重新添加。

此命令可用于以下环境中托管的部署:

注意

MongoDB Atlas不支持此命令。请参阅修改Atlas分片集群以在Atlas 集群中添加或删除分片。

该命令具有以下语法:

db.adminCommand(
{
removeShard : <shardToRemove>
}
)

在删除分片期间无法备份集群数据。

You can have more than one removeShard operation in progress.

如果已启用authorization ,则必须具有clusterManager角色或任何包含removeShard操作的角色。

分片集群中的每个数据库都有一个主分片。如果要删除的分片也是集群数据库的主分片,则必须在从分片迁移所有数据后,手动将数据库移动到新分片。 有关更多信息,请参阅movePrimary命令和从现有分片集群中删除分片

当您从集群中删除数据段分布不均匀的分片时,负载均衡器首先从要清空的分片中删除数据段,然后均衡剩余的不均匀数据段分布。

mongos converts the write concern of the removeShard command to "majority".

分片删除可能会导致打开的更改流游标关闭,而关闭的更改流游标可能无法完全恢复。

From mongosh, the removeShard operation resembles the following:

db.adminCommand( { removeShard : "bristol01" } )

Replace bristol01 with the name of the shard to remove. When you run removeShard, the command returns with a message that resembles the following:

{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "bristol01",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"fizz",
"buzz"
],
"ok" : 1,
"operationTime" : Timestamp(1575398919, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1575398919, 2),
"signature" : {
"hash" : BinData(0,"Oi68poWCFCA7b9kyhIcg+TzaGiA="),
"keyId" : Long("6766255701040824328")
}
}
}

负载均衡器开始将数据块从名为bristol01的分片迁移(“排干”)到集群中的其他分片。这些迁移缓慢进行,以避免对集群造成过度负载。

输出包含dbsToMove字段,该字段指示以bristol01主分片的数据库。在所有数据块从分片中耗尽后,您必须对数据库执行movePrimary,或者选择删除这些数据库。

注意

If the shard you are removing is not the primary shard for any database, the dbsToMove array will be empty and removeShard can complete the migration without intervention.

If you run the command again, removeShard returns the current status of the process. For example, if the operation is in an ongoing state, the command returns an output that resembles the following:

{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : Long(2),
"dbs" : Long(2),
"jumboChunks" : Long(0)
},
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"fizz",
"buzz"
],
"ok" : 1,
"operationTime" : Timestamp(1575399086, 1655),
"$clusterTime" : {
"clusterTime" : Timestamp(1575399086, 1655),
"signature" : {
"hash" : BinData(0,"XBrTmjMMe82fUtVLRm13GBVtRE8="),
"keyId" : Long("6766255701040824328")
}
}
}

在输出中,remaining 字段包括以下字段:

字段
说明

chunks

分片当前剩余的数据段总数。

dbs

主分片是分片的数据库总数。这些数据库在 dbsToMove 输出字段中指定。

jumboChunks

chunks的总数中,该数字是巨型数据。

如果 jumboChunks 大于 0,则等到分片上只剩下 jumboChunks。一旦只剩下巨型块,必须手动清除巨型标志,然后才能完成排干。请参阅 清除巨型标志。

清除 jumbo 标记后,负载均衡器可以迁移这些数据段。有关迁移过程的详细信息,请参阅范围迁移过程

Continue checking the status of the removeShard command (i.e. rerun the command) until the number of chunks remaining is 0.

{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : Long(0), // All chunks have moved
"dbs" : Long(2),
"jumboChunks" : Long(0)
},
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"fizz",
"buzz"
],
"ok" : 1,
"operationTime" : Timestamp(1575400343, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1575400343, 1),
"signature" : {
"hash" : BinData(0,"9plu5B/hw4uWAgEmjjBP3syw1Zk="),
"keyId" : Long("6766255701040824328")
}
}
}

从分片中耗尽所有数据块后,如果有dbsToMove ,则可以对这些数据库执行movePrimary ,或者删除数据库(此操作会删除关联的数据文件)。

After the balancer completes moving all chunks off the shard and you have handled the dbsToMove, removeShard can finish. Running removeShard again returns output that resembles the following:

{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "bristol01",
"ok" : 1,
"operationTime" : Timestamp(1575400370, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1575400370, 2),
"signature" : {
"hash" : BinData(0,"JjSRciHECXDBXo0e5nJv9mdRG8M="),
"keyId" : Long("6766255701040824328")
}
}
}
给本页内容打分