On this page本页内容
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. removeShard
时,MongoDB通过使用平衡器将分片的块移动到集群中的其他分片来排出分片。Once the shard is drained, MongoDB removes the shard from the cluster.一旦分片耗尽,MongoDB就会从集群中删除分片。
To run, from a 要从mongos
instance, issue the command against the admin
database:mongos
实例运行,请针对admin
数据库发出命令:
db.adminCommand( { removeShard : <shardToRemove> } )
removeShard
OperationsremoveShard
操作Starting in MongoDB 4.4 (and 4.2.1), you can have more than one 从MongoDB 4.4(和4.2.1)开始,可以进行多个removeShard
operation in progress.removeShard
操作。
In MongoDB 4.2.0 and earlier, 在MongoDB 4.2.0及更早版本中,如果正在进行另一个removeShard
returns an error if another removeShard
operation is in progress.removeShard
操作,removeShard
将返回错误。
If you have 如果启用了authorization
enabled, you must have the clusterManager
role or any role that includes the removeShard
action.authorization
,则必须具有clusterManager
角色或包含removeShard
操作的任何角色。
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 an Existing Sharded Cluster for more information.movePrimary
命令和从已有的分片群集中删除分片。
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.当您移除集群中具有不均匀块分布的分片时,平衡器首先从排出的分片中移除块,然后平衡剩余的不均匀块分配。
mongos
converts the write concern of the 将removeShard
command to "majority"
.removeShard
命令的写入关注点转换为"majority"
。
A shard removal may cause an open change stream cursor to close, and the closed change stream cursor may not be fully resumable.分片移除可能会导致打开的更改流游标关闭,而关闭的更改流游标可能无法完全恢复。
From 在mongosh
, the removeShard
operation resembles the following:mongosh
中,removeShard
操作类似于以下操作:
db.adminCommand( { removeShard : "bristol01" } )
Replace 将bristol01
with the name of the shard to remove. bristol01
替换为要删除的分片的名称。When you run 运行removeShard
, the command returns with a message that resembles the following:removeShard
时,命令将返回一条类似于以下内容的消息:
{ "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" : NumberLong("6766255701040824328") } } }
The balancer begins migrating ("draining") chunks from the shard named 平衡器开始将块从名为bristol01
to other shards in the cluster. bristol01
的分片迁移(“排出”)到集群中的其他分片。These migrations happen slowly in order to avoid placing undue load on the cluster.这些迁移缓慢进行,以避免给集群带来不适当的负载。
The output includes the field 输出包括dbsToMove
indicating the databases for which bristol01
is the primary shard. dbsToMove
字段,该字段指示bristol01
是主要分片的数据库。After all chunks have been drained from the shard, you must either 从分片中排出所有块后,必须为数据库movePrimary
for the database(s) or alternatively, drop these databases.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.dbsToMove
数组将为空,removeShard
可以在不干预的情况下完成迁移。
If you run the command again, 如果再次运行该命令,removeShard
returns the current status of the process. removeShard
将返回进程的当前状态。For example, if the operaton is in an 例如,如果运算符处于ongoing
state, the command returns an output that resembles the following:ongoing
的状态,则命令返回如下输出:
{ "msg" : "draining ongoing", "state" : "ongoing", "remaining" : { "chunks" : NumberLong(2), "dbs" : NumberLong(2), "jumboChunks" : NumberLong(0) // Available starting in 4.2.2 (and 4.0.14) }, "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" : NumberLong("6766255701040824328") } } }
In the output, the 在输出中,remaining
field includes the following fields:remaining
字段包括以下字段:
chunks | |
dbs | dbsToMove output field. dbsToMove 输出字段中指定。 |
jumboChunks |
|
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" : NumberLong(0), // All chunks have moved "dbs" : NumberLong(2), "jumboChunks" : NumberLong(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" : NumberLong("6766255701040824328") } } }
After all chunks have been drained from the shard, if you have 在从分片中排出所有块之后,如果您有dbsToMove
, you can either movePrimary
for those databases or alternatively, drop the databases (which deletes the associated data files).dbsToMove
,则可以为这些数据库movePrimary
,或者删除数据库(这会删除关联的数据文件)。
After the balancer completes moving all chunks off the shard and you have handled the 在平衡器完成从分片中移除所有块并处理dbsToMove
, removeShard
can finish. dbsToMove
之后,removeShard
就可以完成了。Running 再次运行removeShard
again returns output that resembles the following: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" : NumberLong("6766255701040824328") } } }