Database Manual / Sharding / Balancer

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:在大多数情况下,您应该让自动平衡器分片之间迁移范围。但是,在某些情况下,您可能希望手动迁移范围:

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.有关自动平衡器如何在分片之间移动范围的更多信息,请参阅平衡器内部范围迁移

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. Migrate the range using the following command in mongosh.以下示例假设字段usernamemyapp数据库中名为users的集合的分片键,并且值smith存在于要迁移的范围内。在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. The command will block until the migration is complete.此命令将包含分片键值“smith”的范围移动到名为mongodb-shard3.example.net分片。该命令将一直阻塞,直到迁移完成。

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.有关预分割的介绍,请参阅在分片群集中创建范围

See moveChunk and moveRange for details.

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开始,在范围迁移期间,不会为孤立文档的更新生成更改流事件。