基于MongoDB2.6版本配置MongoDB主从复制集群架构
1:集群环境说明:mongodb1:192.168.43.10、mongodb2:192.168.43.11、mongodb3:192.168.43.12。且基于主机名相互通信
2:编辑mongodb1的
replSet=uplooking
replIndexPrefetch=_id_only
noauth=true
3:将mongodb1的配置文件拷贝到mongodb2、mongodb3
scp
scp
3:启动mongodb1、mongodb2、mongodb3
4:登入mongodb1的shell接口
> rs.status(); (查看集群中的节点状态)
> rs.initiate();(初始化副本集)
> rs.status();(再次查看集群中的节点状态)
uplooking:PRIMARY> (此时的提示符已经变了)
5:当初始化了主节点以后,需要添加其他的从节点到mongodb集群中来
uplooking:PRIMARY> rs.add("192.168.43.11")
uplooking:PRIMARY> rs.add("192.168.43.12")
6:添加了其他节点以后,就可以查看配置信息和副本集的状态信息
uplooking:PRIMARY> rs.conf()
{
"_id" : "uplooking",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "mongodb1:27017"
},
{
"_id" : 1,
"host" : "192.168.43.11:27017"
},
{
"_id" : 2,
"host" : "192.168.43.12:27017"
}
]
}
7:再次查看状态副本集的状态信息
uplooking:PRIMARY> rs.status()
{
"set" : "uplooking",
"date" : ISODate("2017-10-29T16:05:55Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "mongodb1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 914,
"optime" : Timestamp(1509292923, 1),
"optimeDate" : ISODate("2017-10-29T16:02:03Z"),
"electionTime" : Timestamp(1509292619, 1),
"electionDate" : ISODate("2017-10-29T15:56:59Z"),
"self" : true
},
{
"_id" : 1,
"name" : "192.168.43.11:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 237,
"optime" : Timestamp(1509292923, 1),
"optimeDate" : ISODate("2017-10-29T16:02:03Z"),
"lastHeartbeat" : ISODate("2017-10-29T16:05:54Z"),
"lastHeartbeatRecv" : ISODate("2017-10-29T16:05:54Z"),
"pingMs" : 0,
"syncingTo" : "mongodb1:27017"
},
{
"_id" : 2,
"name" : "192.168.43.12:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 232,
"optime" : Timestamp(1509292923, 1),
"optimeDate" : ISODate("2017-10-29T16:02:03Z"),
"lastHeartbeat" : ISODate("2017-10-29T16:05:55Z"),
"lastHeartbeatRecv" : ISODate("2017-10-29T16:05:55Z"),
"pingMs" : 0,
"syncingTo" : "mongodb1:27017"
}
],
"ok" : 1
}
8:此时从节点可以在shell接口查看到在主节点复制过来的数据库和表,但是不能查询表的内容,因此需要通过命令让从节点变为可用节点,在查询就可以了
uplooking:SECONDARY> rs.slaveOk()
9:主节点也可以通过命令变为从节点, 此时其他的节点通过投票将会变为主节点
uplooking:SECONDARY> rs.stepDown()
10:在新的主节点(mongodb2)中可以设置从节点的优先级让从节点变为主节点,让第一个从节点优先级为10,那么第一个从节点就会变为主节点
uplooking:PRIMARY> config=rs.conf()
uplooking:PRIMARY> config.members[0].priority=10
uplooking:PRIMARY> rs.reconfig(config)
11:在主节点中,从副本集中移除一个节点,
rs.conf()
rs.remove("192.168.43.12:27017")
12:也可以将一个从节点变为arbiter仲裁节点,这里将mongodb3变为arbiter节点,必须先让mongodb3下线,然后删除数据目录,在重启,再设置为arbiter仲裁节点
rs.addArb("192.168.43.12")