mongodb的多服務(wù)器配置,以前寫過一篇文章,是master-slave模式的,請(qǐng)參考:詳解mongodb 主從配置。master-slave模式,不能自動(dòng)實(shí)現(xiàn)故障轉(zhuǎn)移和恢復(fù)。所以推薦大家使用mongodb的replica set,來實(shí)現(xiàn)多服務(wù)器的高可用。給我的感覺是replica set好像自帶了heartbeat功能,挺強(qiáng)大的。
一,三臺(tái)服務(wù)器,1主,2從
服務(wù)器1:127.0.0.1:27017
服務(wù)器2:127.0.0.1:27018
服務(wù)器3:127.0.0.1:27019
1,創(chuàng)建數(shù)據(jù)庫(kù)目錄
[root@localhost ~]# mkdir /var/lib/{mongodb_2,mongodb_3}
在一臺(tái)機(jī)子上面模擬,三臺(tái)服務(wù)器,所以把DB目錄分開了。
2,創(chuàng)建配置文件
[root@localhost ~]# cat /etc/mongodb.conf |awk '{if($0 !~ /^$/ $0 !~ /^#/) {print $0}}' //主服務(wù)器配置
port = 27017 //監(jiān)聽端口
fork = true //后臺(tái)運(yùn)行
pidfilepath = /var/run/mongodb/mongodb.pid //進(jìn)程PID文件
logpath = /var/log/mongodb/mongodb.log //日志文件
dbpath =/var/lib/mongodb //db存放目錄
journal = true //存儲(chǔ)模式
nohttpinterface = true //禁用http
directoryperdb=true //一個(gè)數(shù)據(jù)庫(kù)一個(gè)文件夾
logappend=true //追加方式寫日志
replSet=repmore //集群名稱,自定義
oplogSize=1000 //oplog大小
[root@localhost ~]# cat /etc/mongodb_2.conf |awk '{if($0 !~ /^$/ $0 !~ /^#/) {print $0}}' //從服務(wù)器
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}}' //從服務(wù)器
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
在這里要注意一點(diǎn),不要把認(rèn)證開起來了,不然查看rs.status();時(shí),主從服務(wù)器間,無法連接,"lastHeartbeatMessage" : "initial sync couldn't connect to 127.0.0.1:27017"
3,啟動(dòng)三臺(tái)服務(wù)器
mongod -f /etc/mongodb.conf
mongod -f /etc/mongodb_2.conf
mongod -f /etc/mongodb_3.conf
注意:初次啟動(dòng)時(shí),主服務(wù)器比較快的,從服務(wù)器有點(diǎn)慢。
二,配置并初始化replica set
1,配置replica set節(jié)點(diǎn)
> 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各節(jié)點(diǎn)狀態(tài)
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初始化也是要一定時(shí)間的,剛執(zhí)行完rs.initiate,我就查看狀態(tài),從服務(wù)器的stateStr不是SECONDARY,而是stateStr" : "STARTUP2",等一會(huì)就好了。
三,replica set主,從測(cè)試
1,主服務(wù)器測(cè)試
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,從服務(wù)器測(cè)試
[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(); //無權(quán)限查看
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
repmore:SECONDARY> rs.slaveOk(); //從庫(kù)開啟
repmore:SECONDARY> db.test.find(); //從庫(kù)可看到主庫(kù)剛插入的數(shù)據(jù)
{ "_id" : ObjectId("52af64549d2f9e75bc57cda7"), "name" : "tank", "phone" : "12345678" }
repmore:SECONDARY> db.test.insert({'name':'zhangying','phone':'12345678'}); //從庫(kù)只讀,無插入權(quán)限
not master
到這兒,我們的replica set就配置好了。
四,故障測(cè)試
前面我說過,mongodb replica set有故障轉(zhuǎn)移功能,下面就模擬一下,這個(gè)過程
1,故障轉(zhuǎn)移
1.1,關(guān)閉主服務(wù)器
[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 //關(guān)閉主服務(wù)器進(jìn)程
[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,在主庫(kù)執(zhí)行命令
repmore:PRIMARY> show dbs;
Tue Dec 17 04:48:02.392 DBClientCursor::init call() failed
1.3,從庫(kù)查看狀態(tài),如下圖,
replica set 故障測(cè)試
以前的從庫(kù)變主庫(kù)了,故障轉(zhuǎn)移成功
2,故障恢復(fù)
mongod -f /etc/mongodb.conf
啟動(dòng)剛被關(guān)閉的主服務(wù)器,然后在登錄到主服務(wù)器,查看狀態(tài)rs.status();已恢復(fù)到最原始的狀態(tài)了。
您可能感興趣的文章:- MongoDB添加仲裁節(jié)點(diǎn)報(bào)錯(cuò):replica set IDs do not match的解決方法
- mongodb replica set 添加刪除節(jié)點(diǎn)的2種方法
- 詳解mongodb搭建Replica Set的方法