一组维护相同数据集的 MongoDB 服务, 提代冗余和高可用性, 是所有生产部署的基础.
类似于有自动故障恢复功能的主从集群; 通俗来说就是多台机器对同一数据时行异步同步,从而使多台机器拥有同一数据的多个副本, 当主库挂掉时在不需要用户干预的情况自动切换其它备份服务器为主库
而且副本服务器做只读服务, 实现读写分离
和主从复制的区别:
# 新建主节点目录
mkdir -p /opt/mongo-replicate/myrs-27020/
cd /opt/mongo-replicate/myrs-27020/
mkdir -p data/db log
# 拷贝配置文件
cp /etc/mongod.conf ./
# 配置文件重命令
mv mongod.conf mongod-27020.conf
# 编辑配置文件
vim mongod-27020.conf
===================================================================================================
# where to write logging data.
systemLog:
destination: file
logAppend: true
# 修改日志存放路径
path: /opt/mongo-replicate/myrs-27020/log/mongod.log
# Where and how to store data.
storage:
# 修改数据库路径
dbPath: /opt/mongo-replicate/myrs-27020/data/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
# 修改进程ID存放文件
pidFilePath: /opt/mongo-replicate/myrs-27020/log/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
# 修改端口号
port: 27020
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
#security:
#operationProfiling:
replication:
# 为副本集创建一个名字
replSetName: myrs
===================================================================================================
# 启动主节点
mongod -f ./mongod-27020.conf
# 进入副本集目录
cd /opt/mongo-replicate/
# 将主节点目录复制一份
cp -r myrs-27020/ ./myrs-27021
cd myrs-27021
# 修改配置文件名
mv mongod-27020.conf mongod-27021.conf
# 修改配置文件内容
vim mongod-27021.conf
# 将所有 27020 改为 27021
%s/27020/27021/g
# 启动副节点
mongod -f ./mongod-27021.conf
1. 连接主节点
mongo --host=localhost --port=27020
2. 进行初始化
rs.initiate()
==============================================================================
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "centos:27020",
# 初始化成功
"ok" : 1,
"operationTime" : Timestamp(1582522895, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1582522895, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
# 副节点
myrs:SECONDARY>
# 再按下回车变为主节点
myrs:PRIMARY>
==============================================================================
3. 查看配置内容
rs.config()
==============================================================================
{
"_id" : "myrs",
"version" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
# 节点成员
"members" : [
{
"_id" : 0,
"host" : "centos:27020",
# 是否为仲裁节点
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
# 权重值
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
# 副本集参数设置
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5e53620f9776d48e43af7883")
}
}
==============================================================================
4. 添加副本节点
rs.add("192.168.0.10:27021")
5. 添加仲裁节点
rs.addArb("192.168.0.10:27022")
6. 客户端连接副节点后
rs.slaveOk()
7. 客户端连接到仲裁节点后
rs.slaveOk()
8. 查看副本集状态
rs.status()
==============================================================================
"members" : [
{
"_id" : 0,
"name" : "centos:27020",
"health" : 1,
"state" : 1,
# 主节点
"stateStr" : "PRIMARY",
"uptime" : 2498,
"optime" : {
"ts" : Timestamp(1582526228, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1582526228, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-02-24T06:37:08Z"),
"optimeDurableDate" : ISODate("2020-02-24T06:37:08Z"),
"lastHeartbeat" : ISODate("2020-02-24T06:37:08.969Z"),
"lastHeartbeatRecv" : ISODate("2020-02-24T06:37:08.969Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1582522895, 2),
"electionDate" : ISODate("2020-02-24T05:41:35Z"),
"configVersion" : 3
},
{
"_id" : 1,
"name" : "192.168.0.10:27022",
"health" : 1,
"state" : 7,
# 仲裁节点
"stateStr" : "ARBITER",
"uptime" : 3614,
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.0.10:27021",
"health" : 1,
"state" : 2,
# 副节点
"stateStr" : "SECONDARY",
"uptime" : 2483,
"optime" : {
"ts" : Timestamp(1582526228, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1582526228, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-02-24T06:37:08Z"),
"optimeDurableDate" : ISODate("2020-02-24T06:37:08Z"),
"lastHeartbeat" : ISODate("2020-02-24T06:37:08.969Z"),
"lastHeartbeatRecv" : ISODate("2020-02-24T06:37:08.809Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "centos:27020",
"syncSourceHost" : "centos:27020",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3
}
],
==============================================================================
两个条件同时满中才行;若票数相同, 则最新加入的节点为主节点。
一主一副一仲裁的副本集中
1.主节点故障,副节点成为主节点
2.副节点故障,主节点依旧是主节点可以进行读写
3.主节点和仲裁节点都故障, 只剩下副节点, 整个副本集只能读不能写;若仲裁节点回归, 原来的副节点,成为主节点; 若非仲裁节点回归, 新的节点成为主节点
4.副节点和仲裁节点故障, 则整个副本集进行服务降级, 原来的主节点变为副节点, 只能读, 不能写。
# 查看一下副本集的配置, 看主节点的host是否为对外的IP和PORT
rs.config()
# 获取配置信息
var config = rs.config();
config.members[0].host="192.168.0.10:27020";
# 重起配置
rs.reconfig(config);
# 如此便可对外进行连接, (如有必要还需设置一下防火墙)
spring:
data:
mongodb:
# slaveOk=true: 开启副本节点的读功能, 可实现读写分离
# connect=replicaSet: 自动到副本集中选择读写的主机, 如果 slaveOk 是打开的, 则实现读写分离
uri: mongodb://192.168.0.10:27020,192.168.0.10:27021,192.168.0.10:27022/yjh?connect=replicaSet&slaveOk=true&replicaSet=myrs