Having problem in connecting to remote MongoDB Server - mongodb

I tried to connect a remote MongoDB Server running on Ubuntu using MongoDB Compass on Windows. But I have problems connecting always as the IP of the Windows machine changes every day.
I did the following things to connect to the remote server-
Got the IP of the Client Machine, then allowed that IP on the firewall of the server machine on port 27017.
sudo ufw allow from client_machine_ip to any port 27017
Note: The ufw status looked okay.
Got the IP of the Server Machine, then on the MongoDB configuration file on the server, I modified the bindIp.
bindIp: 127.0.0.1,server_machine_ip
Note: I restarted mongod and it was okay too.
I was able to connect the remote MongoDB Server using MongoDB Compass successfully for the first time. Then I saw, the IP of the client machine was changing every day. So, every time, the client IP changes, I need to allow that IP on the firewall of the server machine (in which I am using the MongoDB Server) on port 27017. Could you help to solve this? Thanks in advance.

You can update the firewall for port 27017 to allow from anywhere since client machine IP is not static.
sudo ufw allow 27017 #(this will allow from any IP)

Related

How to connect VPS to MongoDB Compass

I bought a VPS and I want to put MongoDB server there. I connect to VPS with ssh tunnel, install MongoDB and start it.
The question is, that how can I add VPS server to MongoDB Compass? Is it even possible?
I was trying to connect via hostname but I receive this error - connect ECONNREFUSED my.ip.adress.here
Also was trying to connect via ssh tunnel in MongoDB Compass - doesn't work for me.
Maybe there is a problem with port?
I can connect to my VPS from terminal with ssh.
Edit1:
I was using this documentation to install MongoDB on ubuntu and I'm using MongoDB 4.2.8
EDIT2: When I install mongodb on VPS, MongoDB ip is 127.0.0.1 how can I change it? I want it to set it for VPS ip address. If it's possible, otherwise I don't understand how I can connect to my DB on VPS.
Sorry if it's lame questions, just first time using VPS and it's pretty complex to me.
Before you can connect to vps with mongodb compass, you need to allow remote connections to mongodb from your vps.
It is recommended to secure your mongodb connection with a username and password
Edit the mongod.conf file located at
sudo nano /etc/mongod.conf
Enter a comma and append the IP address of the vps to the end of the bindIp field. This will allow remote connections to the mongodb instance
Save the file and restart the mongodb connection
sudo systemctl restart mongod
Setup the firewall to allow connections to port 27017 (the default mongodb port) from anywhere. (Please note that will expose your mongo database to the internet. Therefore it is recommended to close this port when shipping to production)
sudo ufw allow 27017
That's it for the vps.
You can now open up mongodb compass
Choose "fill in connection fields individually"
Fill in the hostname with the IP address of your vps, port is the port which you opened previously 27017 in this case and select authentication by username and password.
Enter the username and password of the mongodb admin which you should have created already.
Choose the authentication database (usually admin)
That's it. You should be connected. You can now view the collections and fields of your mongodb instance on your remote machine

How to access Mongodb in AWS EC2 Ubuntu from my laptop [duplicate]

I've successfully installed MongoDB on Windows (on a local machine) as a service, but now I want to move MongoDb to a separate server. So I extracted the tarball to a virtual server on network (running linux).
When I connected to the server ("testmongoserver") using PuTTY from my local machine, I started the mongod server and it told me that it was listening to the default 28017 port. The mongo console is also working and allowed me to create a new database (testdb) and add users to it.
However, I could not access the server from remote. When I type testmongoserver:28017 it doesn't open the HTTP console as localhost:28017 on my local machine does. I also can't connect using official drivers and providing a connectionstring.
What are the neccesarry steps to install MongoDB on Linux, so that I could access it from a remote machine with a connectionstring and use its HTTP console via testmongoserver:28017
Thanks!
1. Bind IP option
Bind IP is a MongoDB option that restricts connections to specifics IPs.
Have a look at your mongod configuration file, most of the time bind_ip is set to 127.0.0.1 for obvious security reasons. You can:
Add your desired IP by concatenating a list of comma separated values to bind MongoDB to multiple IP addresses.
Remove or comment (with # character) the bind_ip line. But be aware that all remote connection will be able to connect your MongoDB server!
More about bind_ip configuration option: https://docs.mongodb.com/manual/reference/configuration-options/#net.bindIp
Bind IP can also be set as a command argument: http://docs.mongodb.org/manual/reference/program/mongod/#cmdoption--bind_ip
2. Firewall
Check that you are not running behind a firewall
Make sure in your /etc/mongodb.conf file you have the following line,
bind_ip = 0.0.0.0
http://jitu-blog.blogspot.com.br/2013/06/allow-mongo-to-connect-from-remote-ip.html
Run netstat -a on mongo server and check a port.
Check DNS settings and check that linux server allows external connections.
Check that mongodb can accept external/remote connection.
Default port for mongo is 27017.
28017 - port for webstats.
See http://www.mongodb.org/display/DOCS/Security+and+Authentication#SecurityandAuthentication-Ports
Just had this issue and this fixed it:
Edit /etc/mongod.conf with sudo nano /etc/mongod.conf ensure that the net section looks like below (localhost binding by default doesn't allow for remote access):
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
Make sure to restart mongod when you are done with above with below (assuming systemd ubuntu 16.04+ etc.):
sudo service mongod restart
Obviously from a security perspective if you are going to be opening up mongo to your network/the world be aware of the implications of this (if any)
Another problem may be that the mongodb port is not enabled. Check, from another host, the ports enabled on your server. For that you can use the command:
sudo nmap -P0 your_server_ip
You can get an answer like this:
Host is up (0.052s latency).
Not shown: 997 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp closed https
If you use a virtual server in the cloud, as AWS, you need to add a new rule to add mongodb port (27017 by default).
Important: Note that with this configuration anyone can have access to your database
I fixed by below reference :
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89
Actually, first i changed my bindIp from 127.0.0.1 to 0.0.0.0 in mongod.conf,
and enable security:
security:
authorization: "enabled"
then i restarted mongod using sudo service mongod restart.(because of new changes in mongod.conf), after that set firewall to open mongod running port (by iptables) and create a new user in admin db with new access (based on this link : https://medium.com/mongoaudit/how-to-enable-authentication-on-mongodb-b9e8a924efac), finally test open ports in my server from outside with (https://www.yougetsignal.com/tools/open-ports/) and connected successfully to remote mongod using mongocompass.exe.

mongodb only connect by localhost [duplicate]

I've successfully installed MongoDB on Windows (on a local machine) as a service, but now I want to move MongoDb to a separate server. So I extracted the tarball to a virtual server on network (running linux).
When I connected to the server ("testmongoserver") using PuTTY from my local machine, I started the mongod server and it told me that it was listening to the default 28017 port. The mongo console is also working and allowed me to create a new database (testdb) and add users to it.
However, I could not access the server from remote. When I type testmongoserver:28017 it doesn't open the HTTP console as localhost:28017 on my local machine does. I also can't connect using official drivers and providing a connectionstring.
What are the neccesarry steps to install MongoDB on Linux, so that I could access it from a remote machine with a connectionstring and use its HTTP console via testmongoserver:28017
Thanks!
1. Bind IP option
Bind IP is a MongoDB option that restricts connections to specifics IPs.
Have a look at your mongod configuration file, most of the time bind_ip is set to 127.0.0.1 for obvious security reasons. You can:
Add your desired IP by concatenating a list of comma separated values to bind MongoDB to multiple IP addresses.
Remove or comment (with # character) the bind_ip line. But be aware that all remote connection will be able to connect your MongoDB server!
More about bind_ip configuration option: https://docs.mongodb.com/manual/reference/configuration-options/#net.bindIp
Bind IP can also be set as a command argument: http://docs.mongodb.org/manual/reference/program/mongod/#cmdoption--bind_ip
2. Firewall
Check that you are not running behind a firewall
Make sure in your /etc/mongodb.conf file you have the following line,
bind_ip = 0.0.0.0
http://jitu-blog.blogspot.com.br/2013/06/allow-mongo-to-connect-from-remote-ip.html
Run netstat -a on mongo server and check a port.
Check DNS settings and check that linux server allows external connections.
Check that mongodb can accept external/remote connection.
Default port for mongo is 27017.
28017 - port for webstats.
See http://www.mongodb.org/display/DOCS/Security+and+Authentication#SecurityandAuthentication-Ports
Just had this issue and this fixed it:
Edit /etc/mongod.conf with sudo nano /etc/mongod.conf ensure that the net section looks like below (localhost binding by default doesn't allow for remote access):
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
Make sure to restart mongod when you are done with above with below (assuming systemd ubuntu 16.04+ etc.):
sudo service mongod restart
Obviously from a security perspective if you are going to be opening up mongo to your network/the world be aware of the implications of this (if any)
Another problem may be that the mongodb port is not enabled. Check, from another host, the ports enabled on your server. For that you can use the command:
sudo nmap -P0 your_server_ip
You can get an answer like this:
Host is up (0.052s latency).
Not shown: 997 filtered ports
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
443/tcp closed https
If you use a virtual server in the cloud, as AWS, you need to add a new rule to add mongodb port (27017 by default).
Important: Note that with this configuration anyone can have access to your database
I fixed by below reference :
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89
Actually, first i changed my bindIp from 127.0.0.1 to 0.0.0.0 in mongod.conf,
and enable security:
security:
authorization: "enabled"
then i restarted mongod using sudo service mongod restart.(because of new changes in mongod.conf), after that set firewall to open mongod running port (by iptables) and create a new user in admin db with new access (based on this link : https://medium.com/mongoaudit/how-to-enable-authentication-on-mongodb-b9e8a924efac), finally test open ports in my server from outside with (https://www.yougetsignal.com/tools/open-ports/) and connected successfully to remote mongod using mongocompass.exe.

Can't connect to Ubuntu MongoDB instance from MongoChef Client

All, new to MongoDB and new to Ubuntu. I have an Ubuntu VPS with MongoDB running. On my local machine, I'm trying to set-up MongoChef for my admin GUI. It creates the SSH tunnel successfully but then fails when connecting to my VPS on port 27017. I assumed it was a firewall on my VPS blocking traffic but using the ufw status command, I see the port is accepting traffic:
To Action From
-- ------ ----
Apache Full ALLOW Anywhere
27017 ALLOW Anywhere
Any ideas of what I can try next? I've also commented out the blockip line in my mongodb config file since that seems to have resolved the issue for others.
MongoChef Connection Error
enter image description here
Turns out my mongodb was not restarting properly after commenting the bind_ip. Had to work through multiple repair commands, start the service again, and issuing a netstat -a | grep "LISTEN " shows that port 27017 was now open to accept from any IP. MongoChef connected after that.

Can't connect my mongoDB database from my local machine

UPDATED:
I just start to deal with a new VPS and i am trying to connect its port 27017 (mongodb database) from localhost (using robomongo).
It's working on my localhost machine and with another remote server, but i can't connect mongodb on this VPS, what could cause this issue ?
I start it this way:
mongod --setParameter enableLocalhostAuthBypass=0 --config /etc/mongodb.conf --fork
with the auth = true uncommented in the /etc/mongodb.conf file (but it looks like the issue is not that i can't auth but mongodb is not responding at all).
When i start mongo admin -u root -p root by being connected with SSH, it works great (i can connect my database) and i can see that mongodb is well listenning on the port 27017.
But when i try to access it from my local machine, it can't establish the connection, and if i try to connect it with my browser, i am supposed to get a mongodb error message: You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number but i don't, i just have a failure page with Chrome (and the port 28017 is not responding either).
How to solve this ? My VPS is brand new and i could need to install more packages but i really don't see what could be need for this, it also looks like a firewall issue, like something prevent mongodb and response before the request could reach it, i have juste installed apache2 and nginx and they are not running so i really don't see...
Ok, I have found the issue!
There is a line bind_ip 127.0.0.1 in the /etc/mongod.conf file.
It provides access only to the specified IP address to mongodb.
You should create an array of IPs and edit the config file as:
bind_ip [127.0.0.1, x.x.x.x, ...]
Where x.x.x.x is your IP.
i resolved this issue with file(/etc/mongod.conf)
commenting out as follows:
#bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.