[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