Failed to connect to cluster using a env file

Hello everyone, I am following this YouTube Video guide
here. All steps up to connecting to the Mongo DB work. Ive turned off my Laptop’s firewall, and my my router’s firewall, allowed all IPs on MongoDB, still no working connection. I constantly get the below error:

MongoError: failed to connect to server [cluster0.3az5zxq.mongodb.net:27017] on first connect [Error: getaddrinfo ENOTFOUND cluster0.3az5zxq.mongodb.net
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:118:26) {
  name: 'MongoError'
}] did not connect
[nodemon] clean exit - waiting for changes before restart

For the life of me I cannot seem to get anything to work. I cannot ping the cluster, I have tried 4 different wifi networks, phone hotspot, liabry internet, home wifi, work wifi, and nothing! I’ve disabled my mac’s firewall to try to get it to work, port forwarded “1337”, “27016”, “27017”.
Below is a copy of my code I want to work:


import express from "express";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import cors from "cors";
import helmet from "helmet";
import morgan from "Morgan";
import dotenv from "dotenv";
/* Configurations (mainly used as a boiler plate to config these packages in the long run m*/
dotenv.config();
const app = express();
app.use(express.json());
app.use(helmet());
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin" }));
app.use(morgan("common"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());


console.log("Hello World");
/* Mongoose SETUP */
const PORT = process.env.PORT || 9000;
console.log("Port from ENV", PORT);


mongoose
    .connect(process.env.MONGO_URL, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
    })
    .then(async () => {
        app.listen(PORT, () => console.log(`Server is running on port ${PORT}`));
    })
    .catch((error) => console.log(`${error} did not connect`));

Below is a copy of my .env file, with sensitive info removed for this forum:

MONGO_URL='mongodb://portfoliopromdb:*mypasswordredacted*@cluster0.3az5zxq.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0'
PORT=1337

Hi @James_Coates

MongoDB Atlas URIs are mongodb+srv:// try updating the connection string to begin with than instead of mongodb://

1 Like

this has been fixed by MongoDB support messages, now I have a new issues regarding: MongoError: (Unauthorized) not authorized on admin to execute command { dropDatabase: 1, writeConcern: { w: “majority” } } did not connect.

I have ensured that on the MongoDB website all user permissions are set to readwrite all database but still no luck

@James_Coates

dropDatabase is not a privilege granted by readWrite all, the dbAdmin role will grant the necessary privilege. If dbAdmin grants more privileges than desired creating a custom role is another option.

https://www.mongodb.com/docs/manual/reference/built-in-roles/#mongodb-authrole-dbAdmin
https://www.mongodb.com/docs/atlas/mongodb-users-roles-and-privileges/#specific-privileges
https://www.mongodb.com/docs/atlas/security-add-mongodb-roles/#configure-custom-database-roles