mongodb 主从配置

#进入主节点
mongosh 主机名(ip):port
rs.initiate() 进入节点配置
回车一直到现在primary 模式
rs0 [direct: primary] test>

rs.status() 查看主节点的信息
有一个members属性 显示的是主节点有哪些节点

{
  set: 'rs0',
  date: ISODate("2023-05-31T06:19:01.043Z"),
  myState: 2,
  term: Long("1"),
  syncSourceHost: 'zyj:28017',
  syncSourceId: 0,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
    lastCommittedWallTime: ISODate("2023-05-31T06:18:59.097Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
    appliedOpTime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
    durableOpTime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
    lastAppliedWallTime: ISODate("2023-05-31T06:18:59.097Z"),
    lastDurableWallTime: ISODate("2023-05-31T06:18:59.097Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1685513919, i: 1 }),
  members: [
    {
      _id: 0,
      name: 'zyj:28017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 9333,
      optime: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
      optimeDurable: { ts: Timestamp({ t: 1685513939, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-05-31T06:18:59.000Z"),
      optimeDurableDate: ISODate("2023-05-31T06:18:59.000Z"),
      lastAppliedWallTime: ISODate("2023-05-31T06:18:59.097Z"),
      lastDurableWallTime: ISODate("2023-05-31T06:18:59.097Z"),
      lastHeartbeat: ISODate("2023-05-31T06:19:00.716Z"),
      lastHeartbeatRecv: ISODate("2023-05-31T06:19:00.718Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1685503988, i: 2 }),
      electionDate: ISODate("2023-05-31T03:33:08.000Z"),
      configVersion: 5,
      configTerm: 1
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1685513939, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1685513939, i: 1 })
}
rs.add("主机名:port")

![[Pasted image 20230531114343.png]]

##现在主节点的成员节点变成了2个


members: [
    {
      _id: 0,
      name: 'zyj:28017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 1329,
      optime: { ts: Timestamp({ t: 1685504628, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-05-31T03:43:48.000Z"),
      lastAppliedWallTime: ISODate("2023-05-31T03:43:48.781Z"),
      lastDurableWallTime: ISODate("2023-05-31T03:43:48.781Z"),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1685503988, i: 2 }),
      electionDate: ISODate("2023-05-31T03:33:08.000Z"),
      configVersion: 3,
      configTerm: 1,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'zyj:28018',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 24,
      optime: { ts: Timestamp({ t: 1685504628, i: 1 }), t: Long("1") },
      optimeDurable: { ts: Timestamp({ t: 1685504628, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-05-31T03:43:48.000Z"),
      optimeDurableDate: ISODate("2023-05-31T03:43:48.000Z"),
      lastAppliedWallTime: ISODate("2023-05-31T03:43:48.781Z"),
      lastDurableWallTime: ISODate("2023-05-31T03:43:48.781Z"),
      lastHeartbeat: ISODate("2023-05-31T03:43:51.166Z"),
      lastHeartbeatRecv: ISODate("2023-05-31T03:43:50.167Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'zyj:28017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 3,
      configTerm: 1
    }
  ],

验证

# 第一次进入从节点会报错
rs0 [direct: secondary] test> db.test.find()
MongoServerError: not primary and secondaryOk=false - consider using db.getMongo().setReadPref() or readPreference in the connection string

# 需要设置从节点允许读操作
db.getMongo().setReadPref("secondary")
# 现在查询就不会报错了
rs0 [direct: secondary] test> db.fruit.findOne()
{ _id: ObjectId("6476c59c7776dcfd03e5b6d0"), name: 'apple' }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值