### mongodb的复制集
### 注意点
- 服务器节点之前时间要同步
- 开启防火墙的一定要允许通过
- 开启selinux的也要进行设置
- 建立双击互信模式最好不过
### 主服务器配置文件
- 添加一行 replSet = zhuima 定义一个副本集
[root@blog ~]# sed -e ‘/^#/d;/^$/d‘ /etc/mongodb.conf bind_ip = 192.168.58.10 port = 27017 fork = true pidfilepath = /var/run/mongodb/mongodb.pid logpath = /var/log/mongodb/mongodb.log dbpath =/mongo/data journal = true replSet = zhuima nohttpinterface = true [root@blog ~]#
### 从服务器配置文件
[root@redis ~]# sed -e ‘/^#/d;/^$/d‘ /etc/mongodb.conf bind_ip = 192.168.58.30 port = 27017 fork = true pidfilepath = /var/run/mongodb/mongodb.pid logpath = /var/log/mongodb/mongodb.log dbpath =/var/lib/mongodb journal = true replSet = zhuima nohttpinterface = true [root@redis ~]#
### 重启服务器进行副本集初始化操作
- 主服务器
> rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "192.168.58.30:27017", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } > rs.config function () { return rs.conf(); } > rs.config() { "_id" : "zhuima", "version" : 1, "members" : [ { "_id" : 0, "host" : "192.168.58.30:27017" } ] } >
- 从服务器
> rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "192.168.58.10:27017", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } > rs.config() { "_id" : "zhuima", "version" : 1, "members" : [ { "_id" : 0, "host" : "192.168.58.10:27017" } ] } >
### 在主服务器上进行副本集节点添加
zhuima:PRIMARY> rs.add("192.168.58.30:27017") { "ok" : 1 } zhuima:PRIMARY> rs.config() { "_id" : "zhuima", "version" : 2, "members" : [ { "_id" : 0, "host" : "192.168.58.10:27017" }, { "_id" : 1, "host" : "192.168.58.30:27017" } ] } zhuima:PRIMARY>
- 从节点上
rs.slaveOk() 让自己成为从节点
### 过几分钟,观察数据同步情况
### 后记
mongodb的replSet复制集是mongodb官方推荐的一种集群方式
放弃master/slave,跟replSet走吧!
本文出自 “追马” 博客,请务必保留此出处http://lovelace.blog.51cto.com/1028430/1441043