



db.products.insert({ writeConcern: { w: 2, wtimeout: 5000 } } #至少写入一个从结点){ w: "majority", wtimeout: 5000 }
use admindb.runCommand( { resync: 1 } )
use localdb.sources.update( { host : "prod.mississippi" },{ $set : { host : "prod.mississippi.example.net" } } )
> rs.initiate();{"info2" : "no configuration specified. Using a default configuration for the set","me" : "node1:27017","ok" : 0,"errmsg" : "No host described in new configuration 1 for replica set repl0 maps to this node","code" : 93}
> rs.conf();2015-12-27T11:04:15.763+0800 E QUERY [thread1] Error: Could not retrieve replica set config: {"info" : "run rs.initiate(...) if not yet done for the set","ok" : 0,"errmsg" : "no replset config has been received","code" : 94} :rs.conf@src/mongo/shell/utils.js:1090:11@(shell):1:1> cfg={"_id":"rs0","version":1,"members":[{"_id":1,"host":"192.168.75.10:27017"}]};{"_id" : "rs0","version" : 1,"members" : [{"_id" : 1,"host" : "192.168.75.10:27017"}]}> rs.initiate(cfg);{ "ok" : 1 }
rs0:PRIMARY> rs.add("192.168.75.11:27017");{ "ok" : 1 }rs0:PRIMARY> rs.add("192.168.75.12:27017");{ "ok" : 1 }
rs0:PRIMARY> rs.status();{"set" : "rs0","date" : ISODate("2015-12-27T03:13:26.970Z"),"myState" : 1,"term" : NumberLong(1),"heartbeatIntervalMillis" : NumberLong(2000),"members" : [{"_id" : 1,"name" : "192.168.75.10:27017","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 575,"optime" : {"ts" : Timestamp(1451185968, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2015-12-27T03:12:48Z"),"electionTime" : Timestamp(1451185613, 2),"electionDate" : ISODate("2015-12-27T03:06:53Z"),"configVersion" : 3,"self" : true},{"_id" : 2,"name" : "192.168.75.11:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 41,"optime" : {"ts" : Timestamp(1451185968, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2015-12-27T03:12:48Z"),"lastHeartbeat" : ISODate("2015-12-27T03:13:24.973Z"),"lastHeartbeatRecv" : ISODate("2015-12-27T03:13:26.011Z"),"pingMs" : NumberLong(1),"syncingTo" : "192.168.75.10:27017","configVersion" : 3},{"_id" : 3,"name" : "192.168.75.12:27017","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 37,"optime" : {"ts" : Timestamp(1451185968, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2015-12-27T03:12:48Z"),"lastHeartbeat" : ISODate("2015-12-27T03:13:25.028Z"),"lastHeartbeatRecv" : ISODate("2015-12-27T03:13:25.128Z"),"pingMs" : NumberLong(1),"configVersion" : 3}],"ok" : 1}
rs0:PRIMARY> use testdbswitched to db testdbrs0:PRIMARY> db.test1231.insert({"name":"test repl"});WriteResult({ "nInserted" : 1 })
rs0:SECONDARY> rs.slaveOk();rs0:SECONDARY> use testdbswitched to db testdbrs0:SECONDARY> show collections;test1231rs0:SECONDARY> db.test1231.find();{ "_id" : ObjectId("567f58f221cb21ff2f187d33"), "name" : "test repl" }
设置优先级,是否隐藏及延时cfg = rs.conf()cfg.members[0].priority = 0cfg.members[0].hidden = truecfg.members[0].slaveDelay = 3600rs.reconfig(cfg)
设置选举权:cfg = rs.conf()cfg.members[3].votes = 0cfg.members[4].votes = 0cfg.members[5].votes = 0rs.reconfig(cfg)
原文:http://www.cnblogs.com/skyrim/p/5098167.html