环境:
192.168.99.129 master
192.168.99.130 slave1
1、配置复制集
在两台服务器上分别执行
mongod --replSet "rs0"
执行mongo命令,先添加一台
rs.initiate( { _id : "rs0", members: [ { _id : 0, host : "master:27017" } ] })
再添加另外一台
rs.add("slave1")
执行rs.status()查看复制集节点的状态
rs0:PRIMARY> rs.status() { "set" : "rs0", "date" : ISODate("2017-01-01T05:08:29.623Z"), "myState" : 1, "term" : NumberLong(3), "heartbeatIntervalMillis" : NumberLong(2000), "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1483247303, 1), "t" : NumberLong(3) }, "appliedOpTime" : { "ts" : Timestamp(1483247303, 1), "t" : NumberLong(3) }, "durableOpTime" : { "ts" : Timestamp(1483247303, 1), "t" : NumberLong(3) } }, "members" : [ { "_id" : 0, "name" : "master:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 38, "optime" : { "ts" : Timestamp(1483247303, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2017-01-01T05:08:23Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1483247282, 1), "electionDate" : ISODate("2017-01-01T05:08:02Z"), "configVersion" : 2, "self" : true }, { "_id" : 1, "name" : "slave1:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 33, "optime" : { "ts" : Timestamp(1483247303, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1483247303, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2017-01-01T05:08:23Z"), "optimeDurableDate" : ISODate("2017-01-01T05:08:23Z"), "lastHeartbeat" : ISODate("2017-01-01T05:08:29.007Z"), "lastHeartbeatRecv" : ISODate("2017-01-01T05:08:27.902Z"), "pingMs" : NumberLong(0), "syncingTo" : "master:27017", "configVersion" : 2 } ], "ok" : 1 }
2、验证复制集数据同步
在主节点插入数据后,看从节点是否存在同样的数据,但在从节点执行查询数据库指令时抛错
rs0:SECONDARY> show dbs 2016-12-31T20:49:59.362-0800 E QUERY [main] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk" } :
因为从节点默认是不允许读写的,解决这个问题,有两种方式
- 在从节点上执行rs.slaveOk()
- 或者在主节点上执行db.getMongo().setSlaveOk()
下面开始验证
首先在主节点上插入数据
rs0:PRIMARY> db.student.insert({"name":"xiaoming"}) WriteResult({ "nInserted" : 1 }) rs0:PRIMARY> db.student.find() { "_id" : ObjectId("58688fa863a36c070316cd69"), "name" : "xiaoming" }
然后在从节点上可以查询到这条数据
rs0:SECONDARY> db.student.find() { "_id" : ObjectId("58688fa863a36c070316cd69"), "name" : "xiaoming" }
3、验证复制集故障迁移
mongodb的复制集至少有3个节点才会自动故障迁移,当只有两个节点时,主节点挂掉,从节点的日志显示
Not starting an election, since we are not electable due to: Not standing for election because I cannot see a majority (mask 0x1)
所以要再添加一个节点,才能验证,执行如下命令启动第三个节点
mongod --replSet "rs0" --port 27018 --dbpath /data/db2 &
在主节点上执行如下命令,将第三个节点添加到集群中
rs.add("slave1:27018")
此时将主节点的进程杀掉,在其他从节点上执行rs.status()查看结果如下:
"members" : [ { "_id" : 0, "name" : "master:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDurable" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2017-01-01T14:41:23.260Z"), "lastHeartbeatRecv" : ISODate("2017-01-01T14:41:03.671Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "Connection refused", "configVersion" : -1 }, { "_id" : 1, "name" : "slave1:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 317, "optime" : { "ts" : Timestamp(1483281677, 2), "t" : NumberLong(8) }, "optimeDate" : ISODate("2017-01-01T14:41:17Z"), "infoMessage" : "could not find member to sync from", "electionTime" : Timestamp(1483281677, 1), "electionDate" : ISODate("2017-01-01T14:41:17Z"), "configVersion" : 3, "self" : true }, { "_id" : 2, "name" : "slave1:27018", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 75, "optime" : { "ts" : Timestamp(1483281677, 2), "t" : NumberLong(8) }, "optimeDurable" : { "ts" : Timestamp(1483281677, 2), "t" : NumberLong(8) }, "optimeDate" : ISODate("2017-01-01T14:41:17Z"), "optimeDurableDate" : ISODate("2017-01-01T14:41:17Z"), "lastHeartbeat" : ISODate("2017-01-01T14:41:23.252Z"), "lastHeartbeatRecv" : ISODate("2017-01-01T14:41:22.253Z"), "pingMs" : NumberLong(0), "syncingTo" : "slave1:27017", "configVersion" : 3 } ], "ok" : 1 }