Connection error while trying to connect to MongoDB Atlas

I’m using Mongoose to attempt to connect to my mongodb cluster and I keep getting this error:

I have tried searching online and most of the solutions point to the Network Access section of Atlas.
I have allowed access to all ips, but i still get the same error.

I’m using Node.js.
Here is the code i’m using to connect if it’s any help:

const mongoose = require("mongoose");

const options = {
  useNewUrlParser: true,
  useUnifiedTopology: true,
  serverSelectionTimeoutMS: 5000,
const uri =
  process.env.NODE_ENV === "development"
    ? "mongodb://localhost:27017"
    : process.env.MONGODB_URI;

const connectDB = async () => {
  try {
    const conn = await mongoose.connect(uri, options);
    console.log(`MongoDB Connected: ${}`);
  } catch (error) {

module.exports = connectDB;

Node version: v16.16.0
Nodemon: ^2.0.20
MongoDB: 6.0
Mongoose: ^6.0.12

Hello @John_Fiewor ,

Welcome to The MongoDB Community Forums! :wave:

Could you please try connecting to your Atlas cluster via Mongo Shell or Compass?

If your URI is valid and you are able to establish a connection then, the next most common reason for such connection issues is either your IP address is not whitelisted (you have whitelisted your IP/ Allow access to all IPs) or port 27017 is blocked by either your IT department or your Internet Service Provider.

Can you try below command in your terminal to make sure port 27017 is open for you?


Let me know if this works.


1 Like

Hi @Tarun_Gaur ,

Thank you for the warm welcome and for your answer.

I was able to connect to my cluster via Compass.

I also tried the command you suggested and it worked which should mean port 27017 is available.

As you are able to connect to your Atlas Cluster using Compass means your Atlas cluster is working as expected and there are no connection issues between your machine and Atlas.

Can you reconfirm if your Connection string is correct and the user your are trying to connect with is having all the required access?

Have you added in the IP access list of your Atlas cluster?
If not, please double check the whitelist includes the host IP your app is running on.

There could be many other other cases such as

  • Firewall is blocking the connection (disable firewall and try again)
  • Application environment might have some issue, it could also happen due to system settings such as mis-matched server’s date & time.
  • Check that the database exists and you are connecting to the correct one in the yourDatabaseName slot
  • In case, you are using any proxy or certificate based internet then such issues can rise. Try connecting via different internet connection.

Above are some of the most common reasons for connection issues.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.