mongodb replica configure-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 331494
  • 博文数量: 68
  • 博客积分: 405
  • 博客等级: 一等列兵
  • 技术积分: 1288
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-09 15:39
个人简介

本人从事 linux nginx mysql php 开发多年,联系v:luhuang2003 ,希望接触到更新的web开发软件,开此博客希望能把自己的经验和接触的东西与大家进行共享。

文章分类

全部博文(68)

文章存档

2024年(1)

2013年(67)

我的朋友
相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: web开发

2013-02-21 14:30:41

mongodb replica configure

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

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以下的参考凯发k8官网下载客户端中心官网:

十二、手动同步数据:

 

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

> use admin

> db.runcommand({resync: 1})

 

> db.printreplicationinfo();         查看oplog的信息

手动增大oplog的大小方法

十三、凯发k8官网下载客户端中心官网解释同步方法

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.

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

上一篇:mongodb replica configure

下一篇:mongodb的监控

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图