MongoDb in Fastcomet shared hosting server - node.js

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!

Related

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

Alternatives for connecting MongoDB Atlas to Heroku Node.js app

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.

Connect to MongoDB Atlas from Google App Engine

I'm trying to set up an API on the Google App Engine standard environment but I'm having trouble connecting to a distant MongoDB instance.
I use a free M0 cluster on MongoDB Atlas, all IP are whitelisted. The Google App Engine firewall rules allow all traffic from all IP addresses to make sure the connection request is not blocked.
However, I cannot connect to my Mongo instance and I get the following error:
ERROR db_connection querySrv ESERVFAIL <mongo-url>.gcp.mongodb.net
To connect to the MongoDB instance I use Mongoose and do the following:
const db = await mongoose.connect(uri, { useNewUrlParser: true });
However db will always be null since I cannot connect. Is there a way to connect to MongoDB Atlas from App Engine Standard or do I have to use Cloud Datastore ?
I had to use the older version of the atlas url
It works for me with older driver version url:
You don't have to use Datastore. You can connect to a mongoDB hosted outside of Google servers, there is some example code here. I would also like to refer you to this documentation on connecting to external databases from the Standard Environment.
Making sure your firewalls are open is necessary (and you have already done that, so that's great).
MongoDB Node.JS drivers are listed here, depending on the version you use, different reference documents are available with connection samples (all listed on that same link).

Procedure for creating a database locally using mongodb

I've connected to mLab using MongoClient.connect, but now I want to create a local database rather than use mLab. I've tried changing the URL to "mongodb://localhost:27017/mydb" to create a database locally as suggested, but I'm getting a failed to connect to server error. Is there anything I should install? Or am I just missing something?
First, install your mongoDB server:
https://docs.mongodb.com/manual/installation/
After that, you should be able to start your local mongodb server:
sudo service mongod start //in case of linux
Here the docs to start the mongodb server
Then, you will type mongo and the client command line will start.
And when typing use yourDatabase, mongo will create the database if
it does not exist.
Then, your server will be up, and the database created. Anyway, check the docs if you need the guides for windows or other systems.
First you need to understand how server works and mlab works. mLab is a company who provide database as a services. Means you no need to handle server hurdles, they take care server , backups , infrastructure etc. They provide mongodb server , You just need to create database on mongodb server's and use them.
Now comes to main points.
If you want use mongodb on your local system then you first need to install mongodb on your local system and then connect it.

Secure MongoDB Over Public Internet

With the recent attacks on Mongo databases, I've seen many guides on how to password-protect your database. I've gone through each guide and I've set up a 'superAdmin' with root role and another basicAdmin with read/write privileges. I reboot mongo using
mongo --auth
and authenticate using my superAdmin login, however this causes problems for my site which uses this db. When I boot my Node app, I can't access any pages as it cannot connect to the database because it has auth enabled. If in my config/database.js file I have:
module.exports = {
'database': 'mongodb://myWebsite.com/myDatabase'
};
How can I allow my site to access my MongoDB and read/write as users signup but also restrict any ransomware group from just walking in and dropping every collection over and over?
There are three main methods that you can use to protect your database.
Username and password
This is the simpler one. As you have mentioned that you have already secured the server using password, you can simply connect to database using mongoose as
mongoose.connect('mongodb://username:password#host:port/database');
I might recommend here that you change the default port of mongoDb to something else. Changing port can be found in file /etc/mongodb.conf.
Bind to private ip and use firewall
Again refering to file /etc/mongodb.conf change bind_ip to local ip of your network. Most of the services do provide that. Also better to setup firewall for the same. Simple firewall that you can use is UFW. Only allow traffic from servers that you are using. This method might not be effective if you are using shared vpn service.
SSH tunnel to access database
This is far most the most reliable method and i would recommend you to use this with the last method. Here is how this works. Set bind_ip to 127.0.0.1. Let us assume that you are running port on 5000. In order to set up a tunnel use
ssh \
-L 4000:localhost:5000 \
-i ~/.ssh/key \
<username>#mongo_db_ip
Remember to add your ssh key in instance running mongodb database. The above command should be issued on server that is running nodejs. 5000 as mentioned is the remove port and 4000 is the local port that you need to connect to on mongodb. So your command in mongoose to connect to database would be
mongoose.connect('mongodb://localhost:4000/<database>');
Summary
As you can see in almost all the steps i have focused on setting up a firewall which is very important.
Also username and passwords should be avoided and it is better to use ssh keys. From practical experience they also reduce a lot of burden while you are scaling up your service.
Hope this helps you out.

Resources