Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB connection error getaddrinfo ENOTFOUND Cluster0_shard_name

I'm having an issue with trying to connect to MongoDB. I'm using mongoose here,

console.log("trying here"); // prints

await mongoose.connect(db, {
  useNewUrlParser: true,
  useCreateIndex: true,
  useFindAndModify: false
});

console.log("Mongo DB connected"); // never reached

And it throws an error

getaddrinfo ENOTFOUND cluster0-shard-00-01-abcde.mongodb.net

I have never had this error before, it used to work just fine -- not sure where to go from here. I've tried building a new cluster and network access by IP is set to public 0.0.0.0. It just keeps throwing the same error.

like image 943
Mike K Avatar asked Jan 25 '26 03:01

Mike K


1 Answers

Note that if you specify useNewUrlParser: true, you must specify a port in your connection string, like mongodb://localhost:27017/dbname. The new url parser does not support connection strings that do not have a port, like mongodb://localhost/dbname.

see more info here

one more option, if you use node.js +mongo Atlas:

  1. log in https://cloud.mongodb.com/
  2. go to your cluster0 or other cluster,
  3. click connect... choose connet your application
  4. choose node.js and then 2.2.12 or later -

you will get connection string that start with mongodb://

insted of mongodb+srv:// (for node js 3.0 or later)

like image 123
Citizen-Dror Avatar answered Jan 27 '26 18:01

Citizen-Dror