### mongodb的主从配置(不带auth认证)
### 注意事项:
- 服务器节点之前时间要同步
- 开启防火墙的一定要允许通过相关端口
- 开启selinux的也要进行设置
- 建立双击互信模式最好不过
### master配置文件
- - 添加了master = true 这一行即可
- - 多网卡机器bind_ip这一行尽可能写成一个具体地址(最好写内网地址),因为slave上是根据这个同步的
[[email protected] ~]# 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 [[email protected] ~]#
### slave配置文件
- 添加了以下两行
- slave = true
- source = 192.168.58.10:27017
[[email protected] ~]# 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 [[email protected] ~]#
### 重启两节点的mongodb服务进行初始化操作并验证数据同步情况
- master
[[email protected] ~]# [[email protected] ~]# hostname blog.unix178.com [[email protected] ~]# mongo MongoDB shell version: 2.4.6 connecting to: test > show dbs admin0.203125GB local10.0732421875GB test0.203125GB zhuima0.203125GB >
- slave
[[email protected] ~]# hostname redis.unix178.com [[email protected] ~]# mongo MongoDB shell version: 2.4.6 connecting to: test > show dbs admin0.203125GB local0.078125GB zhuima0.203125GB >
### 查看同步情况
- master服务器上
[[email protected] ~]# 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服务器上
[[email protected] ~]# 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上添加数据
[[email protected] ~]# 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上验证
[[email protected] ~]# 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复制集模式
- 仅供参考
时间: 2024-10-17 05:33:30