MongoDB remote connection - mongodb

I am trying to use MongoDB for remote connection. First I tried localhost and internal ip address to build the connection. Both of them succeeded. Then I tried external ip address to connect on my own computer of MAC as an experiment first. I have created an admin user for the database in MongoDB. The bind_ip option is also commented in mongodb.conf. The firewall should also have been turned down. But the connection cannot been built still. Does anyone know the reason?
Thank you very much!

To allow connecting remote clients (which is your case) to MongoDB Server.
Make sure to do set bind_ip to either of values, in your mongodb.conf file -
Following is with any IP
bind_ip = 0.0.0.0
Following for bind to specific IP
bind_ip = 10.52.36.99 (or any specific IP)

Related

MongoDB not running on the provided host and port

I tried to connect to my local mongodb server through compass but It throws me an error "MongoDB not running on the provided host and port", I tried to change the config file many times. I tried setting
"bindIpAll: true" , bindIp: my hostname(IPv4 Address), comment the line. Everything is a failure. Anybody help me with this.
Thanks in Advance.
Just go to task manager then click on services and search for MongoDB, right click on it and start the service.
This works for me.
Have you try port number after IP address 27017 followed by ":".
I had this issue when attempting to connect with mongoDB Compass to a mongoDB on an AWS Ubuntu Linux instance (Lightsail Service to be exact).
After reading lots of similar questions here and elsewhere (and having checked my firewall rules to allow port 27017 connections) I went to check the /etc/mongodb.conf file.
By default the bind_ip (at row 11 in my system) comes configured as 127.0.0.1, and that is fine to connect from the same machine. In this config entry you should put all the IP's that are listening for connections.
I tried to add my instance public IP just after a comma
bind_ip = 127.0.0.1,35.xx.xx.xx (where 35.xx.xx.xx was my public IP)
At this point the service didn't start anymore. I started the service manually while following the log (look under /var/log/mongodb) to read this error message:
2019-03-19T15:51:59.338+0000 [initandlisten] ERROR: listen(): bind() failed errno:99 Cannot assign requested address for socket: 35.xx.xx.xx:27017
So I tried with 0.0.0.0 as suggested on another site but it gave a similar error.
I solved this by putting my AWS instance private IP address in the config file:
bind_ip = 127.0.0.1,172.xx.xx.xx (where 172.xx.xx.xx is your PRIVATE IP)
Hope this helps!
I have stumbled with the same issue while using latest mongodb v:6.14.4, my solution is to open cmd and run 'mongo' this gives the path where the mongodb is running if already installed, copy the path from cmd and you can use as below in your node module:
const mongodb = "mongodb://127.0.0.1:27017/database-name";

Connecting mongo via remote from Google Compute Engine

I've installed the mongodb in one of my GCE machine. I'm having problem connecting to mongodb from remote machine(i.e. Home Computer).
With Google search I found, I have to bind the right ip address and need to add respective firewall rules, After doing so, Still not able to connect.
Is there any way to connect from my remote machine to access mongodb database ?
Attaching mongodb.conf and firewall rule
#mongodb.conf file
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
bind_ip = 0.0.0.0
port = 27017
journal=true
Try changing "bind_ip" in mongodb.conf file to your mongo instance ip and try. (i assumed that correct firewall rules are added in the compute engine settings)
The problem is with in the target tag which was added when creating the firewall rule. Create the firewall rule for mongodb with Apply to all target tag fixed this issue.

Connect to EC2 via MongoDB Compass Community

I have an EC2 instance (Ubuntu 14). I've defined port 27017 in its security group to be accessed from anywhere.
I'm trying to connect from my PC (Windows 10) to the EC2 instance, but getting the following error:
MongoDB not running on the provided host and port
which is not true since I've made sure that MongoDB is running.
These were the settings I specified:
I've also tried to use SSH (which is also defined in the sercurity group and works well through terminal), but got the following error:
Error creating SSH Tunnel: (SSH) Channel open failure: Connection
refused
These were the settings I specified:
Alon,
Checkpoints
See if there is a need to change the bind_ip variable at the /etc/mongodb.conf file.
By default, it is locked to localhost.
Try setting the value to 0.0.0.0 or assign the IP that will be able to connect the DB to it.
Port is allowed in security group attached to EC2 (You mentioned its done)
Not using private IP to connect (I guess, you are using the right one as you connected through terminal)
Rare case, OS firewall
I guess point 1 should do the trick. Rest points for future reference
[Update 1]
Doc link for bindIp
https://docs.mongodb.com/manual/reference/configuration-options/#net-options
I am putting this as an answer although it is meant as a comment to the answer by raevilman above but I do not have the necessary reputation to post comments yet.
If you set the bind_ip variable to 0.0.0.0 as mentioned, MongoDB accepts connections from all IP addresses. This might be a security risk if you do not use access control.
On the other hand, if I am not wrong, the idea of a SSH tunnel is for that, in this case MongoDB, the entering connection comes from localhost so no change in the configuration should be necessary.
That said, I came here because I could not connect either. I got the error 'Error creating SSH Tunnel: Timed out while waiting for forwardOut'.
In my case the solution was to put 'localhost' in the hostname field at the top instead of the host IP.

Issues with setting bind_ip in mongo on centos, pymongo

Running Mongo 3.0.3 on Centos6.5. Running app with pymongo (python3.4) from a different server.
Can only get connection from remote server to work by commenting out bind_ip completely, which I'm a bit worried about. How can bind_ip be set to accept connections from specific IP of remote server and from localhost?
I tried:
bind_ip = 127.0.0.1 # only local works
bind_ip = 127.0.0.1, <remote server IP> # all traffic stops
The bind_ip configuration option in mongodb is for attaching mongos or mongod process to listen to specific network interface. As you may know a server may have multiple network interfaces, you may choose on which interface you want mongos/mongod to listen for connections.
If I put it simply, bind_ip configuration allows attaching mongos/mongod process to one or multiple ips that are available on a server so that it will listen for connection requests coming on the specific ip/ips. bind_ip sometimes confuses beginers. They may feel that bind_ip is for allowing an ip FROM where connections to the mongodb are allowed. But this is not true.
For example, suppose you have a server that has 2 network interface i.e x.x.x.x and y.y.y.y
Now if you run mongod process on this server and use x.x.x.x in bind_ip configuration then you can connect to this mongod from anywhere if you use x.x.x.x:27017 in your connection string. Though the other ip is also on the same server but you will not be able to connect to this mongod using y.y.y.y:27017 in your connection string. Similarly, if you bind your mongos/mongod to 127.0.0.1 then you will only be able to connect to it from the server itself.
Hope this helps.

mongodb.conf bind_ip = 127.0.0.1 does not work but 0.0.0.0 works

I could not understand what bind_ip in mongodb is. I could make a remote connection from desktop to the EC2 machine by having bind_ip = 0.0.0.0, but could not make it work with bind_ip = 127.0.0.1.
Please explain me what bind_ip is and why it works for 0.0.0.0 and not for 127.0.0.1.
For reference from mongodb docs:
bind_ip
Default: All interfaces.
Set this option to configure the mongod or mongos process to bind to and listen for connections from applications on this address.
You may attach mongod or mongos instances to any interface; however,
if you attach the process to a publicly accessible interface,
implement proper authentication or firewall restrictions to protect
the integrity of your database.
You may concatenate a list of comma separated values to bind mongod to multiple IP addresses.
Everywhere it's written that you have to bind them like this
bindIp : 127.0.0.1,192.168.0.50
but it doesn't work.
how it works, in the version 3.2.0 is
bindIp : [127.0.0.1,192.168.0.50]
so try to add your ips inside the [ ]
example :
# network interfaces
net:
port: 27017
bindIp : [127.0.0.1,0.0.0.0] (read what is written below in BOLD!)
However 0.0.0.0 opens up the stuff. While this is ok for TESTING, for production you should know the security implications of this setting!
Before binding your server to 0.0.0.0, please be clear about the security implications of those changes: Your server will be publicly exposed to all IPs on the whole internet. Be sure to enable authentication on your server!
You can't access your machine when you bind it to 127.0.0.1 on EC2. That's not a bug, it's reasoned by the network interface bindings.
127.0.0.1 will only bind to the loopback interface (so you will only be able to access it locally), while 0.0.0.0 will bind it to all network interfaces that are available.
That's why you can access your mongodb on EC2 when you bind it to 0.0.0.0(as it's available through the internet now) and not via 127.0.0.1.
For local servers (like a WAMP or a local mongodb server) that won't look different to you, but for that case you should also thing that binding to 0.0.0.0 for local servers might make them available over all network interfaces (so it might be public for someone who knows your IP, if there is no firewall!)
Read on a similar question on Server Fault here.
It should be clear for anyone looking up this answer that binding your mongoDB to 0.0.0.0 could be your worst move ever.
Please read up on the following article and make sure that whenever you DO decide to go all public with your (and your customers) data, you consider the following:
Do you have additional firewall rules to decide who or what can
access your service
Understand that when using Amazon EC2, if you allow 'internal' traffic it should be considered the same as putting it wide open, you
are not alone at Amazon
Are your services password protected ? And what kind of authentication ? Is the data submitted in clear text or using
encryption
Are you using the default database names, or have you copy pasted an example?
For linking to local Mongo db installation (dev environment), I found following 3 step process much easier - and it worked.
docker run -d -p 27017-27019:27017-27019 --name mongodb mongo
docker exec -it mongodb bash
mongo (voilla - you are in i.e. connected to Mongo dab)
Source: https://www.thepolyglotdeveloper.com/2019/01/getting-started-mongodb-docker-container-deployment/