Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting ReplicaSetNoPrimary and MongoServerSelectionError error while connecting MongoDB with nodejs

I am trying to connect to mongodb but getting below error could you please help

var mongo = require('mongodb').MongoClient;
mongo.connect('mongodb://usernamexyz:[email protected]:47017/sampleDB?replicaSet=NAME_2436&readPreference=primary&authSource=admin&w=1', { useNewUrlParser: true, useUnifiedTopology: true  })
.then(() => console.log("Mongodb connected"))
.catch(err => console.log(err));

And the error i am getting as below

MongoServerSelectionError: connection <monitor> to 155.30.360.129:37017 closed
    at Timeout._onTimeout (C:\Fintech\NodeFirstApp\node_modules\mongodb\lib\core\sdam\topology.js:448:30)
    at listOnTimeout (internal/timers.js:531:17)
    at processTimers (internal/timers.js:475:7) {
  name: 'MongoServerSelectionError',
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map {
      'hostmxy-mw-e6-u1238.nam.nsroot.net:47017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}
like image 408
user2454202 Avatar asked Feb 04 '20 18:02

user2454202


People also ask

Why my database is not connecting in MongoDB?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.

Why MongoDB compass is not connecting?

Ensure Your MongoDB Instance is Running Compass must connect to a running MongoDB instance. Make sure you have installed MongoDB and have a running mongod process. You should also check that the port where your MongoDB instance is running matches the port you provide in the Compass connect dialog.


Video Answer


1 Answers

GO to Mongodb where you created a cluster and under ipwhitelist click on edit IP address button and then choose Add ccurrent IP address

this worked for me

like image 174
Godric Gryffindor Avatar answered Oct 15 '22 03:10

Godric Gryffindor