您好,登錄后才能下訂單哦!
### 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復制集模式
僅供參考
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。