1

I am attempting to run a MongoDB cluster locally to test transactions.

I've leveraged the Bitnami docker-compose file

version: '2'
services:
  mongodb-primary:
    image: 'bitnami/mongodb:latest'
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-primary
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
    ports:
      - 27017:27017

    volumes:
      - 'mongodb_master_data:/bitnami'

  mongodb-secondary:
    image: 'bitnami/mongodb:latest'
    depends_on:
      - mongodb-primary
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-secondary
      - MONGODB_REPLICA_SET_MODE=secondary
      - MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary
      - MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017
      - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
    ports:
      - 27027:27017

  mongodb-arbiter:
    image: 'bitnami/mongodb:latest'
    depends_on:
      - mongodb-primary
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=mongodb-arbiter
      - MONGODB_REPLICA_SET_MODE=arbiter
      - MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary
      - MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017
      - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_KEY=replicasetkey123
    ports:
      - 27037:27017

volumes:
  mongodb_master_data:
    driver: local

The cluster successfully runs and I'm able to run rs.status() and rs.config()

rs.config():

{
  _id: 'replicaset',
  version: 5,
  term: 2,
  members: [
    {
      _id: 0,
      host: 'mongodb-primary:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 5,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    },
    {
      _id: 1,
      host: 'mongodb-arbiter:27017',
      arbiterOnly: true,
      buildIndexes: true,
      hidden: false,
      priority: 0,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    },
    {
      _id: 2,
      host: 'mongodb-secondary:27017',
      arbiterOnly: false,
      buildIndexes: true,
      hidden: false,
      priority: 1,
      tags: {},
      secondaryDelaySecs: Long("0"),
      votes: 1
    }
  ],
  protocolVersion: Long("1"),
  writeConcernMajorityJournalDefault: true,
  settings: {
    chainingAllowed: true,
    heartbeatIntervalMillis: 2000,
    heartbeatTimeoutSecs: 10,
    electionTimeoutMillis: 10000,
    catchUpTimeoutMillis: -1,
    catchUpTakeoverDelayMillis: 30000,
    getLastErrorModes: {},
    getLastErrorDefaults: { w: 1, wtimeout: 0 },
    replicaSetId: ObjectId("636ad53c134a3f3884836da1")
  }
}

rs.status():

{
  set: 'replicaset',
  date: ISODate("2022-11-08T22:58:23.847Z"),
  myState: 1,
  term: Long("2"),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 2,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1667948302, i: 1 }), t: Long("2") },
    lastCommittedWallTime: ISODate("2022-11-08T22:58:22.005Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1667948302, i: 1 }), t: Long("2") },
    appliedOpTime: { ts: Timestamp({ t: 1667948302, i: 1 }), t: Long("2") },
    durableOpTime: { ts: Timestamp({ t: 1667948302, i: 1 }), t: Long("2") },
    lastAppliedWallTime: ISODate("2022-11-08T22:58:22.005Z"),
    lastDurableWallTime: ISODate("2022-11-08T22:58:22.005Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1667948242, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate("2022-11-08T22:16:31.521Z"),
    electionTerm: Long("2"),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1667945788, i: 17 }), t: Long("1") },
    numVotesNeeded: 1,
    priorityAtElection: 5,
    electionTimeoutMillis: Long("10000"),
    newTermStartDate: ISODate("2022-11-08T22:16:31.531Z"),
    wMajorityWriteAvailabilityDate: ISODate("2022-11-08T22:16:31.540Z")
  },
  members: [
    {
      _id: 0,
      name: 'mongodb-primary:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 2513,
      optime: { ts: Timestamp({ t: 1667948302, i: 1 }), t: Long("2") },
      optimeDate: ISODate("2022-11-08T22:58:22.000Z"),
      lastAppliedWallTime: ISODate("2022-11-08T22:58:22.005Z"),
      lastDurableWallTime: ISODate("2022-11-08T22:58:22.005Z"),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1667945791, i: 1 }),
      electionDate: ISODate("2022-11-08T22:16:31.000Z"),
      configVersion: 5,
      configTerm: 2,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'mongodb-arbiter:27017',
      health: 1,
      state: 7,
      stateStr: 'ARBITER',
      uptime: 2493,
      lastHeartbeat: ISODate("2022-11-08T22:58:22.069Z"),
      lastHeartbeatRecv: ISODate("2022-11-08T22:58:22.068Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      configVersion: 5,
      configTerm: 2
    },
    {
      _id: 2,
      name: 'mongodb-secondary:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 2454,
      optime: { ts: Timestamp({ t: 1667948302, i: 1 }), t: Long("2") },
      optimeDurable: { ts: Timestamp({ t: 1667948302, i: 1 }), t: Long("2") },
      optimeDate: ISODate("2022-11-08T22:58:22.000Z"),
      optimeDurableDate: ISODate("2022-11-08T22:58:22.000Z"),
      lastAppliedWallTime: ISODate("2022-11-08T22:58:22.005Z"),
      lastDurableWallTime: ISODate("2022-11-08T22:58:22.005Z"),
      lastHeartbeat: ISODate("2022-11-08T22:58:22.069Z"),
      lastHeartbeatRecv: ISODate("2022-11-08T22:58:22.069Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'mongodb-primary:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 5,
      configTerm: 2
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1667948302, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("7c40430db9f17606a984ed8d4e9359e1141366f3", "hex"), 0),
      keyId: Long("7163772610960949254")
    }
  },
  operationTime: Timestamp({ t: 1667948302, i: 1 })
}

I'm able to connect to the nodes individually using

mongodb://root:password123@localhost:27017/?authMechanism=DEFAULT

but I get a time out when attempting to connect with replicaset

Can somebody please help me understand what I'm missing?

Edit: adding details on how I connect

Two ways:

Using pymongo

MongoClient("mongodb://root:password123@localhost:27017/", replica_set="replicasetkey123")

Using Compass

mongodb://root:password123@localhost:27017,localhost:27027,localhost:27037/?authMechanism=DEFAULT&replicaSet=replicasetkey123
  • How are you tryin to [connect to the replicaSet](https://pymongo.readthedocs.io/en/stable/examples/high_availability.html)? – rickhg12hs Nov 08 '22 at 23:12
  • Two ways: Using pymongo: `MongoClient("mongodb://root:password123@localhost:27017/", replica_set="replicasetkey123")` Using Compass `mongodb://root:password123@localhost:27017,localhost:27027,localhost:27037/?authMechanism=DEFAULT&replicaSet=replicasetkey123` – Carnegie118 Nov 08 '22 at 23:20
  • There are some related questions/answers in [bitnami's github repo issues](https://github.com/bitnami/bitnami-docker-mongodb/issues/282). – rickhg12hs Nov 09 '22 at 00:09
  • Maybe this one? https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Nov 09 '22 at 09:16
  • Still no luck. I've tried adding to /etc/hosts and it isn't working. – Carnegie118 Nov 09 '22 at 18:08

3 Answers3

1

Edit your hosts file to add:

127.0.0.1     mongodb-primary
127.0.0.1     mongodb-secondary
127.0.0.1     mongodb-arbiter

Then connect with:

mongo "mongodb://root:password123@mongodb-primary:27017,mongodb-secondary:27027,mongodb-arbiter:27037/admin?replicaSet=replicaset"

Note your replicaset name is replicaset (not replicasetkey123); this is the default unless you set MONGODB_REPLICA_SET_NAME.

Also note that you can happily set up a replicaset with just one member for this type of testing.

Belly Buster
  • 8,224
  • 2
  • 7
  • 20
  • Thank you! I changed the /etc/hosts earlier today but it didn't work. Spotting the replicaset vs replicasetkey123 was the trick. – Carnegie118 Nov 09 '22 at 22:42
0

In case this is still a problem for you, I did figure out how to make this work without having to modify the host machines host file.

Simply change the advertised name to "localhost":

MONGODB_ADVERTISED_HOSTNAME: localhost

I only have tried this with a single node replica set, but I can't imagine it wouldnt work for more than one node.

Joe Jankowiak
  • 1,059
  • 12
  • 37
0

As Joe said, setup with localhost works. Mine setup

version: "3"

services:
  mongodb-primary:
    image: "bitnami/mongodb:latest"
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=localhost
      - MONGODB_REPLICA_SET_MODE=primary
      - MONGODB_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_KEY=replicaset
    ports:
      - 27017:27017

    volumes:
      - "./data:/bitnami"

  mongodb-arbiter:
    image: "bitnami/mongodb:latest"
    depends_on:
      - mongodb-primary
    environment:
      - MONGODB_ADVERTISED_HOSTNAME=loc
      - MONGODB_REPLICA_SET_MODE=arbiter
      - MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary
      - MONGODB_INITIAL_PRIMARY_PORT_NUMBER=27017
      - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password123
      - MONGODB_REPLICA_SET_KEY=replicaset
    ports:
      - 27037:27017

URI for connection to Atlas worked mongodb://root:password123@localhost:27017,localhost:27037/admin?replicaSet=replicaset, but in case of app, you need to specify database and also authSource mongodb://root:password123@localhost:27017,localhost:27037/<database>?authSource=admin&replicaSet=replicaset

Adam
  • 21
  • 1
  • 3