MongoDb database connection time out error

Posted February 16, 2021 4.4k views

When i tried to get connected to mongodb i got a timeout error,
i have added firewall rules for remote access.
what I did :

  • Incoming rules => added new rule => custom TCP | 27017 | All IPv4 All IPv6

the mongo server is running and the user has rules in a specific database.

note : I run it successfully on AWS before with the same steps .

I m sure the error that I got is about the remote access IP .

error :

MongooseServerSelectionError: connection timed out
    at NativeConnection.Connection.openUri (C:\scrap-source\node_modules\mongoose\lib\connection.js:845:32)
    at C:\scrap-source\node_modules\mongoose\lib\index.js:345:10
    at promiseOrCallback (C:\scrap-source\node_modules\mongoose\lib\helpers\promiseOrCallback.js:9:12)
    at Mongoose._promiseOrCallback (C:\scrap-source\node_modules\mongoose\lib\index.js:1135:10)
    at Mongoose.connect (C:\scrap-source\node_modules\mongoose\lib\index.js:344:20)
    at Object.<anonymous> (C:\scrap-source\server.js:8:5)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  reason: TopologyDescription {
    type: 'Single',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(1) { 'My_private_IP':27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null


1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

The first thing I always double-check is to see if the service is listening on my public interface (eg, eth0):

sudo netstat -tnlp

That will list on all running services and the interface / port they are listening on. If your mongodb instance is not listening on the IP you’re trying to connect to or (all interfaces), you won’t be to able to remotely connect.

If mongodb is listening on the correct interface, the next step would be to check the mongodb logs for any additional information about your connection attempts.

Hope these hints help, please let us know if there’s any other questions or feedback on this advice - thanks and good luck!