MongoDB副本集搭建
準備三臺伺服器:
10.175.120.131(主節點)
10.175.121.134(副本節點)
10.175.121.136(副本節點)
1.分別在每臺伺服器上安裝MongoDB,並通過配置檔案啟動服務,配置檔案如下:
dbpath=/data/db #指定資料庫路徑 logpath=/data/mongodb.log #指定日誌檔案路徑 logappend=true #在日誌檔案持續新增 port=27017 #埠號 bind_ip=0.0.0.0 #允許遠端訪問 fork=true #後臺執行 replSet=admin #設定副本集名稱
2.進入MongoDB後臺管理 Shell
./momgo
3.使用admin資料庫
> use admin
4. 定義副本集配置變數,這裡的 _id:”admin” 和配置檔案中的“replSet=admin” 要保持一致
> config = { _id:"admin", members:[ {_id:0,host:"10.175.120.131:27017"}, {_id:1,host:"10.175.121.134:27017"}, {_id:2,host:"10.175.121.136:27017"} ] }
5.初始化副本集配置
> rs.initiate(config)
6.檢視叢集節點的狀態
> rs.status()
輸出:
{ "set" : "admin", "date" : ISODate("2018-09-21T08:52:24.064Z"), "myState" : 1, "term" : NumberLong(3), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) }, "readConcernMajorityOpTime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) }, "appliedOpTime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) }, "durableOpTime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) } }, "members" : [ { "_id" : 1, "name" : "10.175.120.131:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 5874, "optime" : { "ts" : Timestamp(1537519943, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2018-09-21T08:52:23Z"), "electionTime" : Timestamp(1537514082, 1), "electionDate" : ISODate("2018-09-21T07:14:42Z"), "configVersion" : 1, "self" : true }, { "_id" : 2, "name" : "10.175.121.134:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 5861, "optime" : { "ts" : Timestamp(1537519933, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1537519933, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2018-09-21T08:52:13Z"), "optimeDurableDate" : ISODate("2018-09-21T08:52:13Z"), "lastHeartbeat" : ISODate("2018-09-21T08:52:23.012Z"), "lastHeartbeatRecv" : ISODate("2018-09-21T08:52:23.584Z"), "pingMs" : NumberLong(0), "syncingTo" : "10.175.120.131:27017", "configVersion" : 1 }, { "_id" : 3, "name" : "10.175.121.136:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 5866, "optime" : { "ts" : Timestamp(1537519933, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1537519933, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2018-09-21T08:52:13Z"), "optimeDurableDate" : ISODate("2018-09-21T08:52:13Z"), "lastHeartbeat" : ISODate("2018-09-21T08:52:23.013Z"), "lastHeartbeatRecv" : ISODate("2018-09-21T08:52:23.558Z"), "pingMs" : NumberLong(0), "syncingTo" : "10.175.120.131:27017", "configVersion" : 1 } ], "ok" : 1, "operationTime" : Timestamp(1537519943, 1), "$clusterTime" : { "clusterTime" : Timestamp(1537519943, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
輸出部分引數說明:
"health" : 1, #代表機器正常 "stateStr" : "PRIMARY", #代表是主節點,可讀寫,其中有以下幾下狀態 1. STARTUP:剛加入到複製集中,配置還未載入 2. STARTUP2:配置已載入完,初始化狀態 3. RECOVERING:正在恢復,不適用讀 4. ARBITER: 仲裁者 5. DOWN:節點不可到達 6. UNKNOWN:未獲取其他節點狀態而不知是什麼狀態,一般發生在只有兩個成員的架構,腦裂 7. REMOVED:移除複製集 8. ROLLBACK:資料回滾,在回滾結束時,轉移到RECOVERING或SECONDARY狀態 9. FATAL:出錯。檢視日誌grep “replSet FATAL”找出錯原因,重新做同步 10. PRIMARY:主節點 11. SECONDARY:備份節點