Database Manual / Sharding / Data Partitioning / Moveable Collections

Move a Collection移动集合

Starting in MongoDB 8.0, you can move an unsharded collection to a different shard using the moveCollection command.从MongoDB 8.0开始,您可以使用moveCollection命令将未分片的集合移动到其他分片。

About this Task关于此任务

  • moveCollection can only be run on sharded clusters.只能在分片集群上运行。
  • moveCollection can only move unsharded collections.只能移动未录制的集合。
  • moveCollection can only move a single collection at a time.一次只能移动一个集合。
  • moveCollection has a 5 minute minimum duration.最小持续时间为5分钟。
  • MongoDB Search indexes need to be rebuilt after moveCollection runs.运行moveCollection后,需要重建MongoDB搜索索引。
  • You cannot make topology changes, such as add or remove shard or transition between embedded and dedicated config servers, until moveCollection completes.moveCollection完成之前,您无法进行拓扑更改,例如添加或删除分片,或在嵌入式和专用配置服务器之间进行转换。
  • You cannot run the following operations against the collection that is being moved while moveCollection is in progress:moveCollection正在进行时,您无法对正在移动的集合运行以下操作:

  • You cannot run the following operations against the cluster while moveCollection is in progress:moveCollection正在进行时,您无法对群集运行以下操作:

  • Index builds that occur while moveCollection is in progress might silently fail.moveCollection进行过程中发生的索引构建可能会自动失败。

    • Do not create indexes while moveCollection is in progress.moveCollection正在进行时,请勿创建索引。
    • Do not call moveCollection if 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 ) * 2 bytes 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步骤

1

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 sh.status(). For details, see sh.status() Output.要获取可用分片ID的列表,请运行sh.status()。有关详细信息,请参阅sh.status()输出。

2

Monitor the progress of the moveCollection operation.监视moveCollection操作的进度。

  1. Monitor the time remaining.监控剩余时间。

    To monitor the time remaining for the moveCollection operation, use the $currentOp pipeline stage.要监视moveCollection操作的剩余时间,请使用$currentOp管道阶段。

    This example shows how to check the progress of moveCollection on the app.inventory collection:此示例显示了如何检查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 $currentOp pipeline outputs:$currentOp管道输出:

    • totalOperationTimeElapsedSecs: elapsed operation time in seconds:运行时间(秒)
    • remainingOperationTimeEstimatedSecs: estimated time remaining in seconds for the current moveCollection operation. It is returned as -1 when a new moveCollection operation starts.:当前moveCollection操作的估计剩余时间(秒)。当新的moveCollection操作开始时,它将返回-1

    Note

    remainingOperationTimeEstimatedSecs is 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>,
    ...
    },
    ...
    ]
  2. Monitor the number of bytes transferred.监控传输的字节数。

    To monitor the number of bytes transferred, use shardingStatistics.resharding.active.bytesCopied and compare against the number of bytes in the collection.要监视传输的字节数,请使用shardingStatistics.resharding.active.bytesCopied并与集合中的字节数进行比较。

3

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' } ]

Learn More了解更多