mongodb分片balance

liiinuuux發表於2015-04-03
檢視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 } } --&gt> { "int1" : 0, "int2" : 1 } on : rs1 Timestamp(2, 0)
               { "int1" : 0, "int2" : 1 } --&gt> { "int1" : 0, "int2" : 9 } on : rs2 Timestamp(3, 0)
               { "int1" : 0, "int2" : 9 } --&gt> { "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 }





來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26239116/viewspace-1485441/,如需轉載,請註明出處,否則將追究法律責任。

相關文章