Definition定义
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.removeShard时,MongoDB会使用平衡器将分片的块移动到集群中的其他分片,从而耗尽分片。If the shard has unsharded collections, you must drain all the unsharded collections from the shard. Starting in MongoDB 8.0, you can use如果分片有未分片的集合,则必须从分片中清除所有未分片集合。从MongoDB 8.0开始,您可以使用sh.moveCollection()to move unsharded collections.sh.moveCollection()来移动未记录的集合。For details on the procedure to drain a shard, see Remove Shards from a Sharded Cluster.有关清空分片的过程的详细信息,请参阅从分片集群中删除分片。Once the shard is drained, MongoDB removes the shard from the cluster.一旦分片被清空,MongoDB就会从集群中删除该分片。Note
If you want to re-add a removed shard to your sharded cluster, you must clear the如果你想将一个已删除的分片重新添加到分片集群中,你必须清除分片所有节点的storage.dbPathof all the nodes of the shard to remove the shard's files of before you can re-add it.storage.dbPath以删除分片的文件,然后才能重新添加它。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Note
This command is not supported in MongoDB Atlas. See Modify your Atlas Sharded Cluster to add or remove shards from your Atlas cluster.MongoDB Atlas不支持此命令。请参阅修改Atlas分片集群以添加或删除Atlas集群中的分片。
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.adminCommand(
{
removeShard : <shardToRemove>
}
)Behavior行为
No Cluster Back Ups During Shard Removal分片移除过程中无集群备份
You cannot back up the cluster data during shard removal.在分片删除期间,您无法备份集群数据。
Concurrent removeShard Operations并发removeShard操作
removeShard OperationsYou can have more than one 您可以有多个removeShard operation in progress.removeShard操作正在进行中。
Access Requirements访问要求
If you have 如果启用了authorization enabled, you must have the clusterManager role or any role that includes the removeShard action.authorization,则必须具有clusterManager角色或包含removeShard操作的任何角色。
Database Migration Requirements数据库迁移要求
Each database in a sharded cluster has a primary shard. If the shard you want to remove is also the primary of one of the cluster's databases, then you must manually move the databases to a new shard after migrating all data from the shard. 分片集群中的每个数据库都有一个主分片。如果要删除的分片也是集群数据库之一的主分片,那么在从分片迁移所有数据后,必须手动将数据库移动到新的分片。See the 有关详细信息,请参阅movePrimary command and the Remove Shards from a Sharded Cluster for more information.movePrimary命令和从分片群集中删除分片。
Chunk Balancing块平衡
When you remove a shard in a cluster with an uneven chunk distribution, the balancer first removes the chunks from the draining shard and then balances the remaining uneven chunk distribution.当你在一个块分布不均匀的集群中删除一个分片时,平衡器首先从耗尽的分片中删除块,然后平衡剩余的不均匀块分布。
Write Concern写关注
mongos converts the write concern of the 将removeShard command to "majority".removeShard命令的写关注转换为"majority"。
Change Streams更改流
Removing a shard may cause an open change stream cursor to close, and the closed change stream cursor may not be fully resumable.删除分片可能会导致打开的更改流游标关闭,而关闭的更改流游标可能无法完全恢复。
DDL OperationsDDL操作
If you run 如果在集群执行DDL操作(修改诸如removeShard while your cluster executes a DDL operation (operation that modifies a collection such as reshardCollection), removeShard only executes after the concurrent DDL operation finishes.reshardCollection之类的集合的操作)时运行removeShard,则removeShard仅在并发DDL操作完成后执行。
Example示例
From 从mongosh, the removeShard operation resembles the following:mongosh来看,removeShard操作类似于以下内容:
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:bristol01替换为要删除的分片的名称。当您运行removeShard时,该命令将返回一条类似于以下内容的消息:
{
"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")
}
}
}
The balancer begins migrating ("draining") chunks from the shard named 平衡器开始将块从名为bristol01 to other shards in the cluster. These migrations happen slowly in order to avoid placing undue load on the cluster.bristol01的分片迁移(“耗尽”)到集群中的其他分片。这些迁移过程缓慢,以避免给集群带来过重的负载。
If you run the command again, 如果再次运行该命令,removeShard returns the current status of the process. removeShard将返回进程的当前状态。For example, if the operation is in an 例如,如果操作处于ongoing state, the command returns an output that resembles the following:ongoing状态,则该命令返回类似于以下内容的输出:
{
"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")
}
}
}
In the output, the 在输出中,remaining field includes the following fields:remaining字段包括以下字段:
chunks | |
dbs | dbsToMove output field.dbsToMove输出字段中指定。 |
jumboChunks |
|
collectionsToMove |
Continue checking the status of the 继续检查removeShard command (i.e. rerun the command) until the number of chunks remaining is 0.removeShard命令的状态(即重新运行该命令),直到剩余的块数为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")
}
}
}
After the balancer drains all chunks from the shard, you may need to manually move your collections and databases out of the drained shard.在平衡器从分片中耗尽所有块后,您可能需要手动将集合和数据库移出耗尽的分片。
If the 如果removeShard output contains collections in the collectionsToMove field, use moveCollection to move those collections to a shard other than the draining shard or drop the collections (which deletes the associated data files).removeShard输出在collectionsToMove字段中包含集合,请使用moveCollection将这些集合移动到排水分片以外的分片,或删除这些集合(这将删除关联的数据文件)。
If the 如果removeShard output contains databases in the dbsToMove field, use movePrimary for those databases or drop the databases (which deletes the associated data files).removeShard输出在dbsToMove字段中包含数据库,请对这些数据库使用movePrimary或删除数据库(这将删除关联的数据文件)。
Note
For best performance, move your collections before you move your databases.为了获得最佳性能,请在移动数据库之前移动集合。
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:dbsToMove和collectionsToMove之后,removeShard就可以完成了。再次运行removeShard将返回类似于以下内容的输出:
{
"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")
}
}
}