### mongodb的主從配置(不帶auth認證)linux
### 注意事項:redis
- 服務器節點以前時間要同步
mongodb
- 開啓防火牆的必定要容許經過相關端口
shell
- 開啓selinux的也要進行設置
服務器
- 創建雙擊互信模式最好不過
ide
### master配置文件ui
- 添加了master = true 這一行便可 spa
- 多網卡機器bind_ip這一行儘量寫成一個具體地址(最好寫內網地址),由於slave上是根據這個同步的unix
[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配置文件xml
添加了如下兩行
- 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複製集模式
僅供參考