定义
removeShardRemoves 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 Enterprise:基于订阅、自我管理的 MongoDB 版本
MongoDB Community:源代码可用、免费使用且可自行管理的 MongoDB 版本
注意
MongoDB Atlas不支持此命令。请参阅修改Atlas分片集群以在Atlas 集群中添加或删除分片。
语法
该命令具有以下语法:
db.adminCommand( { removeShard : <shardToRemove> } )
行为
在分片删除期间没有集群备份
在删除分片期间无法备份集群数据。
并发 removeShard 操作
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".
Change Streams
分片删除可能会导致打开的更改流游标关闭,而关闭的更改流游标可能无法完全恢复。
例子
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 字段包括以下字段:
字段 | 说明 |
|---|---|
| 分片当前剩余的数据段总数。 |
| 主分片是分片的数据库总数。这些数据库在 |
|
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") } } }