question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MongoError: connection 3 to cluster0.mongodb.net:27017 closed

See original GitHub issue

I’m currently running MongoDB 3.4, NodeJS v6.10.3 and Mongoose 4.9.5.

When trying to connect to a cluster hosted through MongoDB Atlas i’m getting the following error:

1|streamdo | MongoError: connection 5 to cluster0.mongodb.net:27017 closed
1|streamdo |     at Function.MongoError.create (/opt/AppName/node_modules/mongodb-core/lib/error.js:29:11)
1|streamdo |     at Socket.<anonymous> (/opt/AppName/node_modules/mongodb-core/lib/connection/connection.js:202:22)
1|streamdo |     at Socket.g (events.js:292:16)
1|streamdo |     at emitOne (events.js:96:13)
1|streamdo |     at Socket.emit (events.js:188:7)
1|streamdo |     at TCP._handle.close [as _onclose] (net.js:497:12)
0|streamdo | MongoError: connection 3 to cluster0.mongodb.net:27017 closed
0|streamdo |     at Function.MongoError.create (/opt/AppName/node_modules/mongodb-core/lib/error.js:29:11)
0|streamdo |     at Socket.<anonymous> (/opt/AppName/node_modules/mongodb-core/lib/connection/connection.js:202:22)
0|streamdo |     at Socket.g (events.js:292:16)
0|streamdo |     at emitOne (events.js:96:13)
0|streamdo |     at Socket.emit (events.js:188:7)
0|streamdo |     at TCP._handle.close [as _onclose] (net.js:497:12)
PM2        | App name:AppName id:1 disconnected
PM2        | App [AppName] with id [1] and pid [22300], exited with code [0] via signal [SIGINT]
PM2        | Starting execution sequence in -cluster mode- for app name:AppName id:1
PM2        | App name:AppName id:0 disconnected
PM2        | App [AppName] with id [0] and pid [22294], exited with code [0] via signal [SIGINT]
PM2        | Starting execution sequence in -cluster mode- for app name:AppName id:0
PM2        | App name:AppName id:0 online
PM2        | App name:AppName id:1 online
0|streamdo | Env: production Port: 443/undefined User: root DB: cluster0.mongodb.net:27017/AppName,cluster0-01.mongodb.net:27017/AppName,cluster0.mongodb.net:27017/AppName
1|streamdo | Env: production Port: 443/undefined User: root DB: cluster0.mongodb.net:27017/AppName,cluster0-01.mongodb.net:27017/AppName,cluster0.mongodb.net:27017/AppName
1|streamdo | MongoError: connection 3 to cluster0.mongodb.net:27017 closed
1|streamdo |     at Function.MongoError.create (/opt/AppName/node_modules/mongodb-core/lib/error.js:29:11)
1|streamdo |     at Socket.<anonymous> (/opt/AppName/node_modules/mongodb-core/lib/connection/connection.js:202:22)
1|streamdo |     at Socket.g (events.js:292:16)
1|streamdo |     at emitOne (events.js:96:13)
1|streamdo |     at Socket.emit (events.js:188:7)
1|streamdo |     at TCP._handle.close [as _onclose] (net.js:497:12)
PM2        | App name:AppName id:1 disconnected
PM2        | App [AppName] with id [1] and pid [22314], exited with code [0] via signal [SIGINT]
0|streamdo | MongoError: connection 3 to cluster0.mongodb.net:27017 closed
0|streamdo |     at Function.MongoError.create (/opt/AppName/node_modules/mongodb-core/lib/error.js:29:11)
0|streamdo |     at Socket.<anonymous> (/opt/AppName/node_modules/mongodb-core/lib/connection/connection.js:202:22)
0|streamdo |     at Socket.g (events.js:292:16)
0|streamdo |     at emitOne (events.js:96:13)
0|streamdo |     at Socket.emit (events.js:188:7)
0|streamdo |     at TCP._handle.close [as _onclose] (net.js:497:12)
PM2        | Starting execution sequence in -cluster mode- for app name:AppName id:1
PM2        | App name:AppName id:0 disconnected
PM2        | App [AppName] with id [0] and pid [22320], exited with code [0] via signal [SIGINT]
PM2        | Starting execution sequence in -cluster mode- for app name:AppName id:0

Here is my connection configuration:

mongo: {
    host: 'cluster0.mongodb.net:27017/f,cluster0.mongodb.net:27017/AppName,cluster0.mongodb.net:27017/AppName',
    options: {
        user: 'admin',
        pass: 'password',
        auth: {
            authdb: 'admin'
        },
        db: {
            native_parser: true,
            readPreference: "primaryPreferred"
        },
        server: {
            ssl: true,
            slaveOk: true,
            auto_reconnect: true,
            socketOptions: {
                keepAlive: 5,
                connectTimeoutMS: 10000,
                socketTimeoutMS: 10000
            },
            readPreference: "primaryPreferred"
        },
        replset: {
            rs_name: 'Cluster0-shard-0',
            readPreference: "primaryPreferred",
            ha: true,
            socketOptions: {
                keepAlive: 5,
                connectTimeoutMS: 10000,
                socketTimeoutMS: 10000
            }
        }
    }
},

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

18reactions
delino12commented, Mar 2, 2018

Navigate to setting add your system IP address on the white-list section. Solved.

10reactions
lrnfrmHomecommented, Dec 20, 2017

Please check IP whitelist or firewall. My case by default Atlas mongodb allowed only my PC’s IP. Hence blocked from AWS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

connection 3 to cluster0-shard-00-02-z0urk.mongodb.net ...
I had same problem when i want to connect to https://cloud.mongodb.com "mongoDB Atlas" This problem happened to me because when I built ...
Read more >
Port 27017 closed for security reasons - MongoDB Atlas
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...
Read more >
MongoDB database connection error
TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(3) { 'cluster0-shard-00-00.pte5t.mongodb.net:27017' => ServerDescription ...
Read more >
Can't connect to mongodb.Could not connect to any servers in ...
while trying to connect with mongodb i'm getting this error ... servers: Map { 'cluster0-shard-00-00-g4ttz.mongodb.net:27017' ...
Read more >
MongoServerSelectionError: connection closed - MongoDB
MongoServerSelectionError: connection <monitor> closed at Timeout. ... servers: Map(3) { 'cluster0-shard-00-00.d1mrz.mongodb.net:27017' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found