Alternatives for connecting MongoDB Atlas to Heroku Node.js app - node.js

I've been working on a simple website using node.js, express, and MongoDB, with Heroku as the platform. I had a lot of issues early on trying to connect to my MongoDB Atlas cluster, resulting in Heroku throwing request timeout errors.
After some googling, I surmised that the issue was that the IP addresses Heroku sends requests to my DB from weren't whitelisted in Atlas. The simplest solution at that point was to egregiously set up so that any IP is considered whitelisted. Which actually works fine currently.
My actual question comes down to the fact that whitelisting all IPs hardly seems like a professional way to go about doing things.
What is a better solution?

You should only allow those IPs from which your application will connect to MongoDB Atlas. Allow all IPs is a bad practice and could make that MongoDB instance vulnerable.

Related

MongoDb in Fastcomet shared hosting server

I haven't created an account in fastcomet yet, but I would like to ask the following questions. The site mentions that it supports node.js applications. So that means I can upload an express application right? How about MongoDB and mongoose? I can access MongoDB by typing in the terminal
$ mongod --dbpath=data -bind_ip 127.0.0.1
And the MongoDB URL in my case is
mongodb://localhost:27017/ilovearisti
How different would it be in fastcommet? I guess I'll have to contact them for the port numbers right?
Thanks,
Theo.
FastComet Team here! Indeed, our hosting plans are fully compatible with Node.js apps and you can have your project hosted on an appropriate package.
If you plan using the MongoDB database type in particular, our experience shows it requires a server solution with more resources and a higher level of private access. This way you would be able to accommodate the operation on an environment suitable for coding with Mongoose on top of the MongoDB driver for Node.js.
As for addressing your URL query, the default IP when starting a mongo instance is set to 127.0.0.1, so it is configured as localhost and port 27017. With this in mind, you only need to make changes on the IP configuration via the mongod daemon if you are using a remote database setup with a different IP address.
Having said all of the above, we can confirm that if you have created a database called iloveiristi, the MongoDB URL will be as follows:
mongodb://localhost:27017/iloveiristi
We hope this information proves useful and we wish you good luck with your project.
Best regards!

How do I get the exact IP address of my mongodb database?

I am trying to deploy my node app to my Cpanel however the page times out with an error
503 Service unavailable
The website works on Heroku, ngrok and localhost however on my hosting service, it doesn't.
I found out that the issue was due to port 27017 not being open.
On discussing with my hosting providers, they said
"We can open the ports for you but our policy is to open non-standard ports to specific IP's for better security of the server. Is it possible to get the exact IP addresses of the database server you are trying to connect to."
So I'm not familiar with mongodb database having a specific IP address. What could they mean?
To connect to your db, your node app needs a URL something like this.
const url = 'mongodb://hostnameOfMongo.example.com:27017'
Your database's hostname is the stuff after mongodb:// and before :27017.
Open up a shell (a command window) and type
ping -n 1 hostnameOfMongo.example.com
or maybe
ping -c 1 hostnameOfMongo.example.com
It should show you the IP address associated with your mongo server.
(Obvs, put your actual db hostname into the command, not my example.)
It's a little strange that your hosting provider didn't ask for the hostname when you didn't know the IP address. If they were my hosting provider, my confidence in the competence of their support would go down a notch because of that.
And please be aware that running a db in one data center and a node app (or indeed any app that uses the db) in another data center is a formula for poor performance and unreliability. The app and the db work best with a short and private network connecting them. With respect, it doesn't seem likely you have the network engineering chops to make that sort of thing stable and reliable.
Not to mention the security problems with exposing mongodb to the public network. Your hosting service is reluctant to open a port for a very good reason. Read this. Because cybercreeps

Router cannot connect to MongoDB cluster

I am working on a MERN project. I was previously able to connect to the MongoDB cluster via the connection string in my node application. A few days ago I changed my wifi router and I am not able to connect to the database anymore.
I am sure the problem is the router because when I connect through my phone's internet, the connection is successful. Unfortunately, I cannot use my phone's internet always as it is really expensive.
I have tried to factory reset my router as suggested in this question.
Error at connecting to MongoDb Atlas Server
I have also tried to enable port forwarding for port 27017, 27018, 27019 (default ports of MongoDB) through the router settings as suggested by someone on the internet.
Screenshot of my router settings
But did not face any success.
Some additional information. I am using a d-link router(DIR-819). The latest version of windows, node.js at the time of asking this question. Any help would be greatly appreciated as I am stuck on this problem for hours now.
EDIT: Here is the output when I try connecting through the shell.
DNSHostNotFound: Failed to lookup service ": This operation returned because the timeout period expired.
try 'mongo --help' for more information.
Please help me out, guys.
I have whitelisted all the IP addresses.
Cheers Everyone
Okay, look I'm also facing the same issue.
The reason behind this is because your Router is not letting your backend to make a connection with the MongoDB atlas.
You can still use it through your mobile network. Connect your PC with your mobile network and then try again it will work. (worked for me)
And for the Router issue, I'm still searching the solution once I find it, I'll be updating the answer. And yes if you find the solution before me be sure to Update us.
Use MongoDB in local machine. If that helps. I was also facing a similar issue so dropped the idea of connecting it to MongoDB Atlas. I downloaded it and it's running smoothly
Try to downgrade nodejs driver. I have resolved this by downgrading nodejs 3.2 driver string to 2.2 driver string.

queryTxt ETIMEOUT merncluster-0jdze.mongodb.net

I'm using nodejs with MongoDB server using mongoose, whenever I try to run my server, it won't connect to my database. After sometime above log is seen on CMD. My cluster name is merncluster here!!
Is there any solution to this, I've searched a lot, still can't find proper solution to this, this keeps happening all the time.
You may need to whitelist your IP address in MongoDB Atlas.
Adding 8.8.8.8 to my connection to the internet works for me.

I cannot connect to MongoDB Atlas database even though my IP is whitelisted

I am trying to connect to a mongoDB Atlas cluster using my node app, it wasn't working so I tried it with the mongoDB Compass. It shows timeout error. I can't figure out the problem, I have already whitelisted my IP from the database.
I am behind a proxy (my college has a proxy) but I even tried it with mobile data but it still won't work.

Resources