MongoDB-Compass connect error, why?

I have used the MongoDB 6.0 version image in Docker to create three containers, forming a replica set and mapping ports 30001, 30002, and 30003 respectively. While trying to connect using MongoDB Compass, I found that I can successfully connect using the following connection strings: mongodb://localhost:30001/?replicaSet=vision-set, mongodb://localhost:30002/?replicaSet=vision-set, and mongodb://localhost:30003/?replicaSet=vision-set. However, when I tried to connect using the connection string mongodb://localhost:30001,localhost:30002,localhost:30003/?replicaSet=vision-set, it failed to connect. I seek the help of experts to understand what is causing this issue.

While logged into a DB successfully with compass can you do a rs.status() and rs.status().members command from the shell command window that is on the bottom of compass?

Also can you please provide the error you are getting with the last connection string that has all hosts?

connect ETIMEDOUT 172.30.0.4:27017

rs.status()
{
  set: 'vision-set',
  date: ISODate("2023-05-10T01:46:15.515Z"),
  myState: 2,
  term: Long("1"),
  syncSourceHost: '172.30.0.4:27017',
  syncSourceId: 0,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1683683174, i: 1 }), t: Long("1") },
    lastCommittedWallTime: ISODate("2023-05-10T01:46:14.549Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1683683174, i: 1 }), t: Long("1") },
    appliedOpTime: { ts: Timestamp({ t: 1683683174, i: 1 }), t: Long("1") },
    durableOpTime: { ts: Timestamp({ t: 1683683174, i: 1 }), t: Long("1") },
    lastAppliedWallTime: ISODate("2023-05-10T01:46:14.549Z"),
    lastDurableWallTime: ISODate("2023-05-10T01:46:14.549Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1683683154, i: 1 }),
  electionParticipantMetrics: {
    votedForCandidate: true,
    electionTerm: Long("1"),
    lastVoteDate: ISODate("2023-05-09T15:52:23.287Z"),
    electionCandidateMemberId: 0,
    voteReason: '',
    lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1683647532, i: 1 }), t: Long("-1") },
    maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1683647532, i: 1 }), t: Long("-1") },
    priorityAtElection: 1,
    newTermStartDate: ISODate("2023-05-09T15:52:23.366Z"),
    newTermAppliedDate: ISODate("2023-05-09T15:52:23.976Z")
  },
  members: [
    {
      _id: 0,
      name: '172.30.0.4:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 35642,
      optime: { ts: Timestamp({ t: 1683683174, i: 1 }), t: Long("1") },
      optimeDurable: { ts: Timestamp({ t: 1683683174, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-05-10T01:46:14.000Z"),
      optimeDurableDate: ISODate("2023-05-10T01:46:14.000Z"),
      lastAppliedWallTime: ISODate("2023-05-10T01:46:14.549Z"),
      lastDurableWallTime: ISODate("2023-05-10T01:46:14.549Z"),
      lastHeartbeat: ISODate("2023-05-10T01:46:14.708Z"),
      lastHeartbeatRecv: ISODate("2023-05-10T01:46:13.855Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1683647543, i: 1 }),
      electionDate: ISODate("2023-05-09T15:52:23.000Z"),
      configVersion: 102042,
      configTerm: -1
    },
    {
      _id: 1,
      name: '172.30.0.2:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 35642,
      optime: { ts: Timestamp({ t: 1683683174, i: 1 }), t: Long("1") },
      optimeDurable: { ts: Timestamp({ t: 1683683174, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-05-10T01:46:14.000Z"),
      optimeDurableDate: ISODate("2023-05-10T01:46:14.000Z"),
      lastAppliedWallTime: ISODate("2023-05-10T01:46:14.549Z"),
      lastDurableWallTime: ISODate("2023-05-10T01:46:14.549Z"),
      lastHeartbeat: ISODate("2023-05-10T01:46:14.709Z"),
      lastHeartbeatRecv: ISODate("2023-05-10T01:46:14.754Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '172.30.0.4:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 102042,
      configTerm: -1
    },
    {
      _id: 2,
      name: '172.30.0.3:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 35648,
      optime: { ts: Timestamp({ t: 1683683174, i: 1 }), t: Long("1") },
      optimeDate: ISODate("2023-05-10T01:46:14.000Z"),
      lastAppliedWallTime: ISODate("2023-05-10T01:46:14.549Z"),
      lastDurableWallTime: ISODate("2023-05-10T01:46:14.549Z"),
      syncSourceHost: '172.30.0.4:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 102042,
      configTerm: -1,
      self: true,
      lastHeartbeatMessage: ''
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1683683174, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1683683174, i: 1 })
}

1 Like

I have found and resolved the issue by myself. The problem was caused by using the Docker’s internal network IP addresses for the replica set nodes, while my MongoDB Compass was running on my local machine. When using the connection string mongodb://localhost:30001,localhost:30002,localhost:30003/?replicaSet=vision-set, it returned the internal IP address of the Docker nodes, which is not accessible from my local machine.

To fix the issue, I modified the IP addresses of the replica set nodes to my external IP address during the replica set creation, which allowed me to successfully connect to the replica set from my local MongoDB Compass. I appreciate your response, which gave me some inspiration to solve the problem.

1 Like