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
CodePudding user response:
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.