mongodb副本集配置


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

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

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

 

 配置副本集模式:

1、副本集之間加入認證
需要生成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-keyfile

2、修改/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 pidfile

# network interfaces
net:
port: 27017
# 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:

服務器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

3、設置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:

 

大概就是這樣了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM