作者:奋斗的小鸟 | 来源:互联网 | 2014-05-28 16:53
MongoDBreplicaSet配置说明MongoDB在1.6版本对开发了新功能replicaset,这比之前的replication功能要强大一些,autoshard已经明确说明不支持replicationparis,建议使用replicaset,replicaset故障切换完全自动,各个DB之间数据完全一致,大大
MongoDB replica Set配置说明
MongoDB在1.6版本对开发了新功能replica set,这比之前的replication功能要强大一些,auto
shard已经明确说明不支持replication paris,建议使用replica set,replica
set故障切换完全自动,各个DB之间数据完全一致,大大降低了维护成功。
replica set 配置如下:
1、创建目录 /data/db ,data/db2 ,/data/db3,启动mongod并创建复制集 foo
启动mongod
[root@TEST40]
mongod --shardsvr --fork --replSet foo --logpath
/var/log/mongodb.log --logappend --rest
mongod --shardsvr --fork --replSet foo --port 27019 --logpath
/var/log/mongodb2.log --logappend --dbpath /data/db2 --rest
mongod --shardsvr --fork --replSet foo --port 27020 --logpath
/var/log/mongodb3.log --logappend --dbpath /data/db3 --rest
2、初始化复制集配置信息
初始化时给每个host分配一个ID,该ID在复制集中是唯一的,在测试过程中没有发现该ID和故障切换有直接关系。
> cOnfig= {_id: 'foo', members: [
...
{_id: 0, host:
'192.168.100.212:27018'},
...
{_id: 1, host:
'192.168.100.212:27019'},
...
{_id: 2, host:
'192.168.100.212:27020'}]
... }
{
"_id" : "foo",
"members" : [
{
"_id" : 0,
"host" : "192.168.100.212:27018"
},
{
"_id" : 1,
"host" : "192.168.100.212:27019"
},
{
"_id" : 2,
"host" : "192.168.100.212:27020"
}
]
}
> rs.initiate(config);
{
"info" : "Config now saved
locally. Should come online in about a minute.",
"ok" : 1
}
>
已经配置成功
3、查看查看replica set存在的成员
state:1表示该host是当前可以进行读写,2:不能读写
health:1表示该host目前是正常的,0:异常
[root@TEST40 data]# mongo 192.168.100.212:27018
MongoDB shell version: 1.6.0
connecting to: 192.168.100.212:27018/test
> use admin
switched to db admin
> db.runCommand({replSetGetStatus : 1});
{
"set" : "foo",
"date" : "Fri Aug 20 2010 08:35:59
GMT+0000 (UTC)",
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "TEST40:27018",
"health" : 1,
"state" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.100.212:27019",
"health" : 1,
"state" : 2,
"uptime" : 516,
"lastHeartbeat" : "Fri Aug 20 2010
08:35:58 GMT+0000 (UTC)"
},
{
"_id" : 2,
"name" : "192.168.100.212:27020",
"health" : 1,
"state" : 2,
"uptime" : 508,
"lastHeartbeat" : "Fri Aug 20 2010
08:35:58 GMT+0000 (UTC)"
}
],
"ok" : 1
}
>
4、failover试验
登陆其它的slave,slave是不可以进行读写的。
[root@TEST40 data]# mongo 192.168.100.212:27019
MongoDB shell version: 1.6.0
connecting to: 192.168.100.212:27019/test
> use test
switched to db test
> db.people.find();
error: { "$err" : "not master", "code" : 10107 }
>
[root@TEST40 data]# mongo 192.168.100.212:27020
MongoDB shell version: 1.6.0
connecting to: 192.168.100.212:27020/test
> use test
switched to db test
> db.people.find();
error: { "$err" : "not master", "code" : 10107 }
>
查看复制集合成员状态
登陆192.168.100.212:27020
[root@TEST40 data]# mongo 192.168.100.212:27020
MongoDB shell version: 1.6.0
connecting to: 192.168.100.212:27020/test
> use admin
switched to db admin
> db.runCommand({replSetGetStatus : 1});
{
"set" : "foo",
"date" : "Fri Aug 20 2010 08:36:39
GMT+0000 (UTC)",
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "192.168.100.212:27018",
"health" : 1,
"state" : 1,
"uptime" : 546,
"lastHeartbeat" : "Fri Aug 20 2010
08:36:38 GMT+0000 (UTC)"
},
{
"_id" : 1,
"name" : "192.168.100.212:27019",
"health" : 1,
"state" : 2,
"uptime" : 546,
"lastHeartbeat" : "Fri Aug 20 2010
08:36:38 GMT+0000 (UTC)"
},
{
"_id" : 2,
"name" : "TEST40:27020",
"health" : 1,
"state" : 2,
"self" : true
}
],
"ok" : 1
}
将主DB关闭,再次查看发现192.168.100.212:27020 DB角色转变为主。原先主库无法登陆,但是state仍然为1
health变为0
> db.runCommand({replSetGetStatus : 1});
{
"set" : "foo",
"date" : "Fri Aug 20 2010 08:41:16
GMT+0000 (UTC)",
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" :
"192.168.100.212:27018",
"health" : 0,
"state" : 1,
"uptime" : 0,
"lastHeartbeat" : "Fri Aug 20 2010
08:41:12 GMT+0000 (UTC)",
"errmsg" : "connect/transport error"
},
{
"_id" : 1,
"name" : "192.168.100.212:27019",
"health" : 1,
"state" : 2,
"uptime" : 823,
"lastHeartbeat" : "Fri Aug 20 2010
08:41:16 GMT+0000 (UTC)"
},
{
"_id" : 2,
"name" : "TEST40:27020",
"health" : 1,
"state" : 1,
"self" : true
}
],
"ok" : 1
}
>