Docs HomeMongoDB Manual

Migrate Ranges in a Sharded Cluster迁移分片群集中的范围

In most circumstances, you should let the automatic balancer migrate ranges between shards. However, you may want to migrate ranges manually in a few cases:在大多数情况下,应该让自动平衡器分片之间迁移范围。但是,在以下几种情况下,您可能需要手动迁移范围:

  • When pre-splitting an empty collection, migrate ranges manually to distribute them evenly across the shards. 预拆分空集合时,请手动迁移范围,以便将它们均匀地分布在分片上。Use pre-splitting in limited situations to support bulk data ingestion.在有限的情况下使用预拆分来支持批量数据接收。
  • If the balancer in an active cluster cannot distribute ranges within the balancing window, then you will have to migrate ranges manually.如果活动集群中的平衡器无法在平衡窗口内分配范围,则必须手动迁移范围。

To manually migrate ranges, use the moveChunk or moveRange command.要手动迁移范围,请使用moveChunkmoveRange命令。

For more information on how the automatic balancer moves ranges between shards, see Balancer Internals and Range Migration.有关自动平衡器如何在分片之间移动范围的更多信息,请参阅平衡器内部范围迁移

For more information on tuning the migration, see chunkMigrationConcurrency.有关调整迁移的更多信息,请参阅chunkMigrationConcurrency

Example

Migrate a single range迁移单个范围

The following example assumes that the field username is the shard key for a collection named users in the myapp database, and that the value smith exists within the range to migrate. 以下示例假设字段usernamemyapp数据库中名为users的集合的分片键,并且值smith存在于要迁移的范围内。Migrate the range using the following command in mongosh.使用mongosh中的以下命令移植范围。

db.adminCommand( { moveChunk : "myapp.users",
find : {username : "smith"},
to : "mongodb-shard3.example.net" } )

This command moves the range that includes the shard key value "smith" to the shard named mongodb-shard3.example.net. 此命令将包含分片键值“smith”的范围移动到名为mongodb-shard3.example.net分片The command will block until the migration is complete.该命令将被阻止,直到迁移完成。

Tip

To return a list of shards, use the listShards command.要返回分片列表,请使用listShards命令。

Example

Evenly migrate ranges均匀迁移范围

To evenly migrate ranges for the myapp.users collection, put each prefix range on the next shard from the other and run the following commands in the mongo shell:要均匀地迁移myapp.users集合的范围,请将每个前缀范围放在另一个分片的下一个分片上,并在mongo shell中运行以下命令:

var shServer = [ "sh0.example.net", "sh1.example.net", "sh2.example.net", "sh3.example.net", "sh4.example.net" ];
for ( var x=97; x<97+26; x++ ){
for( var y=97; y<97+26; y+=6 ) {
var prefix = String.fromCharCode(x) + String.fromCharCode(y);
db.adminCommand({moveChunk : "myapp.users", find : {email : prefix}, to : shServer[(y-97)/6]})
}
}

See Create Ranges in a Sharded Cluster for an introduction to pre-splitting.有关预拆分的介绍,请参阅在分片群集中创建范围

  • Use the moveRange command with the _secondaryThrottle and writeConcern fields to determine when the balancer proceeds with the next document in the migrating range.moveRange命令与_secondaryThrottlewriteConcern字段一起使用,以确定平衡器何时继续迁移范围中的下一个文档。
  • Use the moveRange command with the secondaryThrottle and writeConcern fields to determine when the balancer proceeds with the next document in the migrating range.moveRange命令与secondaryThrottlewriteConcern字段一起使用,以确定平衡器何时继续迁移范围中的下一个文档。

See moveRange and moveRange for details.有关详细信息,请参阅moveRangemoveRange

Change Streams and Orphan Documents更改流和孤立文档

Starting in MongoDB 5.3, during range migration, change stream events are not generated for updates to orphaned documents.从MongoDB 5.3开始,在范围迁移过程中,不会为孤立文档的更新生成更改流事件。