Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2140797
  • 博文数量: 157
  • 博客积分: 10047
  • 博客等级: 上将
  • 技术积分: 6757
  • 用 户 组: 普通用户
  • 注册时间: 2005-05-19 11:38
文章分类

全部博文(157)

文章存档

2011年(16)

2010年(50)

2009年(42)

2008年(49)

我的朋友

分类: LINUX

2010-08-23 14:55:10


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
}

阅读(1683) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~