本人从事 linux nginx mysql php 开发多年,联系v:luhuang2003 ,希望接触到更新的web开发软件,开此博客希望能把自己的经验和接触的东西与大家进行共享。
分类: 数据库开发技术
2013-02-21 11:51:33
原文地址: 作者:
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}) 设置优先级和隐藏成员 下面是一些参数的介绍:
|
七、移除节点:
rs.remove("ip:27017") rs.remove("ip") |
prority 优先级 ;delayed 同步间隔 ;hidden 隐藏
例子:delayed的配置:
详细的配置 |
九、测试,可以关掉主节点看看是否转移或者在主节点进行写入数据
十、节点优先级的调整:
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. |