Chinaunix首页 | 论坛 | 博客
  • 博客访问: 330743
  • 博文数量: 67
  • 博客积分: 405
  • 博客等级: 一等列兵
  • 技术积分: 1278
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-09 15:39
个人简介

本人从事 Linux Mysql PHP 开发多年,联系QQ:83882066 ,希望接触到更新的web开发软件,开此博客希望能把自己的经验和接触的东西与大家进行共享。 详细介绍: http://blog.chinaunix.net/uid-28543558-id-3492336.html 新博客地址: http://luhuang.sinaapp.com

文章分类

全部博文(67)

文章存档

2013年(67)

我的朋友

分类: Web开发

2013-02-21 14:30:41

Mongodb Replica Configure

     我在配置replica的时候,文档中也把官网的中一些重要解释放在里面了但是并没有用中文做必要的解释,不过都是很容易理解的。说一下环境,这里的环境是:

system:centos 64bit  生产环境不用说,直接选择64的

机器:dell R410  三台 (对于Replica的环境,只要是两台以上同网段的就行)

Mongodb Version:2.0.4

不建议采用rpm和yum的方式(个人习惯)

一、命名主机和设置hosts文件:

   编辑/etc/sysconfig/network /etc/hosts
 

二、创建用户及目录:

        #useradd mongodb

        # # mkdir /mongodb/{data,logs} -pv

        #chown -R mongodb /mongodb


 

三、Download MongoDB

         #wget

         # # tar xvf mongodb-linux-x86_64-2.0.6.tgz -C /usr/local/ 

         # ln -sv mongodb-linux-x86_64-2.0.6 /mongodb  

         # chown -R mongodb.mongodb mongodb-linux-x86_64-2.0.6

 四、repl节点启动:

   在每个节点进行启动

    /usr/local/mongodb/bin/mongod --fork --rest --replSet myset --logpath /mongodb/logs/mongodb.log --dbpath /mongodb/data --logappend --port 27017

五、configure Replica Sets;

./mongo

> config={_id:"myset",members:[

... {_id:0,host:"192.168.29.129:27017"},

... {_id:1,host:"192.168.29.128:27017"},]

... }

{

         "_id" : "myset",

         "members" : [

                   {

                            "_id" : 0,

                            "host" : "192.168.29.129:27017"

                   },

                   {

                            "_id" : 1,

                            "host" : "192.168.29.128:27017"

                   }

         ]

}

> rs.initiate(config);

 

{

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

         "ok" : 1

}

>rs.conf() 查看配置信息


说明:在从节点中show collections不能使用,只有主节点才能进行读写,从节点不能。并且相关的数据保存在local数据库中


SECONDARY> show collections;  说明从节点不能进行读写

Thu Aug  9 17:42:12 uncaught exception: error: { "$err" : "not master and slaveok=false", "code" : 13435 }

解决:

SECONDARY>db.getMongo().setSlaveOk() 这样就可以了

 查看数据文件:

SECONDARY> show dbs

local 1.203125GB

test  (empty)

SECONDARY> show dbs

local 1.203125GB

test  (empty)

SECONDARY> use local

switched to db local

SECONDARY> show collections;

me

oplog.rs            数据语句是先存储在该文件中的,100ms写入磁盘

replset.minvalid

system.indexes

system.replset

SECONDARY>

 查看状态:

PRIMARY> rs.isMaster();

{

         "setName" : "myset",

         "ismaster" : true,

         "secondary" : false,

         "hosts" : [

                   "192.168.29.129:27017",

                   "192.168.29.128:27017"

         ],

         "primary" : "192.168.29.129:27017",

         "me" : "192.168.29.129:27017",

         "maxBsonObjectSize" : 16777216,

         "ok" : 1

}

PRIMARY>

SECONDARY> rs.status();

{

         "set" : "myset",

         "date" : ISODate("2012-08-07T16:35:07Z"),

         "myState" : 2,

         "syncingTo" : "192.168.29.129:27017",

         "members" : [

                   {

                            "_id" : 0,

                            "name" : "192.168.29.129:27017",

                            "health" : 1,

                            "state" : 1,

                            "stateStr" : "PRIMARY",

                            "uptime" : 553,

                            "optime" : {

                                     "t" : 1344356746000,

                                     "i" : 1

                            },

                            "optimeDate" : ISODate("2012-08-07T16:25:46Z"),

                            "lastHeartbeat" : ISODate("2012-08-07T16:35:06Z"),

                            "pingMs" : 0

                   },

                   {

                            "_id" : 1,

                            "name" : "192.168.29.128:27017",

                            "health" : 1,

                            "state" : 2,

                            "stateStr" : "SECONDARY",

                            "optime" : {

                                     "t" : 1344356746000,

                                     "i" : 1

                            },

                            "optimeDate" : ISODate("2012-08-07T16:25:46Z"),

                            "self" : true

                   }

         ],

         "ok" : 1

}

SECONDARY>

六、添加新成员:

 

1、在服务器上启动mongoDB

/usr/local/mongodb/bin/mongod --fork --rest --replSet myset --logpath /mongodb/logs/mongodb.log --dbpath /mongodb/data --logappend --port 27017

2、在主节点上进行添加成员

PRIMARY> rs.add("192.168.29.130:27017")

{ "ok" : 1 }>rs.conf()      查看配置信息

>rs.status()     确定是否添加进去

或者:

rs.add({_id: 1, host: "IP:27017", priority: 0, hidden: true}) 设置优先级和隐藏成员

  下面是一些参数的介绍:

arbiterOnly

false

If true, this member will participate in vote but receive no data.

1.6

buildIndexes

true

When false, prevent secondary indexes from being created on this member. This is typically used on machines that are pure "backup" machines that are never queried. By not having the secondary indexes, the member performs less works on writes and requires less ram. Note the _id index is still created. Can only be set to false if priority:0. It is rare to use this option.

1.6

hidden

false

If true, do not advertise the member's existence to clients in isMaster command responses. Hidden replicas makes sense for replicas of data which have very different use patterns (reporting, integration, backup, etc.) than the main set of replicas; this option allows you to keep from sending normal non-primary queries to the node.

1.7

priority

1.0

Priority of the server for elections. Higher priority servers will be preferred as primary. ()

1.6, 1.9

tags

{}

An document representing the location of this server. Tags can be used for location-aware write guarantees and read locality, see 

1.9.1

0

Number of seconds to remain behind the primary.
A value of 0 implies "as up-to-date as possible". 
Used to recover from human errors (e.g.: accidentally dropping a database).
Can only be set on members with priority 0. Slave delay members are a great way to keep a rolling backup from a certain amount of time in the past.

1.6.3

votes

1

Number of votes this member has in an election. Generally you should not change this. ()

1.6

七、移除节点:


rs.remove("IP:27017")

rs.remove("IP")

八、replica 的成员级别类型:

 

prority 优先级 Delayed 同步间隔 Hidden 隐藏

You must send the rs.reconfig() command to a set member that can become primary. In the above example, if you issue the rs.reconfig() operation to the member with the _id of 0, the operation will fail.这个是隐藏的解释,其它的就不用说了。

   例子:Delayed的配置:

cfg = rs.conf()

cfg.members[0].priority = 0

cfg.members[0].slaveDelay = 3600

rs.reconfig(cfg)

详细的配置


九、测试,可以关掉主节点看看是否转移或者在主节点进行写入数据

十、节点优先级的调整:

cfg = rs.conf()

cfg.members[0].priority = 0.5

cfg.members[1].priority = 2

cfg.members[2].priority = 2

rs.reconfig(cfg)

要是cfg.members.priority=0 说明永远不会成为primary;当在0-0.5的时候很少会成为primary,其次默认是1,数值越大越优先级越高,如果不生效使用以下命令:

rs.reconfig(cfg,{force:true})

十一、rs.reconfig使用条件:

当成员节点down时,在成员中没有主节点或者出现在偶数节点中不能选举主节下点时使用。此操作很是危险!

>config = rs.config()

> printjson(config) # store this somewhere

> config.members = [config.members[1], config.members[3], config.members[4]]

> rs.reconfig(config, {force : true})

这个是2.0以上版本的操作方法:对于2.0以下的参考官网:

十二、手动同步数据:

 

当不能自动进行同步,登录到不能同步的服务器上:(是手动同步所有的数据)

> use admin

> db.runCommand({resync: 1})

 

> db.printReplicationInfo();         查看opLog的信息

手动增大oplog的大小方法

十三、官网解释同步方法

Perform a full resync. If you stop the failed mongod, delete all data in the dbpath (including subdirectories), and restart it, it will automatically resynchronize itself. Obviously it would be better/safer to back up the data first. If disk space is adequate, simply move it to a backup location on the machine if appropriate. Resyncing may take a long time if the database is huge or the network slow – even idealized one terabyte of data would require three hours to transmit over gigabit ethernet.

Copy data from another member: You can copy all the data files from another member of the set IF you have a snapshot of that member's data file's. This can be done in a number of ways. The simplest is to stop mongod on the source member, copy all its files, and then restart mongod on both nodes. The Mongo  feature is another way to achieve this if you are using EBS or a SAN. On a slow network, snapshotting all the datafiles from another (inactive) member to a gziped tarball is a good solution. Also similar strategies work well when using SANs and services such as Amazon Elastic Block Service snapshots.

Find a member with older data: Note: this is only possible (and occurs automatically) in v1.8+. If another member of the replica set has a large enough oplog or is far enough behind that the stale member can sync from it, the stale member can bootstrap itself from this member.

十四、限制说明:


A set can contain

·         最多有12个成员

·         最多只能在7个成员中进行选举

补充点:节点的类型


·         Primary - Can be thought of as "master" although which server is primary can vary over time. Only 1 server is primary at a given point in time.

·         Secondary - Can be thought of as a slave in the cluster; varies over time.

·         Recovering - getting back in sync before entering Secondary mode.

阅读(2080) | 评论(0) | 转发(0) |
0

上一篇:Mongodb Replica Configure

下一篇:MongoDB的监控

给主人留下些什么吧!~~