Chinaunix首页 | 论坛 | 博客
  • 博客访问: 692746
  • 博文数量: 63
  • 博客积分: 1327
  • 博客等级: 中尉
  • 技术积分: 2022
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-19 18:39
文章分类

全部博文(63)

文章存档

2013年(3)

2012年(60)

分类: Mysql/postgreSQL

2012-08-30 21:37:44

复制集

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

 

MongoDB 高可用可用分两种:

l  Master-Slave 主从复制:

         只需要在某一个服务启动时加上–master参数,而另一个服务加上–slave与–source参数,即可实现同步。MongoDB的最新版本已不再推荐此方案。

l  Replica Sets复制集:

          MongoDB在 1.6版本对开发了新功能replica set,这比之前的replication功能要强大一些,增加了故障自动切换和自动修复成员节点,各个DB之间数据完全一致,大大降低了维护成功。autoshard已经明确说明不支持replicationparis,建议使用replica set,replica set故障切换完全自动。

 

如果上图所示,Replica Sets的结构非常类似一个集群。是的,你完全可以把它当成集群,因为它确实跟集群实现的作用是一样的,其中一个节点如果出现故障,其它节点马上会将业务接过来而无须停机操作。

部署Replica Sets

接下来将一步一步的给大家分享一下实施步骤:

 

1、  创建数据文件存储路径

[root@localhost ~]# mkdir -p /data/data/r0

[root@localhost ~]# mkdir -p /data/data/r1

[root@localhost ~]# mkdir -p /data/data/r2

 

2、  创建日志文件路径

[root@localhost ~]# mkdir -p /data/log

 

3、创建主从key文件,用于标识集群的私钥的完整路径,如果各个实例的key file内容不一致,程序将不能正常用。

[root@localhost ~]# mkdir -p /data/key

[root@localhost ~]# echo "this is rs1 super secret key" > /data/key/r0

[root@localhost ~]# echo "this is rs1 super secret key" > /data/key/r1

[root@localhost ~]# echo "this is rs1 super secret key" > /data/key/r2

[root@localhost ~]# chmod 600 /data/key/r*

 

4、启动3个实例

[root@localhost ~]# /Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r0 --fork --port 28010 --dbpath /data/data/r0 --logpath=/data/log/r0.log --logappend

all output going to: /data/log/r0.log

forked process: 6573

[root@localhost ~]# /Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r1 --fork --port 28011 --dbpath /data/data/r1 --logpath=/data/log/r1.log --logappend

all output going to: /data/log/r1.log

forked process: 6580

[root@localhost ~]# /Apps/mongo/bin/mongod --replSet rs1 --keyFile /data/key/r2 --fork --port 28012 --dbpath /data/data/r2 --logpath=/data/log/r2.log --logappend

all output going to: /data/log/r2.log

forked process: 6585

[root@localhost ~]#

 

5、配置及初始化Replica Sets

[root@localhost bin]# /Apps/mongo/bin/mongo -port 28010

MongoDB shell version: 1.8.1

connecting to: 127.0.0.1:28010/test

> config_rs1 = {_id: 'rs1', members: [

...   {_id: 0, host: 'localhost:28010', priority:1},  --成员IP及端口,priority=1指PRIMARY

...   {_id: 1, host: 'localhost:28011'},

...   {_id: 2, host: 'localhost:28012'}]

...                 }

{

        "_id" : "rs1",

        "members" : [

                {

                        "_id" : 0,

                        "host" : "localhost:28010"

                },

                {

                        "_id" : 1,

                        "host" : "localhost:28011"

                },

                {

                        "_id" : 2,

                        "host" : "localhost:28012"

                }

        ]

}

rs.initiate(config_rs1);   --初始化配置

{

        "info" : "Config now saved locally.  Should come online in about a minute.",

        "ok" : 1

}

 

 

6、查看复制集状态

rs.status()

{

        "set" : "rs1",

        "date" : ISODate("2012-05-31T09:49:57Z"),

        "myState" : 1,

        "members" : [

                {

                        "_id" : 0,

                        "name" : "localhost:28010",

                        "health" : 1,      --1表明正常; 0表明异常

                        "state" : 1,       -- 1表明是Primary; 2表明是Secondary;

                        "stateStr" : "PRIMARY",  --表明此机器是主库

                        "optime" : {

                                "t" : 1338457763000,

                                "i" : 1

                        },

                        "optimeDate" : ISODate("2012-05-31T09:49:23Z"),

                        "self" : true

                },

                {

                        "_id" : 1,

                        "name" : "localhost:28011",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 23,

                        "optime" : {

                                "t" : 1338457763000,

                                "i" : 1

                        },

                        "optimeDate" : ISODate("2012-05-31T09:49:23Z"),

                        "lastHeartbeat" : ISODate("2012-05-31T09:49:56Z")

                },

                {

                        "_id" : 2,

                        "name" : "localhost:28012",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 23,

                        "optime" : {

                                "t" : 1338457763000,

                                "i" : 1

                        },

                        "optimeDate" : ISODate("2012-05-31T09:49:23Z"),

                        "lastHeartbeat" : ISODate("2012-05-31T09:49:56Z")

                }

        ],

        "ok" : 1

}

rs1:PRIMARY>

 

还可以用isMaster查看Replica Sets状态。

rs1:PRIMARY> rs.isMaster()

{

        "setName" : "rs1",

        "ismaster" : true,

        "secondary" : false,

        "hosts" : [

                "localhost:28010",

                "localhost:28012",

                "localhost:28011"

        ],

        "maxBsonObjectSize" : 16777216,

        "ok" : 1

}

rs1:PRIMARY>


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