mongodb集群搭建有三种方式。 1、Master-Slave模式 2、Replica-Set方式 3、Sharding方式 其中,第一种方式基本没什么意义,官方也不推荐这种方式搭建。另外两种分别就是副本集和分片的方式。今天介绍副本集的方式搭建mongodb高可用集群。
副本集的方式也很容易理解,这里需要一个主节点,一个备节点,如果主节点发生故障,那么会启用备节点,当主节点修复之后,主节点再次恢复为主节点,备节点不再是主节点的角色。副本集的方式还需要一个角色,那就是仲裁节点,它不存储数据,他的作用就是当主节点出现故障,选举出备节点作为主节点,继续保证集群可用。客户端连接时只连接主节点或者备节点,不用连接仲裁节点。
修改配置文件 master.conf
dbpath=/data/mongodb/master
logpath=/data/mongodb/master.log
pidfilepath=/data/mongodb/master.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=192.168.61.21
port=27017
oplogSize=10000
fork=true
noprealloc=true
slave.conf
dbpath=/data/mongodb/slave
logpath=/data/mongodb/slave.log
pidfilepath=/data/mongodb/slave.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=192.168.61.22
port=27017
oplogSize=10000
fork=true
noprealloc=true
arbiter.conf
dbpath=/data/mongodb/arbiter
logpath=/data/mongodb/arbiter.log
pidfilepath=/data/mongodb/arbiter.pid
directoryperdb=true
logappend=true
replSet=testrs
bind_ip=192.168.61.23
port=27017
oplogSize=10000
fork=true
noprealloc=true
启动节点,保证每个节点上存储数据的文件夹都存在/data/mongodb/master,/data/mongodb/slave,/data/mongodb/arbiter,然后依次启动每个节点。
mongod -f master.conf mongod -f slave.conf mongod -f arbiter.conf
创建集群
[root@linux-node1 mongodb]# mongo 192.168.61.21:27017
MongoDB shell version: 3.2.9
connecting to: 192.168.61.21:27017/test
Server has startup warnings:
2017-11-16T22:44:06.170+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-11-16T22:44:06.170+0800 I CONTROL [initandlisten]
2017-11-16T22:44:06.171+0800 I CONTROL [initandlisten]
2017-11-16T22:44:06.171+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-11-16T22:44:06.171+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-11-16T22:44:06.171+0800 I CONTROL [initandlisten]
2017-11-16T22:44:06.171+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-11-16T22:44:06.171+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2017-11-16T22:44:06.171+0800 I CONTROL [initandlisten]
> use admin
switched to db admin
> cfg={_id:"testrs",members:[{_id:0,host:"192.168.61.21:27017",priority:2},{_id:1,host:"192.168.61.22:27017",priority:1},{_id:2,host:"192.168.61.23:27017",arbiterOnly:true}]};
{
"_id" : "testrs",
"members" : [
{
"_id" : 0,
"host" : "192.168.61.21:27017",
"priority" : 2
},
{
"_id" : 1,
"host" : "192.168.61.22:27017",
"priority" : 1
},
{
"_id" : 2,
"host" : "192.168.61.23:27017",
"arbiterOnly" : true
}
]
}
> rs.initiate(cfg)
{ "ok" : 1 }
查看集群状态
testrs:OTHER> rs.status()
{
"set" : "testrs",
"date" : ISODate("2017-11-16T14:51:33.788Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "192.168.61.21:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 447,
"optime" : {
"ts" : Timestamp(1510843888, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-11-16T14:51:28Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1510843888, 1),
"electionDate" : ISODate("2017-11-16T14:51:28Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.61.22:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 15,
"optime" : {
"ts" : Timestamp(1510843877, 1),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("2017-11-16T14:51:17Z"),
"lastHeartbeat" : ISODate("2017-11-16T14:51:32.718Z"),
"lastHeartbeatRecv" : ISODate("2017-11-16T14:51:31.944Z"),
"pingMs" : NumberLong(0),
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.61.23:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 15,
"lastHeartbeat" : ISODate("2017-11-16T14:51:32.718Z"),
"lastHeartbeatRecv" : ISODate("2017-11-16T14:51:29.987Z"),
"pingMs" : NumberLong(1),
"configVersion" : 1
}
],
"ok" : 1
}
数据测试:
主节点测试
从节点连接,查询数据库出错:not master and slaveOk=false,这个错误是正常的,备节点不允许读操作。
show dbs
2017-11-16T23:10:05.041+0800 E QUERY [thread1] Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 }
可以利用rs.slaveOk()命令来解决这个问题,但是在集群环境中没有必要这么做。
故障测试:
[root@linux-node1 mongodb]# ps -ef |grep mongod
root 24676 1 1 22:44 ? 00:00:21 mongod -f master.conf
root 24856 11281 0 23:18 pts/0 00:00:00 grep --color=auto mongod
[root@linux-node1 mongodb]# kill -9 24676
主节点挂掉:在备节点上查看集群状态,一会会发现备节点成为主节点:primary。
testrs:SECONDARY> rs.status()
{
"set" : "testrs",
"date" : ISODate("2017-11-16T15:18:56.862Z"),
"myState" : 1,
"term" : NumberLong(2),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "192.168.61.21:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-11-16T15:18:55.651Z"),
"lastHeartbeatRecv" : ISODate("2017-11-16T15:18:31.988Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.168.61.22:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1986,
"optime" : {
"ts" : Timestamp(1510845524, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2017-11-16T15:18:44Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1510845523, 1),
"electionDate" : ISODate("2017-11-16T15:18:43Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.61.23:27017",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1655,
"lastHeartbeat" : ISODate("2017-11-16T15:18:55.632Z"),
"lastHeartbeatRecv" : ISODate("2017-11-16T15:18:51.951Z"),
"pingMs" : NumberLong(1),
"configVersion" : 1
}
],
"ok" : 1
}
这里如果再次将挂掉的master节点启动,那么一会master节点会变为primary主节点,而不是由slave备节点继续充当主节点