rs0:PRIMARY> db.printSlaveReplicationInfo()
source: node2.pancou.com:27017
syncedTo: Fri May 19 2017 01:47:15 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: node3.pancou.com:27017
syncedTo: Fri May 19 2017 01:47:15 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
db.system.replset.find():查看复制集
配置信息:
rs0:PRIMARY> db.system.replset.find()
{ "_id" : "rs0", "version" : 3, "protocolVersion" : NumberLong(1), "members" : [ { "_id" : 0, "host" : "node1.pancou.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 1, "host" : "node2.pancou.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 }, { "_id" : 2, "host" : "node3.pancou.com:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : NumberLong(0), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : 2000, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 }, "replicaSetId" : ObjectId("591dd3284fc6957e660dc933") } }
rs0:PRIMARY> db.system.replset.find().forEach(printjson) 这种方式更直观
四、主从切换:
1、把node3冰冻30秒
rs0:SECONDARY> rs.freeze(30)
{ "ok" : 1 }
2、把node1 PRIMARY降级、
rs0:PRIMARY> rs.stepDown(30)
2017-05-19T02:09:27.945+0800 E QUERY [thread1] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:27017' :
DB.prototype.runCommand@src/mongo/shell/db.js:132:1
DB.prototype.adminCommand@src/mongo/shell/db.js:150:16
rs.stepDown@src/mongo/shell/utils.js:1261:12
@(shell):1:1
2017-05-19T02:09:27.947+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2017-05-19T02:09:27.949+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) ok
30秒后就变成从了
rs0:SECONDARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2017-05-18T18:12:09.732Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "node2.pancou.com:27017",
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1495131128, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1495131128, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1495131128, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "node1.pancou.com:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 5519,
"optime" : {
"ts" : Timestamp(1495131128, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-05-18T18:12:08Z"),
"syncingTo" : "node2.pancou.com:27017",
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "node2.pancou.com:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3866,
"optime" : {
"ts" : Timestamp(1495131118, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1495131118, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-05-18T18:11:58Z"),
"optimeDurableDate" : ISODate("2017-05-18T18:11:58Z"),
"lastHeartbeat" : ISODate("2017-05-18T18:12:08.333Z"),
"lastHeartbeatRecv" : ISODate("2017-05-18T18:12:08.196Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1495130977, 1),
"electionDate" : ISODate("2017-05-18T18:09:37Z"),
"configVersion" : 3
},
{
"_id" : 2,
"name" : "node3.pancou.com:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3857,
"optime" : {
"ts" : Timestamp(1495131118, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1495131118, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-05-18T18:11:58Z"),
"optimeDurableDate" : ISODate("2017-05-18T18:11:58Z"),
"lastHeartbeat" : ISODate("2017-05-18T18:12:08.486Z"),
"lastHeartbeatRecv" : ISODate("2017-05-18T18:12:08.116Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "node2.pancou.com:27017",
"configVersion" : 3
}
],
"ok" : 1
}
rs0:SECONDARY>
五、增减节点
1、增加节点