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

removeShard(数据库命令)

removeShard

警告

从MongoDB 8.3 开始,removeShard 命令已弃用,并将在未来发布中删除。

分片排干进程已替换为以下命令:

分片耗尽后,您可以使用 commitShardRemoval 命令将其删除。

有关详细信息,请参阅从分片集群中删除分片。

从分片集群中删除分片。当您运行removeShard 时, MongoDB会使用负载均衡器将该分分片移动到集群中的其他分片,从而清空该分片。

注意

必须启用负载均衡器,removeShard 才能完成。如果禁用负载均衡器,则即使您已经将所有数据移出分片,该命令仍处于排干状态并且无法完成。

如果分片具有未分片集合,则必须从分片清空所有未分片集合。从MongoDB 8.0 开始,您可以使用 sh.moveCollection() 移动未分片的集合。有关分片过程的详细信息,请参阅从分片集群中删除分片。

一旦分片被清空, MongoDB就会从集群中删除分片。

注意

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

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

注意

This command is not supported in MongoDB Atlas. See Modify your Atlas Sharded Cluster to add or remove shards from your Atlas cluster.

该命令具有以下语法:

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

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

You can have more than one removeShard operation in progress.

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

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

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

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

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

如果您在集群执行 DDL 操作(修改集合,如reshardCollection的操作)时运行removeShard ,则removeShard仅在并发 DDL 操作完成后才会执行。

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 call moveCollection for collectionsToMove and afterwards movePrimary for the dbsToMove",
"dbsToMove" : [
"fizz",
"buzz"
],
"collectionsToMove" : [
"fizz.coll1",
"buzz.coll1"
],
"ok" : 1,
"operationTime" : Timestamp(1575398919, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1575398919, 2),
"signature" : {
"hash" : BinData(0,"Oi68poWCFCA7b9kyhIcg+TzaGiA="),
"keyId" : Long("6766255701040824328")
}
}
}

负载均衡器开始将数据块从名为 bristol01 的分片迁移(“排干”)到集群中的其他分片。迁移很慢,以避免给集群带来过度的负载。

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),
"collectionsToMove": Long(2)
},
"note" : "you need to call moveCollection for collectionsToMove and afterwards movePrimary for the dbsToMove",
"dbsToMove" : [
"fizz",
"buzz"
],
"collectionsToMove" : [
"fizz.coll1",
"buzz.coll1"
],
"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 标记后,负载均衡器可以迁移这些数据段。有关迁移过程的详细信息,请参阅范围迁移过程。

collectionsToMove

需要从分片移动的未分片集合的总数。

estimatedRemainingBytes

正在移除的分片上估计剩余字节数。

运行 transitionToDedicatedConfigServer 时,removeShard 输出可能包括以下字段。这些字段仅在负载均衡器将所有数据块和数据库从配置服务器移出后才会出现:

字段
说明

pendingRangeDeletions

正在移除的分片上剩余的待处理范围删除数量。

firstNonEmptyCollection

正在移除的分片上的第一个非空集合。

Continue checking the status of the removeShard command (that is, 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),
"collectionsToMove": Long(2)
},
"note" : "you need to call moveCollection for collectionsToMove and afterwards movePrimary for the dbsToMove",
"dbsToMove" : [
"fizz",
"buzz"
],
"collectionsToMove" : [
"fizz.coll1",
"buzz.coll1"
],
"ok" : 1,
"operationTime" : Timestamp(1575400343, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1575400343, 1),
"signature" : {
"hash" : BinData(0,"9plu5B/hw4uWAgEmjjBP3syw1Zk="),
"keyId" : Long("6766255701040824328")
}
}
}

负载均衡器从分片清空所有数据段后,您可能需要手动将集合和数据库从已清空的分片移出。

如果 removeShard 输出包含 collectionsToMove字段中的集合,则使用 moveCollection 将这些集合移动到排干分片以外的分片或删除集合(这会删除关联的数据文件)。

如果 removeShard 输出包含 dbsToMove字段中的数据库,则对这些数据库使用 movePrimary 或删除数据库(这会删除关联的数据文件)。

注意

为获得最佳性能,请在移动数据库之前移动集合。

After the balancer completes moving all chunks off the shard and you have handled the dbsToMove and collectionsToMove, 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")
}
}
}
给本页内容打分