I made three dockerized mongo container, one primary and two secodaries. But, I can’t connect to the DBs. I get Server Selection Error either “server selection timed out” or “getaddrinfo ENOTFOUND”.
I’m pretty much sure that replica set is fine, because when I use the connection string like this,
“mongodb://localhost:27001/MeshedDev?directConnection=true&replicaSet=myReplicaSet”
this works. However, when I restart the server and if the primary replica has been changed, this connection string will fail unless I change the port number manually.
So, ideally, I want to list the members so that the server can find the primary by itself. I tried some different formats of connection string according to connection formatting doc of yours and some other posts I found about similar issues, but none of them worked for me.
I feel, I’m missing something with docker, finding the right host address, but cannot figure it out.
Any help with connection string uri please? Below are some logs
- connection strings I’ve tried examples
"mongodb://localhost:27001,localhost:27002,localhost:27003/MeshedDev?replicaSet=myReplicaSet"
"mongodb://127.0.0.1:27001,127.0.0.1:27002,127.0.0.1:27003/MeshedDev?replicaSet=myReplicaSet"
"mongodb://mongo1.127.0.0.1:27001,mongo2.127.0.0.1:27002,mongo3.127.0.0.1:27003/MeshedDev?replicaSet=myReplicaSet"
"mongodb://mongo1:27001,mongo2:27002,mongo3:27003/MeshedDev?replicaSet=myReplicaSet"
"mongodb://mongo1:27017,mongo2:27017,mongo3:27017/MeshedDev?replicaSet=myReplicaSet"
- server error logs
MongooseServerSelectionError: Server selection timed out after 30000 ms
at NativeConnection.Connection.openUri (/Users/me/Meshed/server/node_modules/mongoose/lib/connection.js:825:32)
at /Users/me/Meshed/server/node_modules/mongoose/lib/index.js:409:10
at /Users/me/Meshed/server/node_modules/mongoose/lib/helpers/promiseOrCallback.js:41:5
at new Promise (<anonymous>)
at promiseOrCallback (/Users/me/Meshed/server/node_modules/mongoose/lib/helpers/promiseOrCallback.js:40:10)
at Mongoose._promiseOrCallback (/Users/me/Meshed/server/node_modules/mongoose/lib/index.js:1262:10)
at Mongoose.connect (/Users/me/Meshed/server/node_modules/mongoose/lib/index.js:408:20)
at mongoConnect (/Users/me/Meshed/server/src/database/mongo/connection.ts:9:12)
at Object.<anonymous> (/Users/me/Meshed/server/src/app.ts:35:13)
at Module._compile (node:internal/modules/cjs/loader:1218:14) {
reason: TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(3) {
'mongo1:27017' => [ServerDescription],
'mongo2:27017' => [ServerDescription],
'mongo3:27017' => [ServerDescription]
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'myReplicaSet',
maxElectionId: ObjectId { [Symbol(id)]: [Buffer [Uint8Array]] },
maxSetVersion: 1,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
},
code: undefined
}
- replica status
{
set: 'myReplicaSet',
date: ISODate("2023-02-15T12:39:21.928Z"),
myState: 1,
term: Long("7"),
syncSourceHost: '',
syncSourceId: -1,
heartbeatIntervalMillis: Long("2000"),
majorityVoteCount: 2,
writeMajorityCount: 2,
votingMembersCount: 3,
writableVotingMembersCount: 3,
optimes: {
lastCommittedOpTime: { ts: Timestamp({ t: 1676464753, i: 1 }), t: Long("7") },
lastCommittedWallTime: ISODate("2023-02-15T12:39:13.254Z"),
readConcernMajorityOpTime: { ts: Timestamp({ t: 1676464753, i: 1 }), t: Long("7") },
appliedOpTime: { ts: Timestamp({ t: 1676464753, i: 1 }), t: Long("7") },
durableOpTime: { ts: Timestamp({ t: 1676464753, i: 1 }), t: Long("7") },
lastAppliedWallTime: ISODate("2023-02-15T12:39:13.254Z"),
lastDurableWallTime: ISODate("2023-02-15T12:39:13.254Z")
},
lastStableRecoveryTimestamp: Timestamp({ t: 1676464723, i: 1 }),
electionCandidateMetrics: {
lastElectionReason: 'electionTimeout',
lastElectionDate: ISODate("2023-02-15T11:54:12.530Z"),
electionTerm: Long("7"),
lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1676461982, i: 1 }), t: Long("6") },
numVotesNeeded: 2,
priorityAtElection: 1,
electionTimeoutMillis: Long("10000"),
numCatchUpOps: Long("0"),
newTermStartDate: ISODate("2023-02-15T11:54:12.584Z"),
wMajorityWriteAvailabilityDate: ISODate("2023-02-15T11:54:12.999Z")
},
members: [
{
_id: 0,
name: 'mongo1:27017',
health: 1,
state: 1,
stateStr: 'PRIMARY',
uptime: 2732,
optime: { ts: Timestamp({ t: 1676464753, i: 1 }), t: Long("7") },
optimeDate: ISODate("2023-02-15T12:39:13.000Z"),
lastAppliedWallTime: ISODate("2023-02-15T12:39:13.254Z"),
lastDurableWallTime: ISODate("2023-02-15T12:39:13.254Z"),
syncSourceHost: '',
syncSourceId: -1,
infoMessage: '',
electionTime: Timestamp({ t: 1676462052, i: 1 }),
electionDate: ISODate("2023-02-15T11:54:12.000Z"),
configVersion: 1,
configTerm: 7,
self: true,
lastHeartbeatMessage: ''
},
{
_id: 1,
name: 'mongo2:27017',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 2719,
optime: { ts: Timestamp({ t: 1676464753, i: 1 }), t: Long("7") },
optimeDurable: { ts: Timestamp({ t: 1676464753, i: 1 }), t: Long("7") },
optimeDate: ISODate("2023-02-15T12:39:13.000Z"),
optimeDurableDate: ISODate("2023-02-15T12:39:13.000Z"),
lastAppliedWallTime: ISODate("2023-02-15T12:39:13.254Z"),
lastDurableWallTime: ISODate("2023-02-15T12:39:13.254Z"),
lastHeartbeat: ISODate("2023-02-15T12:39:21.153Z"),
lastHeartbeatRecv: ISODate("2023-02-15T12:39:21.828Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: 'mongo1:27017',
syncSourceId: 0,
infoMessage: '',
configVersion: 1,
configTerm: 7
},
{
_id: 2,
name: 'mongo3:27017',
health: 1,
state: 2,
stateStr: 'SECONDARY',
uptime: 2707,
optime: { ts: Timestamp({ t: 1676464753, i: 1 }), t: Long("7") },
optimeDurable: { ts: Timestamp({ t: 1676464753, i: 1 }), t: Long("7") },
optimeDate: ISODate("2023-02-15T12:39:13.000Z"),
optimeDurableDate: ISODate("2023-02-15T12:39:13.000Z"),
lastAppliedWallTime: ISODate("2023-02-15T12:39:13.254Z"),
lastDurableWallTime: ISODate("2023-02-15T12:39:13.254Z"),
lastHeartbeat: ISODate("2023-02-15T12:39:21.153Z"),
lastHeartbeatRecv: ISODate("2023-02-15T12:39:21.342Z"),
pingMs: Long("0"),
lastHeartbeatMessage: '',
syncSourceHost: 'mongo2:27017',
syncSourceId: 1,
infoMessage: '',
configVersion: 1,
configTerm: 7
}
],
ok: 1,
'$clusterTime': {
clusterTime: Timestamp({ t: 1676464753, i: 1 }),
signature: {
hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
keyId: Long("0")
}
},
operationTime: Timestamp({ t: 1676464753, i: 1 })
}
- docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
569f32314512 mongo:latest "docker-entrypoint.s…" 24 minutes ago Up 24 minutes 0.0.0.0:27003->27017/tcp mongo3
1efd0830a5c5 mongo:latest "docker-entrypoint.s…" 24 minutes ago Up 24 minutes 0.0.0.0:27002->27017/tcp mongo2
2f4ef16a31b9 mongo:latest "docker-entrypoint.s…" 24 minutes ago Up 24 minutes 0.0.0.0:27001->27017/tcp mongo1
- mongo1 docker terminal log (for a single connection attempt)
2023-02-15 21:00:33
{
"t": {
"$date" : "2023-02-15T12:00:33.824+00:00"
},
"s":"I",
"c":"NETWORK",
"id":22943,
"ctx":"listener",
"msg":"Connection accepted",
"attr": {
"remote":"172.19.0.1:48010",
"uuid":"c54cf73c-b5c6-480b-b6e0-1cc2caaa1298",
"connectionId":29,
"connectionCount":13
}
}
2023-02-15 21:00:33
{
"t": {
"$date":"2023-02-15T12:00:33.834+00:00"
},
"s":"I",
"c":"NETWORK",
"id":51800,
"ctx":"conn29",
"msg":"client metadata",
"attr": {
"remote":"172.19.0.1:48010",
"client":"conn29",
"doc":{
"driver":{
"name":"nodejs|Mongoose",
"version":"4.12.1"
},
"os": {
"type":"Darwin",
"name":"darwin",
"architecture":"arm64",
"version":"22.1.0"
},
"platform":"Node.js v18.13.0, LE (unified)",
"version":"4.12.1|6.8.3"
}
}
}
2023-02-15 21:00:33
{
"t": {
"$date":"2023-02-15T12:00:33.842+00:00"
},
"s":"I",
"c":"NETWORK",
"id":22944,
"ctx":"conn29",
"msg":"Connection ended",
"attr":{
"remote":"172.19.0.1:48010",
"uuid":"c54cf73c-b5c6-480b-b6e0-1cc2caaa1298",
"connectionId":29,
"connectionCount":12
}
}