Cannot connect via Compass or any other client when replicaSet is active

Hi,
I am working on an application that needs a mongoDB with replicaSet. I would like to recreate a single-node cluster with replicaSet locally with docker compose, also adding mongo-express to more easily access data

I can connect to mongoDB via mongo-express and compass until I activate replicationSet. As soon as I enter the mongo machine and run the rs.initiate(…) command then mongo-express continues to work, but compass refuses me the connection with error
connect ECONNREFUSED 127.0.0.1:27017

I would like to launch

  • mongoDB on port 37017
  • mongo-express on port 8082

Below is my docker-compose.yml

services:
  mongo-replica-1:
    image: mongo:latest
    container_name: orb-replica-mongodb-1
    ports:
      - 37017:27017
    networks:
      - MONGO
    volumes:
      - ./mongodb:/data/db
      - ./configdb:/data/configdb
    command: ["mongod", "--replSet", "rs0", "--bind_ip_all", "--port", "27017"] 

  mongo-express:
    image: mongo-express:latest
    container_name: orb-replica-mongo-express
    environment:
      - ME_CONFIG_MONGODB_URL=mongodb://mongo-replica-1:27017/?authSource=admin
    ports:
      - 8082:8081
    networks:
      - MONGO
    depends_on:
      - mongo-replica-1

networks:
  MONGO:
    name: MONGO

This is the rs.status()

rs0 [direct: primary] test> rs.status()
{
  set: 'rs0',
  date: ISODate('2024-07-03T08:54:02.738Z'),
  myState: 1,
  term: Long('2'),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long('2000'),
  majorityVoteCount: 1,
  writeMajorityCount: 1,
  votingMembersCount: 1,
  writableVotingMembersCount: 1,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
    lastCommittedWallTime: ISODate('2024-07-03T08:53:59.332Z'),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
    appliedOpTime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
    durableOpTime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
    lastAppliedWallTime: ISODate('2024-07-03T08:53:59.332Z'),
    lastDurableWallTime: ISODate('2024-07-03T08:53:59.332Z')
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1719996789, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate('2024-07-03T07:46:07.709Z'),
    electionTerm: Long('2'),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long('-1') },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1719992763, i: 23 }), t: Long('1') },
    numVotesNeeded: 1,
    priorityAtElection: 1,
    electionTimeoutMillis: Long('10000'),
    newTermStartDate: ISODate('2024-07-03T07:46:07.712Z'),
    wMajorityWriteAvailabilityDate: ISODate('2024-07-03T07:46:07.714Z')
  },
  members: [
    {
      _id: 0,
      name: 'mongo-replica-1:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 4076,
      optime: { ts: Timestamp({ t: 1719996839, i: 1 }), t: Long('2') },
      optimeDate: ISODate('2024-07-03T08:53:59.000Z'),
      lastAppliedWallTime: ISODate('2024-07-03T08:53:59.332Z'),
      lastDurableWallTime: ISODate('2024-07-03T08:53:59.332Z'),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1719992767, i: 1 }),
      electionDate: ISODate('2024-07-03T07:46:07.000Z'),
      configVersion: 1,
      configTerm: 2,
      self: true,
      lastHeartbeatMessage: ''
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1719996839, i: 1 }),
    signature: {
      hash: Binary.createFromBase64('AAAAAAAAAAAAAAAAAAAAAAAAAAA=', 0),
      keyId: Long('0')
    }
  },
  operationTime: Timestamp({ t: 1719996839, i: 1 })
}

Any idea? I also added in hosts file the following rule
127.0.0.1 mongo-replica-1

Thanks

Daniele

Hi @Daniele_Leoni. A couple of considerations:

  1. when you configure a replica set within docker that way, when the driver (Compass uses node.js) connects, it will try to discover the nodes of the replica set. MongoDB will return 127.0.0.1:27017, and that is what Compass will try to connect to. You can avoid this problem by adding ?directConnection=true to the connection string in Compass, or in the advanced connection options select Direct Connection. That should work.
  2. Instead of initializing the replica set manually in your compose file, you could consider using the mongodb/mongodb-atlas-local image documented here. That will give you a 1-node replica set out of the box, and includes full-text search and vector search capabilities with Atlas Search and Atlas Vector Search.
1 Like

thanks. This is the solution. I give a try to mongodb/mongodb-atlas-local, so I can avoid a lot of configuration. thank you very much