Chinaunix首页 | 论坛 | 博客
  • 博客访问: 834343
  • 博文数量: 167
  • 博客积分: 7173
  • 博客等级: 少将
  • 技术积分: 1671
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-04 23:07
文章分类

全部博文(167)

文章存档

2018年(1)

2017年(11)

2012年(2)

2011年(27)

2010年(88)

2009年(38)

分类: 数据库开发技术

2011-01-30 14:18:54

mongoDB 高可用
mongoDB支持在多个机器中通过异步复制达到故障转移和实现冗余。多机器中
同一时刻只有一台是用于写操作。正是由于这个情况,为mongoDB提供了数据
一致性的保障。担当主角色的机器能把读操作分发给slaves/secondaries。

mongodb 高可用可用分两种
    * Master-Slave Replication
    * Replica Sets

以下为两个的架构图:


官网中为选择这两种架构提供了以下意见:
    * if using     * if need automatic fail-over and recovery (easy administration): replica sets
    * if using --auth (security) : for now, master/slave
    * if using sharding : either, but replica sets are best for clusters that are not small
    * if risk averse : master/slave (replica sets are new to v1.6.0)

Master-Slave Replication
步骤:
Master:
bin/mongod --port=9999 --logpath=/data/mongodb/mongodb.log --dbpath=/data/mongodb  --directoryperdb --master --oplogSize 64 >> /dev/null 2>&1 &
相关参数:
 --master              master mode 指定改服务角色为master
 --oplogSize arg       size limit (in MB) for op log 为主从复制的日志指定大少
Slave:

bin/mongod --port=9999 --logpath=/data/mongodb/mongodb.log --dbpath=/data/mongodb  --directoryperdb --slave --source 192.168.10.103:9999 --only sky --slavedelay 10 >> /dev/null 2>&1 &

  --slave               slave mode
  --source arg          arg specifies master as 指定主服务器及端口
  --only arg            arg specifies a single database to replicate指定复制的库名字
  --slavedelay arg      arg specifies delay (in seconds) to be used 丛库检测主库的时间
                        when applying master ops to slave
  --autoresync          automatically resync if slave data is stale


Master:
db.printReplicationInfo() 打印状态
Slave:
db.printSlaveReplicationInfo() 打印从状态

主从间的安全性:
为其增加用户和密码,请参考:

详细请参考:


Replica Sets

Replica sets 在主从复制上做的扩展,增加了故障自动切换和自动修复成员节点。

步骤/加上参数--rest则可用web端进行数据观察/



还有其他很多相关信息。

10.106
bin/mongod --shardsvr --fork --replSet sky --port=9998 --logpath=/data/mongodb/mongodb.log --dbpath=/data/mongodb  --directoryperdb --rest  >> /dev/null 2>&1 &
10.107
bin/mongod --shardsvr --fork --replSet sky --port=9998 --logpath=/data/mongodb/mongodb.log --dbpath=/data/mongodb  --directoryperdb --rest  >> /dev/null 2>&1 &
10.103
bin/mongod --shardsvr --fork --replSet sky --port=9998 --logpath=/data/mongodb/mongodb.log --dbpath=/data/mongodb  --directoryperdb --rest  >> /dev/null 2>&1 &

web端图:


我这里以10.107作为主服务器
进行配置:
>config = {_id: 'sky', members: [
                           {_id: 0, host: '192.168.10.107:9998'},
                           {_id: 1, host: '192.168.10.106:9998'},
                           {_id: 2, host: '192.168.10.103:9998'}]
           }
配置文件的语法是:
{
  _id : ,
  members: [
    {
      _id : ,
      host : ,
      [, priority: ]
      [, arbiterOnly : true]
      [, votes : ]
      [, hidden : true]
      [, slaveDelay : ]
      [, buildIndexes : ]
      [, initialSync : {
           [state : 1|2,]
           [_id : ,]
           [name : ,]
           [optime : ]}]
    }
    , ...
  ],

  [settings: {
    [getLastErrorDefaults: ]
    [, heartbeatSleep : ]
    [, heartbeatTimeout : ]
    [, heartbeatConnRetries : ]
  }]
}

详细参数请参考 按需使用

>rs.initiate(config) 出现以下提示为配置成功
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
>rs.status() 查看状态
{
        "set" : "sky",
        "date" : ISODate("2011-01-30T05:36:00Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.10.107:9998",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "optime" : {
                                "t" : 1296365738000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-01-30T05:35:38Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "192.168.10.106:9998",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 13,
                        "optime" : {
                                "t" : 1296365738000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-01-30T05:35:38Z"),
                        "lastHeartbeat" : ISODate("2011-01-30T05:35:59Z")
                },
                {
                        "_id" : 2,
                        "name" : "192.168.10.103:9998",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 9,
                        "optime" : {
                                "t" : 1296365738000,
                                "i" : 1
                        },
                        "optimeDate" : ISODate("2011-01-30T05:35:38Z"),
                        "lastHeartbeat" : ISODate("2011-01-30T05:35:59Z")
                }
        ],
        "ok" : 1
}
PS:配置完后用客户端工具登录会发现 10.107 sky:PRIMARY> 其它为sky:SECONDARY> ;初始化默认id 0 为primary

状态中关键数据位
state:1表示该host是当前可以进行读写,2:不能读写
health:1表示该host目前是正常的,0:异常

可在从库上进行读写操作,发现提示为error: { "$err" : "not master and slaveok=false", "code" : 13435 }

但这运行命令查看相应状态:
>use admin
>db.runCommand({replSetGetStatus : 1});
或直接运行rs.status()
这也反应了前面所说,在物理是ms或sets  在同一时刻只有一个可读写


故障自动切换测试
把107的mongodb kill掉
状态变为
sky:SECONDARY> rs.status()
{
        "set" : "sky",
        "date" : ISODate("2011-01-30T06:09:51Z"),
        "myState" : 2,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "192.168.10.107:9998",
                        "health" : 0,
                        "state" : 1,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "t" : 1296366366000,
                                "i" : 10
                        },
                        "optimeDate" : ISODate("2011-01-30T05:46:06Z"),
                        "lastHeartbeat" : ISODate("2011-01-30T06:09:34Z"),
                        "errmsg" : "socket exception"
                },
                {
                        "_id" : 1,
                        "name" : "192.168.10.106:9998",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "optime" : {
                                "t" : 1296366366000,
                                "i" : 10
                        },
                        "optimeDate" : ISODate("2011-01-30T05:46:06Z"),
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "192.168.10.103:9998",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 2039,
                        "optime" : {
                                "t" : 1296366366000,
                                "i" : 10
                        },
                        "optimeDate" : ISODate("2011-01-30T05:46:06Z"),
                        "lastHeartbeat" : ISODate("2011-01-30T06:09:50Z")
                }
        ],
        "ok" : 1
}

103已成为primary  一个随机切换;但107起来后依然抢回主的位置


至此,两种东西也配置了一遍。
参考文章:

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