本文只说如何操作配置副本集,历程艰难,官网文档看了半天也只说了怎么添加单个,没有给出来一个完整的流程。
1.第一步安装,参考前一篇安装即可。
配置三台虚拟机:
192.168.182.142
192.168.182.143
192.168.182.144
还需要安装mongosh,安装了的可以忽略:
安装步骤如下:
1.配置repo
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
2. 安装
yum install -y mongodb-mongosh
执行
mongosh
2.第二步启用副本,在配置文件中添加 replSet=rs1
#指定数据库路径
dbpath=/usr/local/mongodb/data
#指定MongoDB日志文件
logpath=/usr/local/mongodb/logs/mongodb.log
# 使用追加的方式写日志
logappend=true
#端口号
port=27017
#方便外网访问,外网所有ip都可以访问,不要写成固定的linux的ip
bind_ip=0.0.0.0
fork=true # 以守护进程的方式运行MongoDB,创建服务器进程
auth=false #启用用户验证
#启用日志文件
journal=true
#以后台方式运行进程
replSet=rs1
pidfilepath=/usr/local/mongodb/pid/main.pid
不添加 replSet=rs1, 初始化副本的时候会报错:MongoServerError: This node was not started with replication enabled.
配置添修改完成之后,重启三个节点。
3.第三步初始化副本集
登录任意一节点,执行 rs.initiate
rs.initiate({_id:"rs1",
members:[{_id:0,host:"192.168.182.142:27017" ,priority:2},
{_id:1,host:"192.168.182.143:27017",priority:1},
{_id:2,host:"192.168.182.144:27017", arbiterOnly:true}]})
不用切换db,也不用授权直接在test下执行
过了一会之后,节点显示都变了
查看rs信息
rs.status()
{
set: 'rs1',
date: ISODate("2023-06-03T14:43:17.576Z"),
myState: 1,
term: Long("1"),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long("2000"),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 2,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1685803395, i: 1 }), t: Long("1") },
lastCommittedWallTime: ISODate("2023-06-03T14:43:15.072Z"),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1685803395, i: 1 }), t: Long("1") },
appliedOpTime: { ts: Timestamp({ t: 1685803395, i: 1 }), t: Long("1") },
durableOpTime: { ts: Timestamp({ t: 1685803395, i: 1 }), t: Long("1") },
lastAppliedWallTime: ISODate("2023-06-03T14:43:15.072Z"),
lastDurableWallTime: ISODate("2023-06-03T14:43:15.072Z")
},
lastStableRecoveryTimestamp: Timestamp({ t: 1685803355, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate("2023-06-03T14:16:54.917Z"),
electionTerm: Long("1"),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1685801804, i: 1 }), t: Long("-1") },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1685801804, i: 1 }), t: Long("-1") },
numVotesNeeded: 2,
priorityAtElection: 2,
electionTimeoutMillis: Long("10000"),
numCatchUpOps: Long("0"),
newTermStartDate: ISODate("2023-06-03T14:16:54.942Z"),
wMajorityWriteAvailabilityDate: ISODate("2023-06-03T14:16:55.696Z")
},
members: [
{
_id: 0,
name: '192.168.182.142:27017',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 1792,
optime: { ts: Timestamp({ t: 1685803395, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-06-03T14:43:15.000Z"),
lastAppliedWallTime: ISODate("2023-06-03T14:43:15.072Z"),
lastDurableWallTime: ISODate("2023-06-03T14:43:15.072Z"),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1685801814, i: 1 }),
electionDate: ISODate("2023-06-03T14:16:54.000Z"),
configVersion: 1,
configTerm: 1,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: '192.168.182.143:27017',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 1593,
optime: { ts: Timestamp({ t: 1685803395, i: 1 }), t: Long("1") },
optimeDurable: { ts: Timestamp({ t: 1685803395, i: 1 }), t: Long("1") },
optimeDate: ISODate("2023-06-03T14:43:15.000Z"),
optimeDurableDate: ISODate("2023-06-03T14:43:15.000Z"),
lastAppliedWallTime: ISODate("2023-06-03T14:43:15.072Z"),
lastDurableWallTime: ISODate("2023-06-03T14:43:15.072Z"),
lastHeartbeat: ISODate("2023-06-03T14:43:16.044Z"),
lastHeartbeatRecv: ISODate("2023-06-03T14:43:16.865Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: '192.168.182.142:27017',
syncSourceId: 0,
infoMessage: '',
configVersion: 1,
configTerm: 1
},
{
_id: 2,
name: '192.168.182.144:27017',
health: 1,
state: 7,
stateStr: 'ARBITER',
uptime: 1593,
lastHeartbeat: ISODate("2023-06-03T14:43:16.043Z"),
lastHeartbeatRecv: ISODate("2023-06-03T14:43:15.901Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
configVersion: 1,
configTerm: 1
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1685803395, i: 1 }),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: Long("0")
}
},
operationTime: Timestamp({ t: 1685803395, i: 1 })
}
副本集配置遇到问题:
1.MongoServerError: not authorized on admin to execute command
此错误是因为没有授权给admin用户对system.version表执行命令的权限,解决方法如下: > db.grantRolesToUser ( "root", [ { role: "__system", db: "admin" } ] )
2.MongoServerError: replSetInitiate quorum check failed because not all proposed set members responded affirmatively: 192.168.182.144:27017 failed with Authentication failed., 192.168.182.143:27017 failed with Authentication failed
这个错误是因为开了权限校验,导致在初始化的时候验证失败,本次成功就是因为关闭了校验
参考:
MongoDB 6.0.3副本集搭建_ITPUB博客