1. 程式人生 > >mongodb主從配置及備份

mongodb主從配置及備份

分布式文件存儲 ase b- 操作 查看 ucc until and building

本文將介紹下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    master
172.16.0.139    salve
系統版本為 centos 7.2

部署包地址

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.3.tgz

解壓文件

[root@iZuf6ahuk73s2puww2elmpZ]# tar xf mongodb-linux-x86_64-rhel70-3.6.3.tgz 
[root@iZuf6ahuk73s2puww2elmpZ]# cd mongodb-linux-x86_64-rhel70-3.6.3/

建立數據目錄

[root@iZuf6ahuk73s2puww2elmpZ mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/data -p
[root@iZuf6ahuk73s2puww2elmpZ 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=27017
oplogSize=2048

啟動主庫

[root@iZuf6ahuk73s2puww2elmpZ 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: 5702
child process started successfully, parent exiting
[root@iZuf6ahuk73s2puww2elmpZ mongodb-linux-x86_64-rhel70-3.6.3]# ps -ef |grep mongodb
root      5702     1  2 14:38 ?        00:00:00 ./bin/mongod -f mongodb.conf
root      5729  5601  0 14:39 pts/0    00:00:00 grep --color=auto mongodb

配置從庫

部署包從主庫上拷貝過來

建立數據目錄

[root@izuf6ahuk73s2puww2elmqz mongodb-linux-x86_64-rhel70-3.6.3]# mkdir /opt/mongodb/data -p
[root@izuf6ahuk73s2puww2elmqz 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=27017
bind_ip= 0.0.0.0
slave=true
source=172.16.0.138:27017
autoresync=true

啟動從庫

[root@izuf6ahuk73s2puww2elmqz 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: 10233
child process started successfully, parent exiting
[root@izuf6ahuk73s2puww2elmqz mongodb-linux-x86_64-rhel70-3.6.3]# ps -ef |grep mongodb
root     10302     1  0 15:03 ?        00:00:09 ./bin/mongod -f mongodb.conf
root     10369 10210  0 15:38 pts/0    00:00:00 grep --color=auto mongodb
到這裏基本主從就配置完了,你可以查看主節點的local數據庫裏有沒有slave,oplog.$admin,從節點中有沒有source,me這幾個集合
接下來你可以主節點創建數據庫插入數據看看從節點是否同步過去了.這些都可以通過查看日誌來查看的

驗證結果

  • 主庫
[root@iZuf6ahuk73s2puww2elmpZ mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
> db.printReplicationInfo();
configured oplog size:   2048MB
log 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)
  • 從庫
[root@izuf6ahuk73s2puww2elmqz mongodb-linux-x86_64-rhel70-3.6.3]# ./bin/mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Server 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 testsalve
switched to db testsalve
> db
testsalve
> db.testsalve.insert({"name" : "測試同步"})
WriteResult({ "nInserted" : 1 })
> show collections
testsalve
> db.testsalve.find().pretty()
{ "_id" : ObjectId("5ad44d090dd23b7a5c1a983f"), "name" : "測試同步" }
  • 從庫上查看是否同步
> rs.slaveOk();
> show dbs;
admin      0.000GB
config     0.000GB
local      0.000GB
testsalve  0.000GB
> use testsalve
switched to db testsalve
> db.testsalve.find().pretty()
{ "_id" : ObjectId("5ad44d090dd23b7a5c1a983f"), "name" : "測試同步" }
# 數據已同步過來

註意

salve節點默認是無法讀寫的,如果非要解決,方法如下:
在從庫執行
> rs.slaveOk();

備份

  • MongoDB數據庫備份
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/目錄下
[root@iZuf6ahuk73s2puww2elmpZ 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)
#備份出來的文件
[root@iZuf6ahuk73s2puww2elmpZ 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

恢復

  • MongoDB數據庫恢復

1、語法: mongorestore -h dbhost -d dbname --dir dbdirectory 參數或名: -h: MongoDB所在服務器地址 -d: 需要恢復的數據庫實例,例如:test,當然這個名稱也可以和備份時候的不一樣,比如test2 --dir: 備份數據所在位置,例如:/opt --drop: 恢復的時候,先刪除當前數據,然後恢復備份的數據。就是說,恢復後,備份後添加修改的數據都會被刪除。

  • 示例
#先刪除當前庫
> use testsalve
switched to db testsalve
> db.dropDatabase()
{ "dropped" : "testsalve", "ok" : 1 }
#執行恢復
[root@iZuf6ahuk73s2puww2elmpZ 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 instead
2018-04-16T15:23:27.417+0800    building a list of collections to restore from /opt/testsalve dir
2018-04-16T15:23:27.418+0800    reading metadata for testsalve.testsalve from /opt/testsalve/testsalve.metadata.json
2018-04-16T15:23:27.440+0800    restoring testsalve.testsalve from /opt/testsalve/testsalve.bson
2018-04-16T15:23:27.449+0800    no indexes to restore
2018-04-16T15:23:27.449+0800    finished restoring testsalve.testsalve (1 document)
2018-04-16T15:23:27.449+0800    done
#查看恢復結果
> show dbs;
admin      0.000GB
config     0.000GB
local      0.000GB
testsalve  0.000GB
> use testsalve
switched to db testsalve
> db.testsalve.find().pretty()
{ "_id" : ObjectId("5ad44d090dd23b7a5c1a983f"), "name" : "測試同步" }

mongodb主從配置及備份