准備工作
環境: CentOS6.5 64位
MongoDB 3.0.1 64位
一、啟動mongod服務
[root@localhost data]# /usr/local/mongodb3.0.1/bin/mongod --port 40144 --dbpath=/data/mongodb3.0.1/data/ --logpath=/data/mongodb3.0.1/log/mongodb.log --directoryperdb --maxConns 100 --logappend --replSet repl/192.168.66.89:50144 --fork
參數:
--oplogSize 日志操作文件的大(默認磁盤剩余空間的5%)
--dbpath 數據文件路徑
--logpath 日志文件路徑
--port 端口號,默認是27017
--replSet 復制集的名字,一個replica sets中的每個節點的這個參數都要用一個復制集名字,這裡是test.
--replSet test/ 這個後面跟的是其他standard節點的ip和端口
--maxConns 最大連接數
--fork 後台運行
--logappend 日志文件循環使用,如果日志文件已滿,那麼新日志覆蓋最久日志。
任選一個節點登錄mongodb數據庫
[root@localhost data]# /usr/local/mongodb3.0.1/bin/mongo --port 40144repl:PRIMARY> use mtdamai; switched to db mtdamai repl:PRIMARY> db.user.insert({name:"wangxiuli"}); WriteResult({ "nInserted" : 1 }) repl:PRIMARY> db.user.find(); { "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" } #備節點查詢發現已有數據 repl:SECONDARY> use mtdamai; switched to db mtdamai repl:SECONDARY> db.user.find(); { "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" }
repl:SECONDARY> rs.status(); { "set" : "repl", "date" : ISODate("2015-05-08T02:51:25.273Z"), "myState" : 2, "members" : [ { "_id" : 1, "name" : "192.168.66.89:40144", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2015-05-08T02:51:23.984Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T02:50:55.975Z"), "pingMs" : 0, "lastHeartbeatMessage" : "Failed attempt to connect to 192.168.66.89:40144; couldn't connect to server 192.168.66.89:40144 (192.168.66.89), connection attempt failed", "configVersion" : -1 }, { "_id" : 2, "name" : "192.168.66.89:50144", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 5563, "optime" : Timestamp(1431053169, 2), "optimeDate" : ISODate("2015-05-08T02:46:09Z"), "lastHeartbeat" : ISODate("2015-05-08T02:51:24.227Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T02:51:24.120Z"), "pingMs" : 0, "electionTime" : Timestamp(1431053458, 1), "electionDate" : ISODate("2015-05-08T02:50:58Z"), "configVersion" : 1 }, { "_id" : 3, "name" : "192.168.66.89:60144", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 5733, "optime" : Timestamp(1431053169, 2), "optimeDate" : ISODate("2015-05-08T02:46:09Z"), "configVersion" : 1, "self" : true } ], "ok" : 1 } #發現主節點已經重新被選出 再來插入數據驗證 repl:PRIMARY> db.user.insert({name:"chenglong"}); WriteResult({ "nInserted" : 1 }) repl:PRIMARY> db.user.find(); { "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" } { "_id" : ObjectId("554c25202dcdffb154643e56"), "name" : "chenglong" } #驗證備節點不可寫 repl:SECONDARY> db.user.insert({name:"liudehua"}); WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } }) #查詢數據已存在,說明數據已同步到其他節點 repl:SECONDARY> use mtdamai; switched to db mtdamai repl:SECONDARY> db.user.find(); Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 } repl:SECONDARY> rs.slaveOk(); repl:SECONDARY> db.user.find(); { "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" } { "_id" : ObjectId("554c25202dcdffb154643e56"), "name" : "chenglong" } #再次啟動被關閉的節點,該節點變為secondary repl:SECONDARY> rs.status(); { "set" : "repl", "date" : ISODate("2015-05-08T07:54:20.607Z"), "myState" : 2, "syncingTo" : "192.168.66.89:50144", "members" : [ { "_id" : 1, "name" : "192.168.66.89:40144", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 19, "optime" : Timestamp(1431053600, 1), "optimeDate" : ISODate("2015-05-08T02:53:20Z"), "lastHeartbeat" : ISODate("2015-05-08T07:54:19.079Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T07:54:20.557Z"), "pingMs" : 0, "syncingTo" : "192.168.66.89:60144", "configVersion" : 1 }, { "_id" : 2, "name" : "192.168.66.89:50144", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 23738, "optime" : Timestamp(1431053600, 1), "optimeDate" : ISODate("2015-05-08T02:53:20Z"), "lastHeartbeat" : ISODate("2015-05-08T07:54:20.152Z"), "lastHeartbeatRecv" : ISODate("2015-05-08T07:54:20.105Z"), "pingMs" : 0, "electionTime" : Timestamp(1431053458, 1), "electionDate" : ISODate("2015-05-08T02:50:58Z"), "configVersion" : 1 }, { "_id" : 3, "name" : "192.168.66.89:60144", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 23908, "optime" : Timestamp(1431053600, 1), "optimeDate" : ISODate("2015-05-08T02:53:20Z"), "syncingTo" : "192.168.66.89:50144", "configVersion" : 1, "self" : true } ], "ok" : 1 } #數據也同步過來了 repl:SECONDARY> use mtdamai; switched to db mtdamai repl:SECONDARY> db.user.find(); { "_id" : ObjectId("554c237123db30ab1a233e4b"), "name" : "wangxiuli" } { "_id" : ObjectId("554c25202dcdffb154643e56"), "name" : "chenglong" }