標籤:DBName 接下來 大小 系統版本 read 建立 tps data use
本文將介紹下mongodb主從配置及備份
MongoDB 是一個基於分布式檔案儲存體的資料庫。由 C++ 語言編寫。旨在為 WEB 應用提供可擴充的高效能資料儲存解決方案。
MongoDB 是一個介於關聯式資料庫和非關聯式資料庫之間的產品,是非關聯式資料庫當中功能最豐富,最像關聯式資料庫的。
主從伺服器的實現原理
首先,主節點會把本服務的與寫有關的操作記錄下來,讀操來不記錄,這些操作就記錄在local資料庫中的oplog.$admin這個集合中,這是一個固定集合,大小是可以配置的,主要是通過配置oplogSize這個參數來實現,單位是M,大小一般為磁碟剩餘空間的5%左右.因為是固定集合所以當固定集合放滿日誌的時候,新進來的日誌就會把最舊的日誌覆蓋掉,如果這個值設定的不合理,導致資料很快的被覆蓋,而從節點沒有來得及更新,這樣就會產生資料不同步的情況.設定為主節點的local資料庫有會有oplog.$admin與slave這兩個集合.slave記錄的是從節點的資訊.
從節點與主節點的資料同步主要是從節點定時的會去串連主節點,請求主節點的動作記錄,從而對自己的資料副表進行同樣的操作來達到資料的同步.從節點的local資料庫中會多了source與me這兩個集合,source是記錄主節點的資訊,me是記錄從節點的標識
環境配置
我這裡準備了2台機器作為示範機172.16.0.138 master172.16.0.139 salve系統版本為 centos 7.2
部署套件地址
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.3.tgz
解壓檔案
[[email protected]]# tar xf mongodb-linux-x86_64-rhel70-3.6.3.tgz [[email protected]]# cd mongodb-linux-x86_64-rhel70-3.6.3/
建立資料目錄
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/data -p[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/logs -p
設定檔修改
# 設定資料檔案的存放目錄dbpath=/opt/mongodb/data# 設定記錄檔的存放目錄及其記錄檔名logpath=/opt/mongodb/logs/mongodb.log# 設定連接埠號碼(預設的連接埠號碼是 27017)master=true# 設定為以守護進程的方式運行,即在後台運行fork=true#監聽網卡bind_ip= 0.0.0.0#服務連接埠port=27017oplogSize=2048
啟動主庫
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongod -f mongodb.conf about to fork child process, waiting until server is ready for connections.forked process: 5702child process started successfully, parent exiting[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ps -ef |grep mongodbroot 5702 1 2 14:38 ? 00:00:00 ./bin/mongod -f mongodb.confroot 5729 5601 0 14:39 pts/0 00:00:00 grep --color=auto mongodb
配置從庫
部署套件從主庫上拷貝過來
建立資料目錄
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/data -p[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/logs -p
修改設定檔
# 設定資料檔案的存放目錄dbpath=/opt/mongodb/data# 設定記錄檔的存放目錄及其記錄檔名logpath=/opt/mongodb/logs/mongodb.log# 設定為以守護進程的方式運行,即在後台運行fork=true#服務連接埠port=27017bind_ip= 0.0.0.0slave=truesource=172.16.0.138:27017autoresync=true
啟動從庫
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongod -f mongodb.conf about to fork child process, waiting until server is ready for connections.forked process: 10233child process started successfully, parent exiting[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ps -ef |grep mongodbroot 10302 1 0 15:03 ? 00:00:09 ./bin/mongod -f mongodb.confroot 10369 10210 0 15:38 pts/0 00:00:00 grep --color=auto mongodb到這裡基本主從就配置完了,你可以查看主節點的local資料庫裡有沒有slave,oplog.$admin,從節點中有沒有source,me這幾個集合接下來你可以主節點建立資料庫插入資料看看從節點是否同步過去了.這些都可以通過查看日誌來查看的
驗證結果
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongoMongoDB shell version v3.6.3connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.6.3> db.printReplicationInfo();configured oplog size: 2048MBlog length start to end: 1517secs (0.42hrs)oplog first event time: Mon Apr 16 2018 14:38:53 GMT+0800 (CST)oplog last event time: Mon Apr 16 2018 15:04:10 GMT+0800 (CST)now: Mon Apr 16 2018 15:04:11 GMT+0800 (CST)
[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongoMongoDB shell version v3.6.3connecting to: mongodb://127.0.0.1:27017MongoDB server version: 3.6.3Server has startup warnings: > db.printSlaveReplicationInfo()source: 172.16.0.138:27017 syncedTo: Mon Apr 16 2018 15:04:30 GMT+0800 (CST) 7 secs (0 hrs) behind the freshest member (no primary available at the moment)
同步測試
> use testsalveswitched to db testsalve> dbtestsalve> db.testsalve.insert({"name" : "測試同步"})WriteResult({ "nInserted" : 1 })> show collectionstestsalve> db.testsalve.find().pretty(){ "_id" : ObjectId("5ad44d090dd23b7a5c1a983f"), "name" : "測試同步" }
> rs.slaveOk();> show dbs;admin 0.000GBconfig 0.000GBlocal 0.000GBtestsalve 0.000GB> use testsalveswitched to db testsalve> db.testsalve.find().pretty(){ "_id" : ObjectId("5ad44d090dd23b7a5c1a983f"), "name" : "測試同步" }# 資料已同步過來
注意
salve節點預設是無法讀寫的,如果非要解決,方法如下:在從庫執行> rs.slaveOk();
備份
1、文法: mongodump -h dbhost -d dbname -o dbdirectory 參數說明: -h: MongDB所在伺服器位址,例如:127.0.0.1,當然也可以指定連接埠號碼:127.0.0.1:27017 -d: 需要備份的資料庫執行個體,例如:test -o: 備份的資料存放位置,例如:/home/mongodump/,當然該目錄需要提前建立,這個目錄裡面存放該資料庫執行個體的備份資料。
#以下命令備份了testsalve庫到/opt/目錄下[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongodump -h 127.0.0.1:27017 -d testsalve -o /opt/2018-04-16T15:19:54.779+0800 writing testsalve.testsalve to 2018-04-16T15:19:54.780+0800 done dumping testsalve.testsalve (1 document)#備份出來的檔案[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ll /opt/testsalve/total 8-rw-r--r-- 1 root root 45 Apr 16 15:19 testsalve.bson-rw-r--r-- 1 root root 133 Apr 16 15:19 testsalve.metadata.json
恢複
1、文法: mongorestore -h dbhost -d dbname --dir dbdirectory 參數或名: -h: MongoDB所在伺服器位址 -d: 需要恢複的資料庫執行個體,例如:test,當然這個名稱也可以和備份時候的不一樣,比如test2 --dir: 備份資料所在位置,例如:/opt --drop: 恢複的時候,先刪除當前資料,然後恢複備份的資料。就是說,恢複後,備份後添加修改的資料都會被刪除。
#先刪除當前庫> use testsalveswitched to db testsalve> db.dropDatabase(){ "dropped" : "testsalve", "ok" : 1 }#執行恢複[[email protected] mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongorestore -h 127.0.0.1:27017 -d testsalve --dir /opt/testsalve/2018-04-16T15:23:27.416+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead2018-04-16T15:23:27.417+0800 building a list of collections to restore from /opt/testsalve dir2018-04-16T15:23:27.418+0800 reading metadata for testsalve.testsalve from /opt/testsalve/testsalve.metadata.json2018-04-16T15:23:27.440+0800 restoring testsalve.testsalve from /opt/testsalve/testsalve.bson2018-04-16T15:23:27.449+0800 no indexes to restore2018-04-16T15:23:27.449+0800 finished restoring testsalve.testsalve (1 document)2018-04-16T15:23:27.449+0800 done#查看恢複結果> show dbs;admin 0.000GBconfig 0.000GBlocal 0.000GBtestsalve 0.000GB> use testsalveswitched to db testsalve> db.testsalve.find().pretty(){ "_id" : ObjectId("5ad44d090dd23b7a5c1a983f"), "name" : "測試同步" }
mongodb主從配置及備份