本文只说如何操作配置副本集,历程艰难,官网文档看了半天也只说了怎么添加单个,没有给出来一个完整的流程。
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,appliedOpTime: { ts: Timestamp({ t: 1685803395,durableOpTime: { ts: Timestamp({ t: 1685803395,lastAppliedWallTime: ISODate("2023-06-03T14:43:15.072Z"),lastDurableWallTime: ISODate("2023-06-03T14:43:15.072Z")
},lastStableRecoveryTimestamp: Timestamp({ t: 1685803355,electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',lastElectionDate: ISODate("2023-06-03T14:16:54.917Z"),electionTerm: Long("1"),lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1685801804,t: Long("-1") },lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1685801804,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,optimeDate: ISODate("2023-06-03T14:43:15.000Z"),lastDurableWallTime: ISODate("2023-06-03T14:43:15.072Z"),infoMessage: '',electionTime: Timestamp({ t: 1685801814,electionDate: ISODate("2023-06-03T14:16:54.000Z"),configVersion: 1,configTerm: 1,self: true,lastHeartbeatMessage: ''
},{
_id: 1,name: '192.168.182.143:27017',state: 2,stateStr: 'SECONDARY',uptime: 1593,optimeDurable: { ts: Timestamp({ t: 1685803395,optimeDurableDate: ISODate("2023-06-03T14:43:15.000Z"),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,configTerm: 1
},{
_id: 2,name: '192.168.182.144:27017',state: 7,stateStr: 'ARBITER',lastHeartbeat: ISODate("2023-06-03T14:43:16.043Z"),lastHeartbeatRecv: ISODate("2023-06-03T14:43:15.901Z"),configTerm: 1
}
],ok: 1,'$clusterTime': {
clusterTime: Timestamp({ t: 1685803395,signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000","hex"),0),keyId: Long("0")
}
},operationTime: Timestamp({ t: 1685803395,i: 1 })
}
添加新节点
1.安装新节点,和之前的安装相同,配置文件也要相同,必须设置replSet=rs1,才能添加
登录主节点添加节点
rs.add("192.168.182.145:27017")
rs1 [direct: primary] test> rs.add("192.168.182.145:27017")
{
ok: 1,'$clusterTime': {
clusterTime: Timestamp({ t: 1685845557,operationTime: Timestamp({ t: 1685845557,i: 1 })
}
rs1 [direct: primary] test>
rs.status() 查看状态
{
_id: 3,name: '192.168.182.145:27017',uptime: 121,optime: { ts: Timestamp({ t: 1685845677,t: Long("4") },optimeDurable: { ts: Timestamp({ t: 1685845677,optimeDate: ISODate("2023-06-04T02:27:57.000Z"),optimeDurableDate: ISODate("2023-06-04T02:27:57.000Z"),lastAppliedWallTime: ISODate("2023-06-04T02:27:57.408Z"),lastDurableWallTime: ISODate("2023-06-04T02:27:57.408Z"),lastHeartbeat: ISODate("2023-06-04T02:27:57.814Z"),lastHeartbeatRecv: ISODate("2023-06-04T02:27:57.810Z"),syncSourceHost: '192.168.182.143:27017',syncSourceId: 1,configVersion: 4,configTerm: 4
},
主节点创建的集合也自动同步过来了
副本集配置遇到问题:
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
这个错误是因为开了权限校验,导致在初始化的时候验证失败,本次成功就是因为关闭了校验
参考:
原文地址:https://blog.csdn.net/jingyu333/article/details/131015460
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。