### mongodb的主从配置(不带auth认证)
### 注意事项:
- 服务器节点之前时间要同步
- 开启防火墙的一定要允许通过相关端口
- 开启selinux的也要进行设置
- 建立双击互信模式最好不过
### master配置文件
- 添加了master = true 这一行即可
- 多网卡机器bind_ip这一行尽可能写成一个具体地址(最好写内网地址),因为slave上是根据这个同步的
[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 master = true nohttpinterface = true [root@blog ~]#
### slave配置文件
-
添加了以下两行
- source = 192.168.58.10:27017
[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 nohttpinterface = true slave = true source = 192.168.58.10:27017 [root@redis ~]#
### 重启两节点的mongodb服务进行初始化操作并验证数据同步情况
master
[root@blog ~]# [root@blog ~]# hostname blog.unix178.com [root@blog ~]# mongo MongoDB shell version: 2.4.6 connecting to: test > show dbs admin0.203125GB local10.0732421875GB test0.203125GB zhuima0.203125GB >
slave
[root@redis ~]# hostname redis.unix178.com [root@redis ~]# mongo MongoDB shell version: 2.4.6 connecting to: test > show dbs admin0.203125GB local0.078125GB zhuima0.203125GB >
### 查看同步情况
master服务器上
[root@blog ~]# mongo 192.168.58.30 MongoDB shell version: 2.4.6 connecting to: 192.168.58.30/test > show dbs admin0.203125GB local0.078125GB nick0.203125GB test0.203125GB zhuima0.203125GB >
slave服务器上
[root@redis ~]# mongo 192.168.58.10 MongoDB shell version: 2.4.6 connecting to: 192.168.58.10/test > show dbs; admin0.203125GB local10.0732421875GB nick0.203125GB test0.203125GB zhuima0.203125GB >
### 添加新的数据进行验证
master上添加数据
[root@redis ~]# mongo 192.168.58.10 MongoDB shell version: 2.4.6 connecting to: 192.168.58.10/test > show dbs; admin0.203125GB local10.0732421875GB nick0.203125GB test0.203125GB zhuima0.203125GB > use nick switched to db nick > use newdb switched to db newdb > db.newdb.insert({new:"yes",info:"nothing"}) > show collections newdb system.indexes > db.newdb.find() { "_id" : ObjectId("53cbd4b35974b01658718b2d"), "new" : "yes", "info" : "nothing" } >
slave上验证
[root@blog ~]# mongo 192.168.58.30 MongoDB shell version: 2.4.6 connecting to: 192.168.58.30/test > show dbs admin0.203125GB local0.078125GB nick0.203125GB test0.203125GB zhuima0.203125GB > show dbs admin0.203125GB local0.078125GB newdb0.203125GB nick0.203125GB test0.203125GB zhuima0.203125GB > use newdb switched to db newdb > show collections newdb system.indexes > db.newdb.find() { "_id" : ObjectId("53cbd4b35974b01658718b2d"), "new" : "yes", "info" : "nothing" } >
### 后记
mongodb官方已经不再推荐使用master/salve模式的结构
官方推荐使用replica_set复制集模式
仅供参考
本文出自 “追马” 博客,请务必保留此出处http://lovelace.blog.51cto.com/1028430/1441037