Mongodb配置主從模式,進行數據的同步與備份
1. 背景
- 做數據庫的人都知道要對數據做備份,避免數據庫單點部署,以防止數據遭到毀滅性破壞。
- mongodb提供的Master/slave主從模式,剛好能支持這種雙機熱備份,還有讀寫分離,集羣部署等。
2. 環境。
- 系統:win7
- MongoDB 3.4.6
3. 配置主從模式
3.1. 模型圖
- 目標:就是將Master中的數據,備份到兩個slave中。
3.2. 配置Master
- 主數據庫的配置非常簡單,只需要按照如下命令,在主機 172.16.1.99 上啓動mongodb,然後等待slave們的連接…
mongod --dbpath=f:/data --master
3.3. 配置slave
- 從數據庫(slave1: 172.16.7.107)只需要在啓動mongodb時,指定角色爲slave,以及數據源地址(master的mongodb地址),詳細的命令如下:
mongod --dbpath=f:/data --slave --source=172.16.1.99:27017
- 如果slave連接上了master,數據就會開始自動備份,如下:
出現了 sycing和sleep 說明已經從主數據庫複製完成了。
後面如果有數據寫入到master中,slave也會間隔一定的時間進行相應的同步:
3.4. 讀取slave數據庫
- 在默認情況下,mongodb是不支持從屬數據庫(slave)的數據讀取操作的,這是爲了實現讀寫分離。
- 但是沒關係,在驅動中給我們提供了一個叫做“slaveOkay”來讓我們可以顯示的讀取從屬數據庫,來減輕主數據庫的性能壓力。需要在從數據庫機器的shell模式中執行rs.slaveOk(),官方解釋如下:
Provides a shorthand for the following operation:
db.getMongo().setSlaveOk()
This allows the current connection to allow read operations to run on secondary members. See the readPref() method for more fine-grained control over read preference in the mongo shell.
- 第一步:在slave機器上,鍵入如下命令 mongo 127.0.0.1:27017,進入MongoDB shell模式
mongo 127.0.0.1:27017
- 第二步:默認情況下,鍵入如下命令 show dbs,發現無法查看數據。提示如下error:
show dbs
'''
2018-01-04T15:32:36.389+0800 E QUERY [thread1] Error: listDatabases failed:{
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk"
} :
'''
- 第三步:執行命令 rs.slaveOk(),發現就可以看到從數據庫上所有的數據了。注意,一定要注意大小寫!!!
rs.slaveOk()
show dbs
3.5. 關於Mongodb可視化工具MongoBooster
3.5.1. 問題點
上面,我們通過 rs.slaveOk() 設置,就可以看到從數據庫中的數據了。但是從MongoBooster中,依然無法查看這個數據(暫時不知道如何設置…….歡迎指導)
測試時可以連通的。
但是連接時,還是會提示如下錯誤:listDatabases failed:not master and slaveOk=false “code” : 13435
{
"message" : "listDatabases failed:not master and slaveOk=false",
"stack" : "Error: listDatabases failed:not master and slaveOk=false" +
"at _getErrorWithCode (evalmachine.<anonymous>:1359:13)" +
"at Mongo.getDBs (evalmachine.<anonymous>:989:15)" +
"at Mongo.getDBNames (evalmachine.<anonymous>:1042:17)" +
"at evalmachine.<anonymous>:1:15" +
"at ContextifyScript.Script.runInContext (vm.js:37:29)" +
"at Object.exports.runInContext (vm.js:69:17)" +
"at C:\\Users\\Administrator\\AppData\\Local\\mongobooster\\app-3.5.7\\resources\\app.asar\\backend\\mongoHandler.js:549:23" +
"at tryBlock (C:\\Users\\Administrator\\AppData\\Local\\mongobooster\\app-3.5.7\\resources\\app.asar\\node_modules\\asyncawait\\src\\async\\fiberManager.js:39:33)" +
"at runInFiber (C:\\Users\\Administrator\\AppData\\Local\\mongobooster\\app-3.5.7\\resources\\app.asar\\node_modules\\asyncawait\\src\\async\\fiberManager.js:26:9)",
"code" : 13435
}
3.5.2. 思路
- 如果想通過MongoBooster查看slave中的數據,就要首先把從數據庫的屬性改變,讓它的身份不在是slave,讓它以一個普通身份,或者master身份啓動,就能通過MongoBooster查看數據了……
- 爲什麼要通過MongoBooster查看數據……當然是爲了確認數據的同步情況了,可視化多麼直觀啊…
# 以普通身份啓動mongodb
mongod --dbpath=f:/data
# 以master身份啓動mongodb
mongod --dbpath=f:/data --master
參考文章:
8天學通MongoDB——第五天 主從複製:https://www.cnblogs.com/huangxincheng/archive/2012/03/04/2379755.html
MongoDB學習九–MongoDB的主從複製:http://blog.csdn.net/done58/article/details/48032471