MongoDB6.0.6 副本集搭建(CentOs8)

本文只说如何操作配置副本集,历程艰难,官网文档看了半天也只说了怎么添加单个,没有给出来一个完整的流程。

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.安装新节点,和之前的安装相同,配置文件也要相同,必须设置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, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1685845557, i: 1 })
}
rs1 [direct: primary] test> 

rs.status() 查看状态

{
      _id: 3,
      name: '192.168.182.145:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 121,
      optime: { ts: Timestamp({ t: 1685845677, i: 1 }), t: Long("4") },
      optimeDurable: { ts: Timestamp({ t: 1685845677, i: 1 }), t: Long("4") },
      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"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '192.168.182.143:27017',
      syncSourceId: 1,
      infoMessage: '',
      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

这个错误是因为开了权限校验,导致在初始化的时候验证失败,本次成功就是因为关闭了校验

参考:

MongoDB 6.0.3副本集搭建_ITPUB博客

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值