Starting in MongoDB 8.0, you can move an unsharded collection to a different shard using the 从MongoDB 8.0开始,您可以使用moveCollection command.moveCollection命令将未分片的集合移动到其他分片。
About this Task关于此任务
moveCollectioncan only be run on sharded clusters.只能在分片集群上运行。moveCollectioncan only move unsharded collections.只能移动未录制的集合。moveCollectioncan only move a single collection at a time.一次只能移动一个集合。moveCollectionhas a 5 minute minimum duration.最小持续时间为5分钟。MongoDB Search indexes need to be rebuilt after运行moveCollectionruns.moveCollection后,需要重建MongoDB搜索索引。You cannot make topology changes, such as add or remove shard or transition between embedded and dedicated config servers, until在moveCollectioncompletes.moveCollection完成之前,您无法进行拓扑更改,例如添加或删除分片,或在嵌入式和专用配置服务器之间进行转换。You cannot run the following operations against the collection that is being moved while当moveCollectionis in progress:moveCollection正在进行时,您无法对正在移动的集合运行以下操作:You cannot run the following operations against the cluster while当moveCollectionis in progress:moveCollection正在进行时,您无法对群集运行以下操作:Index builds that occur while在moveCollectionis in progress might silently fail.moveCollection进行过程中发生的索引构建可能会自动失败。Do not create indexes whilemoveCollectionis in progress.moveCollection正在进行时,请勿创建索引。Do not call如果有正在进行的索引构建,请不要调用moveCollectionif there are ongoing index builds.moveCollection。
Access Control访问控制
If your deployment has access control enabled, the 如果部署启用了访问控制,则enableSharding role grants you access to run the moveCollection command.enableSharding角色将授予您运行moveCollection命令的权限。
Before you Begin开始之前
Before you move your collection, ensure that you meet the following requirements:在移动集合之前,请确保满足以下要求:
Your application can tolerate a period of two seconds where the affected collection blocks writes. During the time period where writes are blocked, your application experiences an increase in latency.应用程序可以容忍受影响的集合块写入的两秒钟时间。在写入被阻止的时间段内,应用程序的延迟会增加。Your database meets these resource requirements:数据库满足以下资源要求:Ensure the shard you are moving the collection to has enough storage space for the collection and its indexes. The destination shard requires at least确保将集合移动到的分片有足够的存储空间用于集合及其索引。目标分片至少需要( Collection storage size + Index Size ) * 2bytes available.( Collection storage size + Index Size ) * 2个可用字节。Ensure that your I/O capacity is below 50%.确保I/O容量低于50%。Ensure that your CPU load is below 80%.确保CPU负载低于80%。
Important
These requirements are not enforced by the database. A failure to allocate enough resources can result in:数据库不强制执行这些要求。未能分配足够的资源可能会导致:
the database running out of space and shutting down数据库空间不足并关闭decreased performance性能下降the operation taking longer than expected手术时间比预期的要长
If your application has time periods with less traffic, perform this operation on the collection during that time if possible.如果应用程序有流量较少的时间段,请在可能的情况下在该时间段内对集合执行此操作。
Steps步骤
Move the collection.移动集合。
To move an unsharded collection named 要将inventory on the app database to the shard02 shard, run moveCollection:app数据库上名为inventory的未分片集合移动到shard02分片,请运行moveCollection:
db.adminCommand(
{
moveCollection: "app.inventory",
toShard: "shard02"
}
)
To get a list of the available shard IDs, run 要获取可用分片ID的列表,请运行sh.status(). For details, see sh.status() Output.sh.status()。有关详细信息,请参阅sh.status()输出。
Monitor the progress of the moveCollection operation.监视moveCollection操作的进度。
Monitor the time remaining.监控剩余时间。To monitor the time remaining for the要监视moveCollectionoperation, use the$currentOppipeline stage.moveCollection操作的剩余时间,请使用$currentOp管道阶段。This example shows how to check the progress of此示例显示了如何检查moveCollectionon theapp.inventorycollection:app.inventory集合上moveCollection的进度:db.getSiblingDB("admin").aggregate( [
{ $currentOp: { allUsers: true, localOps: false } },
{
$match: {
type: "op",
"originatingCommand.reshardCollection": "app.inventory"
}
}
] )Note
To see updated values, you need to continuously run the preceeding pipeline.要查看更新的值,您需要连续运行前面的管道。The$currentOppipeline outputs:$currentOp管道输出:totalOperationTimeElapsedSecs: elapsed operation time in seconds:运行时间(秒)remainingOperationTimeEstimatedSecs: estimated time remaining in seconds for the current:当前moveCollectionoperation. It is returned as-1when a newmoveCollectionoperation starts.moveCollection操作的估计剩余时间(秒)。当新的moveCollection操作开始时,它将返回-1。
Note
remainingOperationTimeEstimatedSecsis set to a pessimistic time estimate:设置为悲观时间估计:-
The catch-up phase time estimate is set to the clone phase time, which追赶阶段时间估计值设置为克隆阶段时间is a relatively long time.这是一个相对较长的时间。
-
In practice, if there are only a few pending write operations, the在实践中,如果只有少数未决的写入操作actual catch-up phase time is relatively short.实际追赶阶段时间相对较短。
This pipeline stage has output similar to the following:该管道阶段的输出类似于以下内容:[
{
shard: '<shard>',
type: 'op',
desc: 'ReshardingRecipientService | ReshardingDonorService | ReshardingCoordinatorService <reshardingUUID>',
op: 'command',
ns: '<database>.<collection>',
originatingCommand: {
reshardCollection: '<database>.<collection>',
key: <shardkey>,
unique: <boolean>,
collation: { locale: 'simple' }
},
totalOperationTimeElapsedSecs: <number>,
remainingOperationTimeEstimatedSecs: <number>,
...
},
...
]Monitor the number of bytes transferred.监控传输的字节数。To monitor the number of bytes transferred, use要监视传输的字节数,请使用shardingStatistics.resharding.active.bytesCopiedand compare against the number of bytes in the collection.shardingStatistics.resharding.active.bytesCopied并与集合中的字节数进行比较。
Confirm the collection has been moved.确认集合已移动。
To confirm the collection has been moved to the expected shard, use the 要确认集合已移动到预期的分片,请使用$collStats pipeline stage.$collStats管道阶段。
This example shows how to confirm that the 此示例显示了如何确认app.inventory collection exists on the expected shard:app.inventory集合存在于预期的分片上:
db.inventory.aggregate( [
{ $collStats: {} },
{ $project: { "shard": 1 } }
] )
This pipeline stage has output similar to the following:该管道阶段的输出类似于以下内容:
[ { shard: 'shard02' } ]