重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
#useradd mongodb # # mkdir /mongodb/{data,logs} -pv #chown -R mongodb /mongodb |
./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()查看配置信息 |
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的配置:
http://docs.mongodb.org/manual/administration/replica-sets/#replica-set-admin-procedure-replace-member详细的配置 |
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}) |
>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以下的参考官网: http://www.mongodb.org/display/DOCS/Reconfiguring+a+replica+set+when+members+are+down |
当不能自动进行同步,登录到不能同步的服务器上:(是手动同步所有的数据) > use admin > db.runCommand({resync: 1}) |
> db.printReplicationInfo(); 查看opLog的信息 http://www.mongodb.org/display/DOCS/Halted+Replication手动增大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 fsync and lock 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. |