1. 程式人生 > >MongoDb雙機房容災啦

MongoDb雙機房容災啦

mongo 提供服務 conf conda ons 單機 還在 ORC eps


機房意外斷電斷網不得不預防,下面模擬測試某機房斷電斷網,B機房斷電斷網後A機房可正常提供服務,A機房斷電斷網後可能需要強制重啟繼續提供服務了,目前查看數據都還在,暫時沒驗證是否有數據丟失,小試了一把。。。
大概架構
A機房
192.168.70.214:27017 (為primary,其他節點都是SECONDARY)
192.168.70.215:27017
192.168.70.216:27017

B機房
192.168.71.214:27017
192.168.71.215:27017

下面重點記錄下A機房斷電斷網,B機房怎麽重啟繼續提供服務,其實主要就3步,見粗體
repset:SECONDARY> use admin


switched to db admin
repset:SECONDARY> cfg=rs.conf()
{
"_id" : "repset",
"version" : 79,
"members" : [
{
"_id" : 4,
"host" : "192.168.71.214:27017",
"priority" : 10
},
{
"_id" : 5,
"host" : "192.168.71.215:27017",
"priority" : 9
},
{
"_id" : 7,
"host" : "192.168.70.214:27017",
"priority" : 11
},
{
"_id" : 8,
"host" : "192.168.70.215:27017",
"priority" : 6
},
{
"_id" : 13,
"host" : "192.168.70.216:27017",
"priority" : 5
}
]
}

repset:SECONDARY> cfg.members = [cfg.members[0], cfg.members[1]]
[
{
"_id" : 4,
"host" : "192.168.71.214:27017",
"priority" : 10
},
{
"_id" : 5,
"host" : "192.168.71.215:27017",
"priority" : 9
}
]

repset:SECONDARY> rs.reconfig(cfg, {force :true })
{ "ok" : 1 }
repset:SECONDARY> rs.status()
{
"set" : "repset",
"date" : ISODate("2018-11-09T03:45:29Z"),
"myState" : 1,
"members" : [
{
"_id" : 4,
"name" : "192.168.71.214:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 69133,
"optime" : Timestamp(1541663971000, 1),
"optimeDate" : ISODate("2018-11-08T07:59:31Z"),
"self" : true
},
{
"_id" : 5,
"name" : "192.168.71.215:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8,
"optime" : Timestamp(1541663971000, 1),
"optimeDate" : ISODate("2018-11-08T07:59:31Z"),
"lastHeartbeat" : ISODate("2018-11-09T03:45:29Z"),
"pingMs" : 0
}
],
"ok" : 1
}
repset:PRIMARY>
repset:PRIMARY> use test
switched to db test
repset:PRIMARY> show collections
system.indexes
test
testdb
repset:PRIMARY> rs.conf()
{
"_id" : "repset",
"version" : 38342,
"members" : [
{
"_id" : 4,
"host" : "192.168.71.214:27017",
"priority" : 10
},
{
"_id" : 5,
"host" : "192.168.71.215:27017",
"priority" : 9
}
]
}

從上面觀察驗證:新選的PRIMARY可以持續提供服務了,之前的數據都還在,解決了單機房斷電斷網的故障。
歡迎各位提供指導,共同成長。

MongoDb雙機房容災啦