https://www.jianshu.com/p/d839119b1d15
1.分片数据迁移
检查 balancer 是否开启 (必须为开启)
mongos> sh.getBalancerState()
true
mongos> sh.getBalancerState()
true
查看分片状态
mongos> db.printShardingStatus()
mongos> db.printShardingStatus()
分片迁移数据块
在 admin 数据库中,运行 removeShard 命令.运行之后会开始将这个分片的数据块”转移”到其他分片的过程
mongos> use admin
mongos> db.runCommand( { removeShard: "shard1" } )
mongos> use admin
mongos> db.runCommand( { removeShard: "shard1" } )
检查迁移状态
mongos> use admin
mongos> db.runCommand( { removeShard: "shard1" } )
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(4255),
"dbs" : NumberLong(6),
"jumboChunks" : NumberLong(0)
},
。。。。。。
remaining 必须迁移到其他分片的数据块中剩余的数据块数量
mongos> use admin
mongos> db.runCommand( { removeShard: "shard1" } )
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(4255),
"dbs" : NumberLong(6),
"jumboChunks" : NumberLong(0)
},
。。。。。。
remaining 必须迁移到其他分片的数据块中剩余的数据块数量
*注意*: 在分片迁移未完成之前不要执行,数据库到另一个分片
数据库迁移到另一个分片
mongos> db.printShardingStatus()
mongos> db.printShardingStatus()
将数据库迁移到另一个分片,需要使用 movePrimary. 将所有的剩余的未分片的数据从 shard1 迁移至 shard2 上
mongos> db.runCommand( { movePrimary: "products", to: "shard2" })
{ "primary" : "mongodb1", "ok" : 1 }
mongos> db.runCommand( { movePrimary: "products", to: "shard2" })
{ "primary" : "mongodb1", "ok" : 1 }
结束删除分片的过程,再次执行 removeShard 命令
mongos> db.runCommand( { removeShard: "shard1" } )
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shard1",
"ok" : 1
}
#至此结束分片迁移
mongos> db.runCommand( { removeShard: "shard1" } )
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "shard1",
"ok" : 1
}
#至此结束分片迁移