removeShard

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就会从集群中删除分片。

Syntax语法

To run, from a mongos instance, issue the command against the admin database:要从mongos实例运行,请针对admin数据库发出命令:

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

Behavior行为

Concurrent removeShard Operations并发removeShard操作

Starting in MongoDB 4.4 (and 4.2.1), you can have more than one removeShard operation in progress.从MongoDB 4.4(和4.2.1)开始,可以进行多个removeShard操作。

In MongoDB 4.2.0 and earlier, removeShard returns an error if another removeShard operation is in progress.在MongoDB 4.2.0及更早版本中,如果正在进行另一个removeShard操作,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 an Existing 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.当您移除集群中具有不均匀块分布的分片时,平衡器首先从排出的分片中移除块,然后平衡剩余的不均匀块分配。

Tip提示
See also: 参阅:

Write Concern写入关注

mongos converts the write concern of the removeShard command to "majority".removeShard命令的写入关注点转换为"majority"

Change Streams更改流

A shard removal may cause an open change stream cursor to close, and the closed change stream cursor may not be fully resumable.分片移除可能会导致打开的更改流游标关闭,而关闭的更改流游标可能无法完全恢复。

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. 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,或者删除这些数据库。

Note注意

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字段包括以下字段:

Field字段Description描述
chunksTotal number of chunks currently remaining on the shard.分片上当前剩余的块总数。
dbsTotal number of databases whose primary shard is the shard. 主分片为分片的数据库总数。These databases are specified in the dbsToMove output field. 这些数据库在dbsToMove输出字段中指定。
jumboChunks

Of the total number of chunks, the number that are jumbo.chunks的总数中,jumbo的数量。

If the jumboChunks is greater than 0, wait until only the jumboChunks remain on the shard. 如果jumboChunks大于0,请等到分片上只剩下jumboChunkOnce only the jumbo chunks remain, you must manually clear the jumbo flag before the draining can complete. 一旦只剩下巨型块,就必须手动清除巨型标记,然后才能完成排出。See Clear jumbo Flag.请参阅清除jumbo标记

After the jumbo flag clears, the balancer can migrate these chunks. jumbo标志清除后,平衡器可以迁移这些块。However, if the queue of writes that modify any documents being migrated surpasses 500MB of memory the migration will fail. 但是,如果修改正在迁移的任何文档的写入队列超过500MB内存,则迁移将失败。For details on the migration procedure, see Chunk Migration Procedure.有关迁移过程的详细信息,请参阅区块迁移过程

Available starting in 4.2.2 (and 4.0.14)从4.2.2(和4.0.14)开始提供

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")
      }
   }
}
←  refineCollectionShardKeyremoveShardFromZone →