通过oplog增加节点,这种方式使数据的同步完全依赖于oplog,即oplog中有多少操作日志,这些操作日志就完全在新添加的节点中执行一遍,以完成同步。
在上面有一个3节点的复制集基础上,现在想配置并启动一个新节点,将其加入现在复制集环境中。
[root@node1 ~]# rsync -avH --progress '-e ssh -p 22' /etc/hosts root@node2.pancou.com:/etc/
[root@node1 ~]# rsync -avH --progress '-e ssh -p 22' /mongodb/mongokey root@node4.pancou.com:/mongodb/
[root@node1 ~]# rsync -avH --progress '-e ssh -p 22' /etc/mongod.conf root@node4.pancou.com:/etc/
[root@node4 ~]# iptables -I INPUT 4 -m state --state NEW -p tcp --dport 27017 -j ACCEPT
在主上添加新节点:
rs0:PRIMARY> rs.add("node4.pancou.com")
{ "ok" : 1 }
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2017-05-19T12:12:57.697Z"),
"myState" : 1,
"term" : NumberLong(8),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
},
"appliedOpTime" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
},
"durableOpTime" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
}
},
"members" : [
{
"_id" : 0,
"name" : "node1.pancou.com:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 159,
"optime" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
},
"optimeDurable" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2017-05-19T12:12:51Z"),
"optimeDurableDate" : ISODate("2017-05-19T12:12:51Z"),
"lastHeartbeat" : ISODate("2017-05-19T12:12:56.111Z"),
"lastHeartbeatRecv" : ISODate("2017-05-19T12:12:57.101Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "node3.pancou.com:27017",
"configVersion" : 4
},
{
"_id" : 1,
"name" : "node2.pancou.com:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 189,
"optime" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
},
"optimeDurable" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2017-05-19T12:12:51Z"),
"optimeDurableDate" : ISODate("2017-05-19T12:12:51Z"),
"lastHeartbeat" : ISODate("2017-05-19T12:12:56.111Z"),
"lastHeartbeatRecv" : ISODate("2017-05-19T12:12:57.103Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "node3.pancou.com:27017",
"configVersion" : 4
},
{
"_id" : 2,
"name" : "node3.pancou.com:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 191,
"optime" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2017-05-19T12:12:51Z"),
"electionTime" : Timestamp(1495195800, 1),
"electionDate" : ISODate("2017-05-19T12:10:00Z"),
"configVersion" : 4,
"self" : true
},
{
"_id" : 3,
"name" : "node4.pancou.com:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 71,
"optime" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
},
"optimeDurable" : {
"ts" : Timestamp(1495195971, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2017-05-19T12:12:51Z"),
"optimeDurableDate" : ISODate("2017-05-19T12:12:51Z"),
"lastHeartbeat" : ISODate("2017-05-19T12:12:56.122Z"),
"lastHeartbeatRecv" : ISODate("2017-05-19T12:12:56.821Z"),
"pingMs" : NumberLong(1),
"syncingTo" : "node3.pancou.com:27017",
"configVersion" : 4
}
],
"ok" : 1
}
查看状态: