mongoose connection throwing MongooseServerSelectionError

Viewed 5180

I am trying to connect to my mongodb Atlas via the mongoose driver and it keeps throwing a MongooseServerSelectionError, and i have checked my username and password for the database user and it is correct.

Here is my code:

server running on port 3000
we are connected: { MongooseServerSelectionError: connection timed out
    at new MongooseServerSelectionError (/home/ateyib/UCSD-classes/CSE 135/hw3/node_modules/mongoose/lib/error/serverSelection.js:22:11)
    at NativeConnection.Connection.openUri (/home/ateyib/UCSD-classes/CSE 135/hw3/node_modules/mongoose/lib/connection.js:808:32)
    at Mongoose.connect (/home/ateyib/UCSD-classes/CSE 135/hw3/node_modules/mongoose/lib/index.js:333:15)
    at Object.<anonymous> (/home/ateyib/UCSD-classes/CSE 135/hw3/server.js:17:4)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
    at internal/main/run_main_module.js:21:11
  message: 'connection timed out',
  name: 'MongooseServerSelectionError',
  reason:
   TopologyDescription {
     type: 'ReplicaSetNoPrimary',
     setName: 'Cluster0-shard-0',
     maxSetVersion: null,
     maxElectionId: null,
     servers:
      Map {
        'cluster0-shard-00-00-ebets.mongodb.net:27017' => [ServerDescription],
        'cluster0-shard-00-01-ebets.mongodb.net:27017' => [ServerDescription],
        'cluster0-shard-00-02-ebets.mongodb.net:27017' => [ServerDescription] },
     stale: false,
     compatible: true,
     compatibilityError: null,
     logicalSessionTimeoutMinutes: 30,
     heartbeatFrequencyMS: 10000,
     localThresholdMS: 15,
     commonWireVersion: 8 },
  [Symbol(mongoErrorContextSymbol)]: {} }
6 Answers

It is something related to your ip address probably. Go to Network Access toolbar option click on EDIT button and then on ADD CURRENT IP ADDRESS and confirm. This worked for me.

Using Mongo 3.6 and Mongoose 5.9.1 connecting to a replicaSet not on Atlas, setting useUnifiedTopology to true caused my issues. Removing this from my options made it work.

{ useUnifiedTopology: false } or don't set it at all and let it default to false

https://github.com/Automattic/mongoose/issues/8180

You are probably behind a corporate firewall.

If you are not sure, you can check to see if you can reach the MongoDB port(27017 from your stacktrace) using a third party site, found in the MongoDB documentation, where there are instructions specifically for the issue you've run into.

This happened to me as well. I restarted my computer and then suddenly my app wasn't working... so restarting mongoDB worked by running the following in my terminal (while in my api/mongoDB folder).

brew services stop mongodb-community@5.0

and then run

brew services start mongodb-community@5.0

I have added authSource=admin, It works

mongodb://127.0.0.1:27017/?authSource=admin

I have used 127.0.0.1:27017 instead of localhost and it worked

Related