mongodb中的副本集搭建实践(转自:http://www.cnblogs.com/visionwang/p/3290435.html)
準(zhǔn)備運(yùn)行1個(gè)主節(jié)點(diǎn),2個(gè)從節(jié)點(diǎn),從節(jié)點(diǎn)中其中是一個(gè)是仲裁節(jié)點(diǎn)(Arb)。
sudo mongod --replSet application --dbpath /data/node1/ --port 9927 --oplogSize 1024 sudo mongod --replSet application --dbpath /data/node2/ --port 9928 --oplogSize 1024 sudo mongod --replSet application --dbpath /data/arbiter/ --port 9929 --oplogSize 1024其中application是副本集的名稱(chēng),節(jié)點(diǎn)必須相同,–dbpath指定數(shù)據(jù)庫(kù)儲(chǔ)存路徑,–port指定偵聽(tīng)端口,–oplogSize指定數(shù)據(jù)同步之前的緩存的空間大小,暫時(shí)指定1G。選擇9927端口的實(shí)例為主節(jié)點(diǎn),進(jìn)入9927的shell:
mongo localhost:9927初始化副本集需要配置表,申明配置表如下:
config = {_id: "application", members: []}注意_id和副本集啟動(dòng)的共享名稱(chēng)一致。下面來(lái)逐步添加節(jié)點(diǎn)的數(shù)據(jù)信息:
config.members.push({_id: 0, host: "localhost:9927"}) config.members.push({_id: 1, host: "localhost:9928"}) config.members.push({_id: 2, host: "localhost:9929", arbiterOnly: true})也可以使用rs.add和rs.addArb函數(shù)來(lái)實(shí)現(xiàn)同樣的操作。然后需要用這個(gè)表作為參數(shù)初始化副本集,在9927端口的shell執(zhí)行:
> rs.initiate(config) {"info" : "Config now saved locally. Should come online in about a minute.","ok" : 1 }返回ok為1表示初始化成功,三個(gè)節(jié)點(diǎn)互相檢測(cè)通信,需要1分鐘左右(反應(yīng)夠慢的 ^-),可以查看三個(gè)終端窗口的信息確認(rèn),完成通信后,在9927端口的shell回車(chē)執(zhí)行命令確認(rèn)配置:
> rs.isMaster() {"setName" : "application","ismaster" : true,"secondary" : false,"hosts" : ["localhost:9927","localhost:9928"],"arbiters" : ["localhost:9929"],"primary" : "localhost:9927","me" : "localhost:9927","maxBsonObjectSize" : 16777216,"localTime" : ISODate("2013-01-26T13:34:03.378Z"),"ok" : 1 }注意到9927端口的實(shí)例ismaster是true,secondary為false,hosts有2個(gè)實(shí)例,arbiter有1個(gè)元素,primary關(guān)鍵key表示了主節(jié)點(diǎn),通信完成幾次回車(chē)后可以看到9927的端口的實(shí)例shell的提示符已經(jīng)改變,更改為application:PRIMARY,查看更詳細(xì)的信息:
application:PRIMARY> rs.status() {"set" : "application","date" : ISODate("2013-01-26T13:34:35Z"),"myState" : 1,"members" : [{"_id" : 0,"name" : "localhost:9927","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 140,"optime" : Timestamp(1359207200000, 1),"optimeDate" : ISODate("2013-01-26T13:33:20Z"),"self" : true},{"_id" : 1,"name" : "localhost:9928","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 63,"optime" : Timestamp(1359207200000, 1),"optimeDate" : ISODate("2013-01-26T13:33:20Z"),"lastHeartbeat" : ISODate("2013-01-26T13:34:34Z"),"pingMs" : 0},{"_id" : 2,"name" : "localhost:9929","health" : 1,"state" : 7,"stateStr" : "ARBITER","uptime" : 63,"lastHeartbeat" : ISODate("2013-01-26T13:34:34Z"),"pingMs" : 0}],"ok" : 1 }顯示了每個(gè)節(jié)點(diǎn)的健康狀況,名稱(chēng),啟動(dòng)的時(shí)間,節(jié)點(diǎn)的類(lèi)型等。查看當(dāng)前副本集的配置表:
application:PRIMARY> rs.conf() {"_id" : "application","version" : 1,"members" : [{"_id" : 0,"host" : "localhost:9927"},{"_id" : 1,"host" : "localhost:9928"},{"_id" : 2,"host" : "localhost:9929","arbiterOnly" : true}] }插入測(cè)試數(shù)據(jù):
application:PRIMARY> db.users.insert({username: "visionwang", age: 26})進(jìn)入9928從節(jié)點(diǎn),執(zhí)行查看集合:
application:SECONDARY> show collections application:SECONDARY> show collections Sat Jan 26 21:39:40 uncaught exception: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }發(fā)現(xiàn)shell拋出了異常,顯示slaveOK為false,當(dāng)前副本集需要明確從節(jié)點(diǎn)參數(shù),執(zhí)行函數(shù):
application:SECONDARY> rs.slaveOk() application:SECONDARY> show collections system.indexes users查詢(xún)測(cè)試數(shù)據(jù):
application:SECONDARY> db.users.find() { "_id" : ObjectId("5103dbc8f556a05a96a28e69"), "username" : "visionwang", "age" : 26 }插入數(shù)據(jù)沒(méi)有拋出異常,但是顯示not master,表示當(dāng)前從節(jié)點(diǎn)是只讀的:
application:SECONDARY> db.users.insert({username: "nl", age: 25}) not master切換到9927主節(jié)點(diǎn),使用系統(tǒng)空間庫(kù)查詢(xún)副本集信息:
application:PRIMARY> db.system.replset.findOne() {"_id" : "application","version" : 1,"members" : [{"_id" : 0,"host" : "localhost:9927"},{"_id" : 1,"host" : "localhost:9928"},{"_id" : 2,"host" : "localhost:9929","arbiterOnly" : true}] } application:PRIMARY> db.getReplicationInfo() {"logSizeMB" : 1024,"usedMB" : 0.01,"timeDiff" : 169,"timeDiffHours" : 0.05,"tFirst" : "Sat Jan 26 2013 21:33:20 GMT+0800 (CDT)","tLast" : "Sat Jan 26 2013 21:36:09 GMT+0800 (CDT)","now" : "Sat Jan 26 2013 21:48:07 GMT+0800 (CDT)" }進(jìn)入9929端口,可以看到仲裁節(jié)點(diǎn)的提示符號(hào):
mongo localhost:9929 application:ARBITER>執(zhí)行故障轉(zhuǎn)移測(cè)試,可以ctrl+c掉9927的主節(jié)點(diǎn),觀察終端信息提示,9927處于down狀態(tài),9928從節(jié)點(diǎn)自舉為主節(jié)點(diǎn),回車(chē)后發(fā)現(xiàn)shell提示符號(hào)已經(jīng)更改為application:PRIMARY,再將9927上線(xiàn),添加為從節(jié)點(diǎn),再ctrl+c掉9928節(jié)點(diǎn),經(jīng)過(guò)少許時(shí)間,9927端口又恢復(fù)成主節(jié)點(diǎn),再將9928上線(xiàn),系統(tǒng)恢復(fù)到初始的副本集,仲裁節(jié)點(diǎn)的作用是協(xié)調(diào)leader選舉,監(jiān)測(cè)系統(tǒng)運(yùn)行狀態(tài),提供節(jié)點(diǎn)互相通訊的數(shù)據(jù)信息。
mongodb也支持簡(jiǎn)單的主從復(fù)制,但是有了強(qiáng)大的副本集,幾乎找不到什么理由使用簡(jiǎn)單的主從復(fù)制策略,甚至mongodb的分片(auto-sharding)也是使用副本集實(shí)現(xiàn)。
51CTO也有一個(gè)windows上面的版本http://database.51cto.com/art/201301/379033.htm。
總結(jié)
以上是生活随笔為你收集整理的mongodb中的副本集搭建实践(转自:http://www.cnblogs.com/visionwang/p/3290435.html)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 华夏信用卡取现额度是多少
- 下一篇: 银行卡莫名被pos机刷钱