文档菜单
文档首页
/
MongoDB 手册
/ / /

移动集合

本页内容

  • 关于此任务
  • 访问控制
  • 开始之前
  • 步骤
  • 了解更多

从MongoDB 8.0版本开始,您可以使用moveCollection命令将未分片的集合移动到不同的分片。moveCollection命令。

如果您的部署启用了访问控制,则enableSharding角色会授予您运行moveCollection命令的权限。

在您移动集合之前,请确保您满足以下要求

  • 您的应用程序可以容忍两秒钟的写入阻塞时间。在此期间,您的应用程序将经历延迟增加。

  • 您的数据库满足以下资源要求

    • 确保您要移动集合到的分片有足够的空间来存储集合及其索引。目标分片需要至少(集合存储大小 + 索引大小) * 2字节可用。

    • 确保您的I/O容量低于50%。

    • 确保您的CPU负载低于80%。

重要

数据库不会强制执行这些要求。资源分配不足可能导致以下情况:

  • 数据库空间不足并关闭

  • 性能下降

  • 操作时间超过预期

如果您的应用程序有时间流量较少的时段,尽可能在此时段内对集合执行此操作。

1

要将名为 inventory 的未分片集合从 app 数据库移动到 shard02 分片,请运行 moveCollection

db.adminCommand(
{
moveCollection: "app.inventory",
toShard: "shard02"
}
)

要获取可用的分片ID列表,请运行 sh.status()。有关详细信息,请参阅 sh.status() 输出。

2
  1. 监视剩余时间。

    要监视 moveCollection 操作的剩余时间,请使用 $currentOp 管道阶段。

    此示例展示了如何检查 app.inventory 集合上 moveCollection 的进度。

    db.getSiblingDB("admin").aggregate( [
    { $currentOp: { allUsers: true, localOps: false } },
    {
    $match: {
    type: "op",
    "originatingCommand.reshardCollection": "app.inventory"
    }
    }
    ] )

    注意

    要查看更新后的值,您需要持续运行前面的管道。

    $currentOp 管道输出

    • totalOperationTimeElapsedSecs:操作已消耗的时间(秒)

    • remainingOperationTimeEstimatedSecs:当前 moveCollection 操作的剩余时间(秒)。当一个新的 moveCollection 操作开始时,它返回 -1

    注意

    remainingOperationTimeEstimatedSecs 已设置为悲观时间估计

    • 追赶阶段时间估计设置为克隆阶段时间,这
      是一个相对较长时间。
    • 实际上,如果只有少量待写操作,则
      实际追赶阶段时间相对较短。

    此管道阶段输出类似于以下内容

    [
    {
    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. 监控传输的字节数。

    要监控传输的字节数,请使用 shardingStatistics.resharding.active.bytesCopied 并将其与集合中的字节数进行比较。

3

要确认集合已移动到预期的分片,请使用 $collStats 管道阶段。

此示例展示了如何确认 app.inventory 集合存在于预期的分片上

db.inventory.aggregate( [
{ $collStats: {} },
{ $project: { "shard": 1 } }
] )

此管道阶段输出类似于以下内容

[ { shard: 'shard02' } ]

返回

可移动集合