mongodb副本集配置

須要用到mongodb的時候單個實例確定是不行的,掛了怎麼辦,那而後呢,跟mysql同樣搞主從備份嗎,是能夠的mongodb這麼弄,不過官網已經不推薦了這麼幹了,推薦使用副本集的模式,而後數據再大一點到TB級別就須要使用分片節點模式了,不過沒那麼大的數據沒用到過,無論它。副本集就是每一個都是副本,沒有主的數據庫,由副本之間選舉主的mongodb,能夠這樣理解下,就是看到mysql沒有keepalived的功能,mongodb學乖了,就引入了這個功能,而且有些地方還優化了下,蠻好用的。html

mongodb副本集通常是基數個,偶數個也行的不過要引入調節器,還不如加一個mongo實例來的方便。mysql

官網教程:https://docs.mongodb.com/manual/replication/index.htmlspring

 

 配置副本集模式:sql

一、副本集之間加入認證
須要生成keyfile:
先生成keyfile
openssl rand -base64 90 > /var/lib/mongo/mongodb-keyfile
而後複製到其它的服務器中
scp /var/lib/mongo/mongodb-keyfile root@192.168.108.145: /var/lib/mongo/mongodb-keyfile
兩個服務器文件都要受權600
chmod 600 /var/lib/mongo/mongodb-keyfilemongodb

二、修改/etc/mongod.conf
服務器1的27017端口配置文件
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/數據庫

# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log服務器

# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:優化

# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfilehtm

# network interfaces
net:
port: 27017
# bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.blog


#security:
security:
keyFile: /var/lib/mongo/mongodb-keyfile
#operationProfiling:

#replication:
replication:
replSetName: water
#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

服務器1的27018配置文件
# mongod.conf

# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod2.log

# Where and how to store data.
storage:
dbPath: /var/lib/mongo2
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:

# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile

# network interfaces
net:
port: 27018
# bindIp: 0.0.0.0
# bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.


#security:
security:
keyFile: /var/lib/mongo/mongodb-keyfile
#operationProfiling:

#replication:
replication:
replSetName: water
#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
服務器2的27017端口
# mongod.conf

# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:

# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile

# network interfaces
net:
port: 27017
bindIp: 192.168.108.146
# bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.

security:
authorization: enabled
keyFile: /var/lib/mongo/mongodb-keyfile
#security:

#operationProfiling:

#replication:
replication:
replSetName: water
#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:

3個配置文件的副本集名稱都設置成同樣的,例如這裏的water

三、設置admin用戶名和密碼
use admin
db.createUser({user:"admin",pwd:"password",roles:[{role:"root",db:"admin"}]})

以auth方式啓動服務器2的mongodb,而後
#初始化,哪一個服務器先初始化就是主服務器
rs.initiate()
use admin
db.auth("admin","password");
查看副本集節點狀態
rs.status()

添加副本集
rs.add('192.168.108.145:27017')
rs.add('192.168.108.145:27018')
刪除從服務器
rs.remove('192.168.108.145:27017')
rs.remove('192.168.108.145:27018')
而後進入從服務器,查看備份數據
rs.slaveOk()
後面再進行查找操做

實驗的效果是當從的mongodb掛了的時候是卜影響項目的運行的,當主的mongodb掛了的時候,會自動在兩個從的mongodb上面推選出一個主的mongodb,,這裏mongodb必須是基數個,否則不能推選出主的mongodb,其實配置起來不難,spring boot中配置mongodb把主從IP全都加進去就好了host="IP1,IP2,IP3:

 

大概就是這樣了。

相關文章
相關標籤/搜索