mongodb分片balance

查看balance状态

mongos> sh.getBalancerState()
true

 

通过balance锁查看balance活动

如果state是2,表示balance锁已经被获取

mongos> db.locks.find({"_id" : "balancer"}).pretty()
{
        "_id" : "balancer",
        "state" : 2,
        "who" : "db10:4001:1427696724:1804289383:Balancer:1681692777",
        "ts" : ObjectId("5519158885c46501a2380c4a"),
        "process" : "db10:4001:1427696724:1804289383",
        "when" : ISODate("2015-03-30T09:21:12.689Z"),
        "why" : "doing balance round"
}

 

设置balance窗口

db.settings.update({ _id : "balancer" }, { $set : { activeWindow : { start : "23:00", stop : "6:00" } } }, true )

查看

mongos> sh.getBalancerWindow()
{ "start" : "23:00", "stop" : "22:00" }

 

 

手动执行balance

mongos> sh.startBalancer()

 

 

集合级别启用、禁用balance

sh.enableBalancing("test.doc2")

sh.disableBalancing("test.doc2")

查看

mongos> db.getSiblingDB("config").collections.findOne({_id : "test.doc2"})
{
        "_id" : "test.doc2",
        "lastmod" : ISODate("2015-03-30T06:40:58.730Z"),
        "dropped" : false,
        "key" : {
                "_id" : "hashed"
        },
        "unique" : false,
        "lastmodEpoch" : ObjectId("5518effa291477d087fdc6fe"),
        "noBalance" : true
}

 

 

当发现数据分配不平均时

如:

mongos> sh.status()
--- Sharding Status ---
  sharding version: {

...

...

test.doc0
               shard key: { "int1" : 1, "int2" : 1 }
               chunks:
                    rs0     1
                    rs1     1
                    rs2     1
               { "int1" : { "$minKey" : 1 }, "int2" : { "$minKey" : 1 } } -->> { "int1" : 0, "int2" : 1 } on : rs1 Timestamp(2, 0)
               { "int1" : 0, "int2" : 1 } -->> { "int1" : 0, "int2" : 9 } on : rs2 Timestamp(3, 0)
               { "int1" : 0, "int2" : 9 } -->> { "int1" : { "$maxKey" : 1 }, "int2" : { "$maxKey" : 1 } } on : rs0 Timestamp(3, 1)

...

...

上面的doc0,有3个chunk,其中rs0上的chunk包含了从0,9一直到最大值的所有数据。

此时可以split这个chunk

mongos> sh.splitAt( "test.doc0", { int1:30, int2:30 });
{ "ok" : 1 }
mongos> sh.splitAt( "test.doc0", { int1:60, int2:60 });
{ "ok" : 1 }

 

 

手工移动chunk

通过find条件,将这条记录所在的chunk移动到指定rs

mongos> db.runCommand({moveChunk:"test.doc0", find:{int1:30, int2:30}, to:"rs1"})
{ "millis" : 11137, "ok" : 1 }

 

posted @ 2020-04-22 15:10  cchilei  阅读(383)  评论(0编辑  收藏  举报