In most circumstances, you should let the automatic balancer migrate chunks between shards. 在大多数情况下,应该让自动平衡器在分片之间迁移块。However, you may want to migrate chunks manually in a few cases:但是,在某些情况下,您可能希望手动迁移块:
To manually migrate chunks, use the 要手动迁移块,请使用moveChunk
command. moveChunk
命令。For more information on how the automatic balancer moves chunks between shards, see Cluster Balancer and Chunk Migration.有关自动平衡器如何在分片之间移动块的更多信息,请参阅群集平衡器和块迁移。
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 chunk to migrate. username
是myapp
数据库中名为users的集合的分片键,并且要迁移的块中存在值smith
。Migrate the chunk using the following command in 在mongosh
.mongosh
中使用以下命令迁移块。
db.adminCommand( { moveChunk : "myapp.users", find : {username : "smith"}, to : "mongodb-shard3.example.net" } )
This command moves the chunk that includes the shard key value "smith" to the shard named 此命令将包含分片键值“smith”的块移动到名为mongodb-shard3.example.net
. mongodb-shard3.example.net
的分片。The command will block until the migration is complete.该命令将阻塞,直到迁移完成。
To return a list of shards, use the 要返回分片列表,请使用listShards
command.listShards
命令。
To evenly migrate chunks for the 要均匀迁移myapp.users
collection, put each prefix chunk 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 Chunks in a Sharded Cluster for an introduction to pre-splitting.有关预拆分的介绍,请参阅在分片群集中创建区块。
The moveChunk
command has the: _secondaryThrottle
parameter and the writeConcern
parameter that determines when the balancer proceeds with the next document in the migrating chunk. moveChunk
命令包含:_secondaryThrottle
参数和writeConcern
参数,用于确定平衡器何时处理迁移区块中的下一个文档。See 有关详细信息,请参阅moveChunk
command for details.moveChunk
命令。
The moveChunk
command may produce the following error message:moveChunk
命令可能会产生以下错误消息:
The collection's metadata lock is already taken.集合的元数据锁已被占用。
This occurs when clients have too many open cursors that access the migrating chunk. 当客户端有太多打开的游标访问迁移块时,就会发生这种情况。You may either wait until the cursors complete their operations or close the cursors manually.您可以等待游标完成操作,也可以手动关闭游标。