mongodb將備機提升為主機

liiinuuux發表於2015-04-03
方法1 只需要直接將主機的配置資訊priority改成比其他節點大即可
rs0:PRIMARY> var c = rs.conf()
rs0:PRIMARY> c.members[0].priority = 2
2
rs0:PRIMARY> rs.reconfig(c)
{ "ok" : 1 }

過幾秒鐘,members[0]就變成主庫了,原主機就變成備庫了。


方法2 把不想成為主庫的節點暫時凍結。然後把主庫降級。
現有節點:
1 primary
2 secondary
3 secondary
4 arbiter

在2號執行
(凍結200秒,200秒內不能參與投票)
rs0:SECONDARY> rs.freeze(200)
{ "ok" : 1 }

在1號執行
(主庫降級為備庫,並且在200秒之內不能參與投票)
rs0:PRIMARY> rs.stepDown(200)
2015-03-18T13:54:46.217+0800 I NETWORK  DBClientCursor::init call() failed
2015-03-18T13:54:46.235+0800 E QUERY    Error: error doing query: failed
    at DBQuery._exec (src/mongo/shell/query.js:83:36)
    at DBQuery.hasNext (src/mongo/shell/query.js:240:10)
    at DBCollection.findOne (src/mongo/shell/collection.js:186:19)
    at DB.runCommand (src/mongo/shell/db.js:58:41)
    at DB.adminCommand (src/mongo/shell/db.js:66:41)
    at Function.rs.stepDown (src/mongo/shell/utils.js:1001:43)
    at (shell):1:4 at src/mongo/shell/query.js:83
2015-03-18T13:54:46.237+0800 I NETWORK  trying reconnect to 127.0.0.1:4444 (127.0.0.1) failed
2015-03-18T13:54:46.238+0800 I NETWORK  reconnect 127.0.0.1:4444 (127.0.0.1) ok


3號節點變成主庫
rs0:PRIMARY> rs.isMaster()
{
        "setName" : "rs0",
        "setVersion" : 10,
        "ismaster" : true,
        "secondary" : false,
        "hosts" : [
                "db11:4444",
                "1.1.1.101:4001",
                "1.1.1.102:4002"
        ],
        "arbiters" : [
                "1.1.1.100:4000"
        ],
        "primary" : "1.1.1.102:4002",
        "me" : "1.1.1.102:4002",
        "electionId" : ObjectId("550913287121d6dc4a60de08"),
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 1000,
        "localTime" : ISODate("2015-03-18T05:55:34.651Z"),
        "maxWireVersion" : 3,
        "minWireVersion" : 0,
        "ok" : 1
}









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

相關文章