MongoDB複製集之將現有的單節點伺服器轉換為複製集
伺服器情況:
現有的單節點 Primary 192.168.126.9:27017
新增的節點 Secondry 192.168.126.8:27017
仲裁節點 ARBITER 192.168.126.8:27018
mongo 版本 3.2.4
1.停止單節點Primary的Mongo服務
1 > use admin; 2 switched to db admin 3 > db.shutdownServer()
2.重新啟動Primary節點,使用--replSet的選項加上新複製集的名字 repl1
/usr/local/mongodb/bin/mongod -dbpath /sdb1/mongodb/data --fork --port 27017 --logpath /sdb1/mongodb/log/mongo.log --replSet repl1 --logappend
3.串連Primary資料庫
4. 建立複製集配置資訊對象
1 config = { _id:"repl1", members:[2 {_id:0,host:"192.168.126.9:27017"}3 ]4 }
5.初始化複製集並查看複製集的運行狀態
1 > rs.initiate(config) 2 { "ok" : 1 } 3 4 repl1:SECONDARY> rs.status() 5 { 6 "set" : "repl1", 7 "date" : ISODate("2016-06-23T16:05:10.228Z"), 8 "myState" : 1, 9 "term" : NumberLong(1),10 "heartbeatIntervalMillis" : NumberLong(2000),11 "members" : [12 {13 "_id" : 0,14 "name" : "192.168.126.9:27017",15 "health" : 1,16 "state" : 1,17 "stateStr" : "PRIMARY",18 "uptime" : 3524,19 "optime" : {20 "ts" : Timestamp(1466697870, 1),21 "t" : NumberLong(1)22 },23 "optimeDate" : ISODate("2016-06-23T16:04:30Z"),24 "infoMessage" : "could not find member to sync from",25 "electionTime" : Timestamp(1466697868, 2),26 "electionDate" : ISODate("2016-06-23T16:04:28Z"),27 "configVersion" : 1,28 "self" : true29 }30 ],31 "ok" : 1
6.啟動 Secondry Mongo節點服務
root@linux-mongo2:/home/Ubuntu# /usr/local/mongodb/bin/mongod -dbpath=/sdb1/mongodb/data --fork --port 27017 --logpath=/sdb1/mongodb/log/mongo.log --logappend --replSet repl1
about to fork child process, waiting until server is ready for connections. forked process: 2848 child process started successfully, parent exiting
7.在Primary節點將 Secondry Mongo節點添加到複製集裡
repl1:PRIMARY> rs.add("192.168.126.8:27017"){ "ok" : 1 }
8.啟動 ARBITER Mongo仲裁節點
root@linux-mongo2:/home/ubuntu# mkdir -p /sdb1/mongodb/arbroot@linux-mongo2:/home/ubuntu# /usr/local/mongodb/bin/mongod -dbpath=/sdb1/mongodb/arb --fork --port 27018 --logpath=/sdb1/mongodb/arb/mongo.log --logappend --relpSet replSet repl1Error parsing command line: unrecognised option '--relpSet'try '/usr/local/mongodb/bin/mongod --help' for more informationroot@linux-mongo2:/home/ubuntu# /usr/local/mongodb/bin/mongod -dbpath=/sdb1/mongodb/arb --fork --port 27018 --logpath=/sdb1/mongodb/arb/mongo.log --logappend -replSet repl1about to fork child process, waiting until server is ready for connections.forked process: 3229child process started successfully, parent exiting
9.在主節點將仲裁節點添加在複製集中,並查看最終的複製集狀態
1 repl1:PRIMARY> rs.addArb("192.168.126.8:27018") 2 { "ok" : 1 } 3 4 repl1:PRIMARY> rs.status() 5 { 6 "set" : "repl1", 7 "date" : ISODate("2016-06-23T16:27:55.358Z"), 8 "myState" : 1, 9 "term" : NumberLong(1),10 "heartbeatIntervalMillis" : NumberLong(2000),11 "members" : [12 {13 "_id" : 0,14 "name" : "192.168.126.9:27017",15 "health" : 1,16 "state" : 1,17 "stateStr" : "PRIMARY",18 "uptime" : 4889,19 "optime" : {20 "ts" : Timestamp(1466699268, 1),21 "t" : NumberLong(1)22 },23 "optimeDate" : ISODate("2016-06-23T16:27:48Z"),24 "electionTime" : Timestamp(1466697868, 2),25 "electionDate" : ISODate("2016-06-23T16:04:28Z"),26 "configVersion" : 3,27 "self" : true28 },29 {30 "_id" : 1,31 "name" : "192.168.126.8:27017",32 "health" : 1,33 "state" : 5,34 "stateStr" : "STARTUP2",35 "uptime" : 1005,36 "optime" : {37 "ts" : Timestamp(0, 0),38 "t" : NumberLong(-1)39 },40 "optimeDate" : ISODate("1970-01-01T00:00:00Z"),41 "lastHeartbeat" : ISODate("2016-06-23T16:27:54.499Z"),42 "lastHeartbeatRecv" : ISODate("2016-06-23T16:27:54.561Z"),43 "pingMs" : NumberLong(0),44 "syncingTo" : "192.168.126.9:27017",45 "configVersion" : 346 },47 {48 "_id" : 2,49 "name" : "192.168.126.8:27018",50 "health" : 1,51 "state" : 7,52 "stateStr" : "ARBITER",53 "uptime" : 6,54 "lastHeartbeat" : ISODate("2016-06-23T16:27:54.499Z"),55 "lastHeartbeatRecv" : ISODate("2016-06-23T16:27:53.632Z"),56 "pingMs" : NumberLong(1),57 "configVersion" : 358 }59 ],60 "ok" : 161 }
注意: 這裡複本集 192.168.126.8 的狀態是 "STARTUP2" ,是由於Primary庫的資料很大,導致一直處於初始化的同步資料階段。
詳細說明如下
"_id" : #叢集中節點編號"name" : #成員伺服器名稱及連接埠"health" : #表示成員中的健康狀態(0:down;1:up)"state" : #為1~11,表示成員的目前狀態"stateStr" : #描述該成員是主庫(PRIMARY)還是備庫(SECONDARY)"uptime" : #該成員線上時間(秒)"optime" : #成員最後一次應用日誌(oplog)的資訊"optimeDate" : #成員最後一次應用日誌(oplog)的時間"electionTime" : #當前primary從動作記錄中選舉資訊"electionDate" : #當前primary被選定為primary的日期"configVersion" : #mongodb版本"self" : #為true 表示當前節點
其中 state 複製集狀態:
複製集狀態:
STARTUP:剛加入到複製集中,配置還未載入
STARTUP2:配置已載入完,初始化狀態
RECOVERING:正在恢複,不適用讀
ARBITER: 仲裁者
DOWN:節點不可到達
UNKNOWN:未擷取其他節點狀態而不知是什麼狀態,一般發生在只有兩個成員的架構,腦裂
REMOVED:移除複製集
ROLLBACK:資料復原,在復原結束時,轉移到RECOVERING或SECONDARY狀態
FATAL:出錯。查看日誌grep "replSet FATAL"找出錯原因,重新做同步
PRIMARY:主節點
SECONDARY:備份節點
更多MongoDB相關教程見以下內容:
CentOS 編譯安裝 MongoDB與mongoDB的php擴充
CentOS 6 使用 yum 安裝MongoDB及伺服器端配置
Ubuntu 13.04下安裝MongoDB2.4.3
MongoDB入門必讀(概念與實戰並重)
Ubunu 14.04下MongoDB的安裝指南
《MongoDB 權威指南》(MongoDB: The Definitive Guide)英文文字版[PDF]
Nagios監控MongoDB分區叢集服務實戰
基於CentOS 6.5作業系統搭建MongoDB服務
MongoDB 的詳細介紹:請點這裡
MongoDB 的:請點這裡
本文永久更新連結地址: