我无法在 mongodb compass 中连接副本集数据库?只有在使用直接连接时才有效

问题描述 投票:0回答:1

我正在尝试将我的副本集与 MongoDB 指南针连接,因为我的应用程序字符串不起作用..

我有 3 个 mongodb 实例

mongoSet [direct: primary] test> rs.status()
{
  set: 'mongoSet',
  date: ISODate("2023-04-01T05:18:40.508Z"),
  myState: 1,
  term: Long("2"),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1680326311, i: 1 }), t: Long("2") },
    lastCommittedWallTime: ISODate("2023-04-01T05:18:31.301Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1680326311, i: 1 }), t: Long("2") },
    appliedOpTime: { ts: Timestamp({ t: 1680326311, i: 1 }), t: Long("2") },
    durableOpTime: { ts: Timestamp({ t: 1680326311, i: 1 }), t: Long("2") },
    lastAppliedWallTime: ISODate("2023-04-01T05:18:31.301Z"),
    lastDurableWallTime: ISODate("2023-04-01T05:18:31.301Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1680326311, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate("2023-04-01T04:06:38.199Z"),
    electionTerm: Long("2"),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1680266171, i: 1 }), t: Long("1") },
    numVotesNeeded: 2,
    priorityAtElection: 1,
    electionTimeoutMillis: Long("10000"),
    numCatchUpOps: Long("0"),
    newTermStartDate: ISODate("2023-04-01T04:06:38.211Z"),
    wMajorityWriteAvailabilityDate: ISODate("2023-04-01T04:06:38.284Z")
  },
  members: [
    {
      _id: 0,
      name: 'mongo1:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 4332,
      optime: { ts: Timestamp({ t: 1680326311, i: 1 }), t: Long("2") },
      optimeDurable: { ts: Timestamp({ t: 1680326311, i: 1 }), t: Long("2") },
      optimeDate: ISODate("2023-04-01T05:18:31.000Z"),
      optimeDurableDate: ISODate("2023-04-01T05:18:31.000Z"),
      lastAppliedWallTime: ISODate("2023-04-01T05:18:31.301Z"),
      lastDurableWallTime: ISODate("2023-04-01T05:18:31.301Z"),
      lastHeartbeat: ISODate("2023-04-01T05:18:38.983Z"),
      lastHeartbeatRecv: ISODate("2023-04-01T05:18:39.159Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongo2:27017',
      syncSourceId: 1,
      infoMessage: '',
      configVersion: 1,
      configTerm: 2
    },
    {
      _id: 1,
      name: 'mongo2:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 4335,
      optime: { ts: Timestamp({ t: 1680326311, i: 1 }), t: Long("2") },
      optimeDate: ISODate("2023-04-01T05:18:31.000Z"),
      lastAppliedWallTime: ISODate("2023-04-01T05:18:31.301Z"),
      lastDurableWallTime: ISODate("2023-04-01T05:18:31.301Z"),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1680321998, i: 1 }),
      electionDate: ISODate("2023-04-01T04:06:38.000Z"),
      configVersion: 1,
      configTerm: 2,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 2,
      name: 'mongo3:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 4333,
      optime: { ts: Timestamp({ t: 1680326311, i: 1 }), t: Long("2") },
      optimeDurable: { ts: Timestamp({ t: 1680326311, i: 1 }), t: Long("2") },
      optimeDate: ISODate("2023-04-01T05:18:31.000Z"),
      optimeDurableDate: ISODate("2023-04-01T05:18:31.000Z"),
      lastAppliedWallTime: ISODate("2023-04-01T05:18:31.301Z"),
      lastDurableWallTime: ISODate("2023-04-01T05:18:31.301Z"),
      lastHeartbeat: ISODate("2023-04-01T05:18:38.983Z"),
      lastHeartbeatRecv: ISODate("2023-04-01T05:18:39.026Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongo2:27017',
      syncSourceId: 1,
      infoMessage: '',
      configVersion: 1,
      configTerm: 2
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1680326311, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1680326311, i: 1 })
}

我可以通过studio 3T连接

我可以在环境变量中使用 mongodb://localhost:30001/?directConnection=true 来连接应用程序吗?直接连接安全吗?

这里的问题是我想查看所有 3 个节点..但是直接连接只允许一个主机..如何解决这个问题?

mongodb docker
1个回答
0
投票

您应该使用此连接字符串进行连接:

mongodb://mongo1:27017,mongo2:27017,mongo3:27017/?replicaSet=mongoSet

连接字符串 URI 格式

© www.soinside.com 2019 - 2024. All rights reserved.