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:

大概就是这样了。

原文地址:https://www.cnblogs.com/waterlufei/p/8135795.html