MongoServerSelectionError: connection timed out

Hello!

my Mongo connection seems to time out everyday, after about 7-10 hours of my app running with the error:

MongoServerSelectionError: connection timed out
    at Timeout._onTimeout (/opt/discord/node_modules/mongodb/lib/sdam/topology.js:292:38)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      '***.mongodb.net:27017' => [ServerDescription],
      '***.mongodb.net:27017' => [ServerDescription],
      '***.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-gxcew9-shard-0',
    maxElectionId: new ObjectId("7fffffff0000000000000081"),
    maxSetVersion: 7,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

I connect doing:

const url = `mongodb+srv://${environment.mongodb.username}:${environment.mongodb.password}@${environment.mongodb.host}/app_logs?retryWrites=true&keepAlive=true`;
this.client = new MongoClient(url, {
      loggerLevel: LoggerLevel.ERROR,
 });

await this.client.connect();
await this.client.db('app_logs').command({ ping: 1 });

My app runs in a Docker container. I’ve read something about whitelisting the IPs but if I could have some instructions on how to accomplish this with Docker, that’d be very much appreciated.

I’m running Node v16.6.1, Docker 3.8 and Mongo package v4.13.0

Thank you

3 Likes

Same here, did you find out why this happens? it happens to me each 5-7 hours

Hey @Alberto_Oteiza,

Welcome to the MongoDB Community forums :sparkles:

I would recommend creating a new topic with all the deployment details and the specific issue you are facing. This will make it more likely for the community to interact and assist you better.

I also recommend reading Getting Started with the MongoDB Community: README.1ST for some tips to help improve your community outcomes.

Regards,
Kushagra Kesav