Unable to connect MongoDB ("Could not connect to any servers in your MongoDB Atlas cluster")

Since today morning, my application stopped connecting to mongodb. its getting timeout.

48ed670c0dddfa:/app# node <<‘EOF’

> const mongoose = require(‘mongoose’);

>

> const uri = process.env.MONGODB_URI;

>

> console.log(‘[Test] MONGODB_URI is’, uri ? ‘SET’ : ‘NOT SET’);

> if (!uri) {

> console.error(‘[Test] No MONGODB_URI found in env’);

> process.exit(1);

> }

>

> mongoose.set(‘bufferCommands’, false);

>

> mongoose.connect(uri, {

> maxPoolSize: 10,

> socketTimeoutMS: 45000,

> serverSelectionTimeoutMS: 30000,

> family: 4, // :key: force IPv4

> })

> .then(() => {

> console.log(‘[Test] :white_check_mark: MongoDB connected successfully with mongoose + family=4’);

> return mongoose.disconnect();

> })

> .then(() => {

> console.log(‘[Test] :electric_plug: Disconnected cleanly’);

> process.exit(0);

> })

> .catch((err) => {

> console.error(‘[Test] :cross_mark: MongoDB connection failed’);

> console.error(err);

> process.exit(1);

> });

> EOF

[Test] MONGODB_URI is SET

[Test] :cross_mark: MongoDB connection failed

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the database from an IP that isn’t whitelisted. Make sure your current IP address is on your Atlas cluster’s IP whitelist: Configure IP Access List Entries - Atlas - MongoDB Docs

at \_handleConnectionErrors (/app/node_modules/mongoose/lib/connection.js:897:11)

at NativeConnection.openUri (/app/node_modules/mongoose/lib/connection.js:848:11)

at runNextTicks (node:internal/process/task_queues:60:5)

at listOnTimeout (node:internal/timers:545:9)

at process.processTimers (node:internal/timers:519:7) {

reason: TopologyDescription {

type: 'ReplicaSetNoPrimary',

servers: Map(3) {

  'ac-vss5jy1-shard-00-01.e2frgbq.mongodb.net:27017' => \[ServerDescription\],

  'ac-vss5jy1-shard-00-02.e2frgbq.mongodb.net:27017' => \[ServerDescription\],

  'ac-vss5jy1-shard-00-00.e2frgbq.mongodb.net:27017' => \[ServerDescription\]

},

stale: false,

compatible: true,

heartbeatFrequencyMS: 10000,

localThresholdMS: 15,

setName: 'atlas-kiio9q-shard-0',

maxElectionId: null,

maxSetVersion: null,

commonWireVersion: 0,

logicalSessionTimeoutMinutes: null

},

code: undefined

}

Hi… Generally, you would need a static egress IP for such external services. Try checking fly machine egress-ip list for each of your Machines and verify that those addresses are in fact all on the whitelist.


Aside: You can use the </> button in the forum editor’s toolbar to get an area suitable for pasting code, logs, etc.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.