Node.js app cannot connect to MongoDB Atlas cluster

Hi everyone.

I think I have a very specific issue.
I created my first ever website with Node.js in the back-end and a MongoDB Atlas cluster as a database.

I hosted my web app on Heroku where everything works fine. A few days ago when I got everything launch-ready I decided to pay for proper hosting and buy the domain I wanted.
My host server uses cPanel where it is possible to setup a Node.js app, which is what I did. However my website is not loading because I cannot establish a connection to my Atlas cluster.

It’s the exact same code that works fine on Heroku, so I’m not sure it has to do with MongoDB itself. Perhaps it’s some security setting inside cPanel that prevents this connection but I was not able to find any.

With the ‘useUnifiedTopology’ flag set to true I get a MongooseServerSelectionError. I saw some people having issues establishing a connection with this flag, so I tried commenting it out and now get a MongoNetworkError instead.

Here’s my code:

const connectionString = 'mongodb+srv://' + process.env.MONGODB_USER + ':' + process.env.MONGODB_PW + '@<REDACTED_APP>-y1llv.mongodb.net/<REDACTED_APP>';
mongoose
    .connect(connectionString, {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    useFindAndModify: false
    })
    .then(() => console.log('Database connected.'))
    .catch(err => console.log(err));

You can see a picture of my cPanel Node.js app setup and the complete callstack for these errors on my StackOverflow post here.

I really hope someone can help me out. It’s my first website and the first time I host anything, so I might very well miss something but I don’t know where or what to look for anymore. I’m pretty much stuck.

Hello good day!
I am currently facing the same issue. I also saw your post here on stackoverflow : https://stackoverflow.com/questions/62570924/cpanel-node-js-app-cannot-connect-to-mongodb-atlas-cluster-but-works-on-heroku.

I am also using Namecheap. Have you found any solution yet?

Sadly not, seems to be a very specific issue not many people know about. If nobody is able to help I’ll have to stay on Heroku for hosting and pay for Hobby dynos to use my SSL certificate. Which is annoying because I just paid for 2 years hosting on Stellar and it’s stupidly cheap compared to Heroku. Not that Hobby dynos are expensive but compared to Stellar it costs 5 times more.

I’m asking MongoDB support but I’m not sure they’ll be able to help. Maybe they can give me some clues how to get more information why this MongoNetworkError occurs. But I’ll have to wait until Monday to contact them.

Alright thanks.
Keep me posted on when you have solved the issue.
Thanks

Wow I just fixed the issue.
Contact Namecheap support team and ask them to open up ports:
3000
443 and
80.

Good luck!

Hey, I resolved the issue!
I contacted Namecheap support via live chat, they have been very patient.
Looking at my passenger.log file and everything. Turns out they had to open the default MongoDB port 27017 on their end, no way to do it without support unfortunately.

So it seems you just need to contact them and tell them you need this port opened.
Hope that’s all it takes for you as well. Good luck!

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