Remove Shards from an Existing Sharded Cluster从现有分片群集中删除分片
On this page本页内容
To remove a shard you must ensure the shard's data is migrated to the remaining shards in the cluster. 要移除分片,必须确保分片的数据迁移到集群中的其余分片。This procedure describes how to safely migrate data and how to remove a shard.此过程描述了如何安全地迁移数据以及如何删除分片。
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.当您在具有不均匀块分布的集群中移除一个分片时,平衡器首先从排出的分片中移除块,然后平衡剩余的不均匀块分配。
This procedure describes how to remove a shard from a cluster. 此过程描述如何从集群中移除分片。Do not use this procedure to migrate an entire cluster to new hardware. 不要使用此过程将整个群集迁移到新硬件。To migrate, see Migrate a Sharded Cluster to Different Hardware instead.要迁移,请参阅将分片群集迁移到不同的硬件。
To remove a shard, first connect to one of the cluster's 要移除分片,首先使用mongos
instances using mongosh
. mongosh
连接到集群的一个mongos
实例。Then use the sequence of tasks in this document to remove a shard from the cluster.然后使用本文档中的任务序列从集群中移除分片。
Considerations注意事项
A shard removal may cause an open change stream cursor to close, and the closed change stream cursor may not be fully resumable.分片移除可能会导致打开的变更流游标关闭,并且关闭的变更流游标可能无法完全恢复。You can safely restart a cluster during a shard removal process.您可以在分片移除过程中安全地重新启动集群。If you restart a cluster during an ongoing draining process, draining continues automatically after the cluster components restart.如果在正在进行的排放过程中重新启动群集,则在群集组件重新启动后,排放将自动继续。MongoDB records the shard draining status in theMongoDB在config.shards
collection.config.shards
集合中记录分片排出的状态。
Ensure the Balancer Process is Enabled确保平衡器进程已启用
To successfully migrate data from a shard, the balancer process must be enabled. 要成功地从分片迁移数据,必须启用平衡器进程。Check the balancer state using the 使用sh.getBalancerState()
helper in mongosh
. mongosh
中的sh.getBalancerState()
助手检查平衡器状态。For more information, see the section on balancer operations.有关更多信息,请参阅平衡器操作部分。
Determine the Name of the Shard to Remove确定要删除的分片的名称
To determine the name of the shard, connect to a 要确定分片的名称,请使用mongos
instance with mongosh
and either:mongosh
连接到mongos
实例,然后:
Use the使用listShards
command, as in the following:listShards
命令,如下所示:db.adminCommand( { listShards: 1 } )
Run either the运行sh.status()
or thedb.printShardingStatus()
method.sh.status()
或db.printShardingStatus()
方法。
The shards._id
field lists the name of each shard.shards._id
字段列出了每个分片的名称。
Remove Chunks from the Shard从分片中移除大块
From the 从管理数据库中,运行admin
database, run the removeShard
command. removeShard
命令。This begins "draining" chunks from the shard you are removing to other shards in the cluster. 这开始将要移除的分片中的块“排出”到集群中的其他分片中。For example, for a shard named 例如,对于名为mongodb0
, run:mongodb0
的分片,运行:
db.adminCommand( { removeShard: "mongodb0" } )
mongos
converts the write concern of the removeShard
command to "majority"
.mongos
将removeShard
命令的写入关注点转换为"majority"
。
This operation returns with the following response:此操作返回并返回以下响应:
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "mongodb0",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"fiz",
"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 chunks from the shard named 平衡器开始将块从名为mongodb0的分片迁移到集群中的其他分片。这些迁移进行得很慢,以避免给整个集群带来不必要的负载。mongodb0
to other shards in the cluster. These migrations happens slowly to avoid placing undue load on the overall cluster. Depending on your network capacity and the amount of data, this operation can take from a few minutes to several days to complete.根据您的网络容量和数据量,此操作可能需要几分钟到几天的时间才能完成。
The output includes the field 输出包括dbsToMove
indicating the databases, if any, for which the shard is the primary shard. dbsToMove
字段,该字段指示分片是其主要分片的数据库(如果有的话)。After all chunks have been drained from the shard, you must either 从分片中排出所有区块后,必须为数据库movePrimary
for the database(s) or alternatively, drop the databases (which deletes the associated data files).movePrimary
,或者删除数据库(删除相关的数据文件)。
Check the Status of the Migration检查迁移状态
To check the progress of the migration at any stage in the process, run 要检查迁移过程中任何阶段的进度,请再次从removeShard
from the admin
database again. For example, for a shard named mongodb0
, run:admin
数据库运行removeShard
。例如,对于名为mongodb0
的分片,运行:
db.adminCommand( { removeShard: "mongodb0" } )
mongos
converts the write concern of the removeShard
command to "majority"
.mongos
将removeShard
命令的写入关注点转换为"majority"
。
The command returns output similar to the following:该命令返回类似于以下内容的输出:
{
"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 | chunks , the number that are jumbo.chunks 的总数中,为jumbo 的数目。jumboChunks is greater than 0, wait until only the jumboChunks remain on the shard. jumboChunks 大于0,请等待直到只有jumboChunk 保留在分片上。jumbo chunks remain, you must manually clear the jumbo flag before the draining can complete. jumbo Flag.jumbo 标志。jumbo flag clears, the balancer can migrate these chunks. jumbo 标志后,平衡器可以迁移这些块。 |
Continue checking the status of the 继续检查removeShard
command until the number of chunks remaining is 0
.removeShard
命令的状态,直到剩余的块数为0
。
db.adminCommand( { removeShard: "mongodb0" } )
Move Databases to Another Primary Shard将数据库移动到另一个主分片
If the shard is the primary shard for one or more databases in the cluster, then you must make that database use a different shard as its primary shard. 如果分片是集群中一个或多个数据库的主分片,则必须使该数据库使用不同的分片作为其主分片。removeShard
lists any databases that you need to move in the 在命令输出的dbsToMove
field in the command output. dbsToMove
字段中列出了需要移动的所有数据库。If the shard is not the primary shard for any databases, skip to the next task, Finalize the Migration.如果分片不是任何数据库的主分片,请跳到下一个任务完成迁移。
To move a database to another shard, use the 要将数据库移动到另一个分片,请使用movePrimary
command.movePrimary
命令。
To ensure a smooth migration, refer to the considerations in the movePrimary
command documentation before running movePrimary
.
To migrate the fizz
database from mongodb0
to mongodb1
, issue the following command:
db.adminCommand( { movePrimary: "fizz", to: "mongodb1" })
mongos
uses "majority"
write concern for movePrimary
.
This command does not return until MongoDB completes moving all data. 在MongoDB完成所有数据的移动之前,此命令不会返回。The response from this command will resemble the following:此命令的响应如下所示:
{
"ok" : 1,
"operationTime" : Timestamp(1575400369, 9),
"$clusterTime" : {
"clusterTime" : Timestamp(1575400369, 9),
"signature" : {
"hash" : BinData(0,"2Nz8QCcVXB0LJLm1hsXfpTCaM0M="),
"keyId" : NumberLong("6766255701040824328")
}
}
}
Using movePrimary
To Move Unsharded Collections使用movePrimary
移动未硬盘的集合
movePrimary
To Move Unsharded CollectionsFor MongoDB 4.2 and previous, if using the movePrimary
command on a database that contains an unsharded collection, you must perform the following additional steps.
MongoDB 4.4 does not require these additional steps when moving databases that contain unsharded collections.当移动包含未排序集合的数据库时,MongoDB 4.4不需要这些额外的步骤。
- For MongoDB 4.2, you must either:
- Restart all
mongos
instances and allmongod
shard members (including the secondary members); - Use the
flushRouterConfig
command on allmongos
instances and allmongod
shard members (including the secondary members) before reading or writing any data to any unsharded collections that were moved.
- Restart all
- For MongoDB 4.0 and earlier, you must either:
- Restart all
mongos
instances; - Use the
flushRouterConfig
command on allmongos
instances before reading or writing any data to any unsharded collections that were moved.
- Restart all
These steps ensure that all cluster nodes refresh their metadata cache, which includes the location of the primary shard. 这些步骤确保所有集群节点刷新其元数据缓存,其中包括主分片的位置。Otherwise, you may miss data on reads, and may not write data to the correct shard. To recover, you must manually intervene.否则,您可能会在读取时丢失数据,并且可能无法将数据写入正确的分片。要恢复,必须手动干预。
Finalize the Migration完成迁移
To clean up all metadata information and finalize the removal, run 要清除所有元数据信息并完成删除,请再次运行removeShard
again. For example, for a shard named mongodb0
, run:removeShard
。例如,对于名为mongodb0
的分片,运行:
db.adminCommand( { removeShard: "mongodb0" } )
mongos
converts the write concern of the removeShard
command to "majority"
.mongos
将removeShard
命令的写入关注点转换为"majority"
。
A success message appears at completion:完成时会显示一条成功消息:
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "mongodb0",
"ok" : 1,
"operationTime" : Timestamp(1575400370, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1575400370, 2),
"signature" : {
"hash" : BinData(0,"JjSRciHECXDBXo0e5nJv9mdRG8M="),
"keyId" : NumberLong("6766255701040824328")
}
}
}
Once the value of the 一旦state
field is "completed", you may safely stop the instances comprising the mongodb0
shard.state
字段的值“完成”,就可以安全地停止包含mongodb0
分片的实例。