mongodb replica set 配置高性能多服务器详解
mongodb的多服务器配置,以前写过一篇文章,是master-slave模式的,请参考:。master-slave模式,不能自动实现故障转移和恢复。所以推荐大家使用mongodb的replica set,来实现多服务器的高可用。给我的感觉是replica set好像自带了heartbeat功能,挺强大的。
一,三台服务器,1主,2从
服务器1:127.0.0.1:27017
服务器2:127.0.0.1:27018
服务器3:127.0.0.1:27019
1,创建数据库目录
[root@localhost ~]# mkdir /var/lib/{mongodb_2,mongodb_3}
在一台机子上面模拟,三台服务器,所以把db目录分开了。
2,创建配置文件
[root@localhost ~]# cat /etc/mongodb.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}' //主服务器配置 port = 27017 //监听端口 fork = true //后台运行 pidfilepath = /var/run/mongodb/mongodb.pid //进程pid文件 logpath = /var/log/mongodb/mongodb.log //日志文件 dbpath =/var/lib/mongodb //db存放目录 journal = true //存储模式 nohttpinterface = true //禁用http directoryperdb=true //一个数据库一个文件夹 logappend=true //追加方式写日志 replset=repmore //集群名称,自定义 oplogsize=1000 //oplog大小 [root@localhost ~]# cat /etc/mongodb_2.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}' //从服务器 port = 27018 fork = true pidfilepath = /var/run/mongodb/mongodb_2.pid logpath = /var/log/mongodb/mongodb_2.log dbpath =/var/lib/mongodb_2 journal = true nohttpinterface = true directoryperdb=true logappend=true replset=repmore oplogsize=1000 [root@localhost ~]# cat /etc/mongodb_3.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}' //从服务器 port = 27019 fork = true pidfilepath = /var/run/mongodb/mongodb_3.pid logpath = /var/log/mongodb/mongodb_3.log dbpath =/var/lib/mongodb_3 journal = true nohttpinterface = true oplogsize = 1000 directoryperdb=true logappend=true replset=repmore
在这里要注意一点,不要把认证开起来了,不然查看rs.status();时,主从服务器间,无法连接,"lastheartbeatmessage" : "initial sync couldn't connect to 127.0.0.1:27017"
3,启动三台服务器
mongod -f /etc/mongodb.conf mongod -f /etc/mongodb_2.conf mongod -f /etc/mongodb_3.conf
注意:初次启动时,主服务器比较快的,从服务器有点慢。
二,配置并初始化replica set
1,配置replica set节点
> config = {_id:"repmore",members:[{_id:0,host:'127.0.0.1:27017',priority :2},{_id:1,host:'127.0.0.1:27018',priority:1},{_id:2,host:'127.0.0.1:27019',priority:1}]}
2,初始化replica set
> rs.initiate(config); { "info" : "config now saved locally. should come online in about a minute.", "ok" : 1 }
3,查看replica set各节点状态
repmore:primary> rs.status(); { "set" : "repmore", "date" : isodate("2013-12-16t21:01:51z"), "mystate" : 2, "syncingto" : "127.0.0.1:27017", "members" : [ { "_id" : 0, "name" : "127.0.0.1:27017", "health" : 1, "state" : 1, "statestr" : "primary", "uptime" : 33, "optime" : timestamp(1387227638, 1), "optimedate" : isodate("2013-12-16t21:00:38z"), "lastheartbeat" : isodate("2013-12-16t21:01:50z"), "lastheartbeatrecv" : isodate("2013-12-16t21:01:50z"), "pingms" : 0, "syncingto" : "127.0.0.1:27018" }, { "_id" : 1, "name" : "127.0.0.1:27018", "health" : 1, "state" : 2, "statestr" : "secondary", "uptime" : 1808, "optime" : timestamp(1387227638, 1), "optimedate" : isodate("2013-12-16t21:00:38z"), "errmsg" : "syncing to: 127.0.0.1:27017", "self" : true }, { "_id" : 2, "name" : "127.0.0.1:27019", "health" : 1, "state" : 2, "statestr" : "secondary", "uptime" : 1806, "optime" : timestamp(1387227638, 1), "optimedate" : isodate("2013-12-16t21:00:38z"), "lastheartbeat" : isodate("2013-12-16t21:01:50z"), "lastheartbeatrecv" : isodate("2013-12-16t21:01:51z"), "pingms" : 0, "lastheartbeatmessage" : "syncing to: 127.0.0.1:27018", "syncingto" : "127.0.0.1:27018" } ], "ok" : 1 }
在这里要注意,rs.initiate初始化也是要一定时间的,刚执行完rs.initiate,我就查看状态,从服务器的statestr不是secondary,而是statestr" : "startup2",等一会就好了。
三,replica set主,从测试
1,主服务器测试
repmore:primary> show dbs; local 1.078125gb repmore:primary> use test switched to db test repmore:primary> db.test.insert({'name':'tank','phone':'12345678'}); repmore:primary> db.test.find(); { "_id" : objectid("52af64549d2f9e75bc57cda7"), "name" : "tank", "phone" : "12345678" }
2,从服务器测试
[root@localhost mongodb]# mongo 127.0.0.1:27018 //连接 mongodb shell version: 2.4.6 connecting to: 127.0.0.1:27018/test repmore:secondary> show dbs; local 1.078125gb test 0.203125gb repmore:secondary> db.test.find(); //无权限查看 error: { "$err" : "not master and slaveok=false", "code" : 13435 } repmore:secondary> rs.slaveok(); //从库开启 repmore:secondary> db.test.find(); //从库可看到主库刚插入的数据 { "_id" : objectid("52af64549d2f9e75bc57cda7"), "name" : "tank", "phone" : "12345678" } repmore:secondary> db.test.insert({'name':'zhangying','phone':'12345678'}); //从库只读,无插入权限 not master
到这儿,我们的replica set就配置好了。
四,故障测试
前面我说过,mongodb replica set有故障转移功能,下面就模拟一下,这个过程
1,故障转移
1.1,关闭主服务器
[root@localhost mongodb]# ps aux |grep mongod //查看所有的mongod root 16977 0.2 1.1 3153692 44464 ? sl 04:31 0:02 mongod -f /etc/mongodb.conf root 17032 0.2 1.1 3128996 43640 ? sl 04:31 0:02 mongod -f /etc/mongodb_2.conf root 17092 0.2 0.9 3127976 38324 ? sl 04:31 0:02 mongod -f /etc/mongodb_3.conf root 20400 0.0 0.0 103248 860 pts/2 s+ 04:47 0:00 grep mongod [root@localhost mongodb]# kill 16977 //关闭主服务器进程 [root@localhost mongodb]# ps aux |grep mongod root 17032 0.2 1.1 3133124 43836 ? sl 04:31 0:02 mongod -f /etc/mongodb_2.conf root 17092 0.2 0.9 3127976 38404 ? sl 04:31 0:02 mongod -f /etc/mongodb_3.conf root 20488 0.0 0.0 103248 860 pts/2 s+ 04:47 0:00 grep mongod
1.2,在主库执行命令
repmore:primary> show dbs; tue dec 17 04:48:02.392 dbclientcursor::init call() failed
1.3,从库查看状态,如下图,
replica set 故障测试
以前的从库变主库了,故障转移成功
2,故障恢复
mongod -f /etc/mongodb.conf
启动刚被关闭的主服务器,然后在登录到主服务器,查看状态rs.status();已恢复到最原始的状态了。