[mongo@ms3 bin]$ ./mongo -port 27011 rs.remove("192.168.0.19:27013"); rs.add({_id: 2, host:'192.168.0.21:27012',"slaveDelay":36000,"priority":0,"hidden":true,"buildIndexes":true});
先建立相應的文件夾 ./mongod --dbpath ../data1 --logpath ../log1/log1.log --port 20001 --fork --replSet myrepl ./mongod --dbpath ../data2 --logpath ../log2/log2.log --port 20002 --fork --replSet myrepl ./mongod --dbpath ../data3 --logpath ../log3/log3.log --port 20003 --fork --replSet myrepl rs.initiate({_id:"myrepl",members:[{_id:0,host:'127.0.0.1:20001'},{_id:1,host:'127.0.0.1:20002'},{_id:2,host:'127.0.0.1:20003'}]}) 1:建立副本集,這個前面學過,就是啓動全部的成員服務器後,使用rs.initiate命令 2:修改副本集成員,前面也學過一些,好比rs.add(),rs.remove()命令,還能夠使用 rs.reconfig(config)命令,好比修改第一個成員的host名稱,示例以下: var config = rs.config(); config.members[0].host=「newHost:port」; rs.reconfig(config); ----------------------------------------------------------------------------------- ./mongod --configsvr --dbpath ../confdb/confdb1 --logpath ../conflog/conflog1 --fork --port 30001 ./mongod --configsvr --dbpath ../confdb/confdb2 --logpath ../conflog/conflog2 --fork --port 30002 ./mongod --configsvr --dbpath ../confdb/confdb3 --logpath ../conflog/conflog3 --fork --port 30003 ./mongos --configdb 127.0.0.1:30001,127.0.0.1:30002,127.0.0.1:30003 --logpath ../conflog/mongoslog --fork sh.addShard("myrep2/127.0.0.1:20004"); for(var i=0;i++;i<10000){db.tuser.insert({"uname":"liuwe"+i});}; ./mongod --dbpath ../data4 --logpath ../log4 --port 20004 --fork --replSet myrep2 ./mongod --dbpath ../data5 --logpath ../log5 --port 20005 --fork --replSet myrep2 ./mongod --dbpath ../data6 --logpath ../log6 --port 20006 --fork --replSet myrep2 rs.initiate({_id:"myrep2",members:[{_id:0,host:'127.0.0.1:20004'},{_id:1,host:'127.0.0.1:20005'},{_id:2,host:'127.0.0.1:20006'}]}) ------------------------------------------------------------------------------------------------- > db.users.explain("allPlansExecution").find({username:'user101'}) { "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.users", "indexFilterSet" : false, "parsedQuery" : { "username" : { "$eq" : "user101" } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "username" : 1 }, "indexName" : "username_1", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "username" : [ "[\"user101\", \"user101\"]" ] } } }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 0, "totalKeysExamined" : 1, "totalDocsExamined" : 1, "executionStages" : { "stage" : "FETCH", "nReturned" : 1, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 1, "needTime" : 0, "needFetch" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "docsExamined" : 1, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 1, "executionTimeMillisEstimate" : 0, "works" : 2, "advanced" : 1, "needTime" : 0, "needFetch" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "keyPattern" : { "username" : 1 }, "indexName" : "username_1", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "username" : [ "[\"user101\", \"user101\"]" ] }, "keysExamined" : 1, "dupsTested" : 0, "dupsDropped" : 0, "seenInvalidated" : 0, "matchTested" : 0 } }, "allPlansExecution" : [ ] }, "serverInfo" : { "host" : "mongo2", "port" : 27017, "version" : "3.0.0", "gitVersion" : "a841fd6394365954886924a35076691b4d149168" }, "ok" : 1 }
建立比較大的副本集 副本集最多隻能擁有12個成員,只有7個成員擁有投票權。所以要建立超過7個副本 集的話,須要將其餘成員的投票權設置爲0,例如: rs.add({「_id」:8,」host」:」localhost:20008」,」votes」:0}); 若是要配置超過12個成員的話,須要使用Master/Slave的方式,不過這個方式已經
不建議使用了,若是未來副本集能支持更多成員的話,這個方式可能會當即廢除。
強制從新配置
若是副本集沒法達到「大多數」要求的話,可能會沒法選出主節點,這個時候,可
以shell鏈接任意一個成員,而後使用force選項強制從新配置,示例以下:git
rs.reconfig(config,{「force」:true});
---------------------------------------常見問題------------------------------------------------------------spring
rs.initiate({_id:"myrepl",members:[{_id:0,host:'10.1.10.76:20001'},{_id:1,host:'10.1.10.76:20002'},{_id:2,host:'10.1.10.76:20003'}]})這個地方最好寫IP地址不要寫127.0.0.1mongodb
不然spring-data-mongodb 鏈接不上shell