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.
I have MongoDb installed on two Ubuntu 16 VMs both hosted on the same network on Google Cloud's Compute Engine. The connecting instance has Internal IP 10.132.0.2, the database instance has 10.132.0.3.
(https://gyazo.com/59f9086591a1d6673e8194a50fff51e1)
I've opened tcp 27017 for the instance I wish to connect to. I added this using the gcloud console commands.
(https://gyazo.com/0d158aa735c6967e278fac63d598721f)
I have also tried opening tcp:1-65535;udp:1-65535 for testing purposes, with no result.
Running mongo on either instance will properly connect to it's own mongodb service, so they're installed, running and working.
On the database instance, netstat -a | grep :27017 displays that it's listening correctly.
(https://gyazo.com/0f4fb3c49a51f6886ff4ccb2d44a132a)
On the database instance, the config file at /etc/mongod.conf of the is edited so bind_ip also has the Internal IP of said instance.
(https://gyazo.com/20fb669506e7e67ef49fdcf9af6df144)
I have also tried 0.0.0.0 for testing purposes, with no result.
Running mongo 10.132.0.3 from the connecting instance results in Failed to connect to 10.132.0.3:27017, reason: errno:111 Connection refused.
(https://gyazo.com/9e5aec732e3f09cbfa62a4d78df3620f)
Running nmap -p 27017 10.132.0.3 on the connecting instance shows port 27017 is closed.
(https://gyazo.com/7ccd905db5b9946d616176b9ab75479c)
I'm at an utter loss, please help me out? :<
Your mongodb servers are only listening on the localhost address (127.0.0.1), rather than on INADDR_ANY (shows up as *:27017 in netstat).
Like this question, it looks like you need to set bind_ip = 0.0.0.0 in your mongod.conf.
Thanks to E. Anderson's link I managed to solve this issue; suddenly turning this into a duplicate. However, neither this answer nor the answer in the link were "correct".
The solution to set bind_ip=0.0.0.0 in mongod.conf is rather dangerous, besides, it didn't work. However, killing the mongodb service, and then manually running mongod --bind_ip 10.132.0.3 actually allowed my two instances to connect.
Which.. means that javadude's reply in the other thread saying "But I still dont get it. Why this did not work when I modified mongo.conf file to accept bind_ip 0.0.0.0"` also goes for me. I don't get it, but, it definitely worked to manually give it an ip-address to listen on.
I really tried, even reinstall the MongoDB.
And it's the same to MongoDB bind_ip error: bind() failed errno:99 Cannot assign requested address for socket
It works if set bind_ip to: 0.0.0.0, or 127.0.0.1
$ sudo service mongod start
mongod start/running, process 30040
$ sudo service mongod restart
mongod stop/waiting
mongod start/running, process 29704
$ mongo --port 19708
MongoDB shell version: 3.0.3
connecting to: 127.0.0.1:19708/test
>
It won't work if set bing_ip to: 127.0.0.1,192.118.96.10,42.112.36.110
$ sudo service mongod start
mongod start/running, process 29969
$ sudo service mongod restart
stop: Unknown instance:
mongod start/running, process 29766
$ mongo --port 19708
MongoDB shell version: 3.0.3
connecting to: 127.0.0.1:19708/test
2015-06-17T06:32:34.625+0000 W NETWORK Failed to connect to 127.0.0.1:19708
reason: errno:111 Connection refused
2015-06-17T06:32:34.627+0000 E QUERY
Error: couldn't connect to server 127.0.0.1:19708 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
locations:
$ which mongod
/usr/bin/mongod
$ which mongo
/usr/bin/mongo
configurations in /etc/mongod.conf
dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongod.log
logappend=true
port = 19708
# ips, eg:
# private ip for mongodb server: 192.118.96.10
# public ip for remote app server: 42.112.36.110
bind_ip = 127.0.0.1,192.118.96.10,42.112.36.110
auth = true
Thanks in advance.
Edit: I do not know wether I was simply wrong with my answer or if the behavior of bind_ip was changed, but it is possible to bind to multiple, distinct IPs
bind_ip:127.0.0.1,10.0.0.1,8.8.8.8
So, most likely, one of the IP addresses mongod was assigned to bind to did not exist on the machine in question.
You can bind mongod only to one IP, with 0.0.0.0 being the alias for "listen on all available network interfaces".
So either use
bind_ip=127.0.0.1
to listen to the loop back interface or
bind_ip=<someIP>
to listen to that IP only or
bind_ip=0.0.0.0
to listen to all available IPs on the system.
If you need to listen to several specific IPs, it is very likely that your system design is somehow screwed.
I had the same issue just because of the silly mistake.
There was commented line and space problem.
What I did wrong
# network interfaces
net:
port: 27017
#bindIp: 127.0.0.1
bindIp: privateIp
instead of
net:
port: 27017
bindIp: 10.1.2.4
for bind to multiples ips
bindIp: [127.0.0.1,10.128.0.2]
hopefully this answer helpful for someone.
Mongo 3.6.2 Community
The solution for me was to edit the section of /etc/mongod.conf
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,192.168.1.240 # No brackets, No spaces, only comma separated
#security
Then save and do this to restart and verify the service:
> service mongod restart
> service mongod status
No failure here, now verify that someone is listening:
> netstat -a |grep :27017
tcp 0 0 yourhostname:27017 0.0.0.0:* LISTEN
tcp 0 0 localhost:27017 0.0.0.0:* LISTEN
Now connect using your favorite Mongo tools or command line.
Some results of different formatting in /etc/mongod.conf
comma and space results in only the first IP being bound.
space only separator results in only the first IP being bound
[ ] surround results in failure to start mongod
I spent hours beating my head against a wall with this issue. Eventually, looking at logs and googling what I found THERE got me somewhere (all I got when googling 'mongo bindIp multipl' (etc) was a load of pages like this one with answers that didn't help).
First, the block in /etc/mongod.conf that worked for me was:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,172.16.1.2
No spaces, no quotes, no brackets... but even with it correct restarting mongodb gave an error and then it refused to start. I spent hours trying various other configurations that were incorrect (which is frustrating since the correctness of this line did not actually solve the problem and I was unaware that there was another).
I was able to solve it by deleting the mongodb socket file:
rm /etc/mongodb-27017.sock
After this, running
systemctl restart mongod
worked without errors. The interesting thing (part of what made it really frustrating) was that during the trial and error process if I set the bindIP back to just 127.0.0.1 and restarted mongod it worked, which made me think that that line was ok and the problems were with the alternative entries/syntax I was trying. (My best guess is that something in the socket file references the ips? I'm unfamiliar with that element of coding.)
After deleting the socket I was then able to shell into mongo like so (options required with authentication enabled):
mongo -u admin -p password --authenticationDatabase "admin")
which establishes that the 127.0.0.1 works and also to connect from my remote app (in my current scenario the nodebb testing instance I am setting up).
In my case, none of the options above worked. The specification that finally worked for me was:
bind_ip= [<IP_one> <IP_two>]
An example could be:
bind_ip= [127.0.0.1 10.0.0.4]
(Notice that there is no comma between the two directions)
I have MongoDB 2.6.10 on Ubuntu 16.4.5 (LTS)
The documentation said
"You may concatenate a list of comma separated values to bind mongod to multiple IP addresses."
So, it's not true...
I had same problem with AWS EC2 Ubuntu 20. This problem got solved by placing Private IPv4 addresses instead of Public IPv4 address.
Hey I am using MongoDB shell version: 3.2.6 .
bindIP = 127.0.0.1,192.168.0.50
bindIP = [127.0.0.1,192.168.0.50]
bind_ip = 192.168.6.38
not working , i have edited mongod.conf using this command
sudo -i gedit /etc/mongod.conf file
And Changed bind_ip = 0.0.0.0 to bind_ip = 192.168.6.38 it working fine ....
Using MongoDB 4.0.5:
For me the following worked:
bindIp=[127.0.0.1,11.22.33.44]
And then I switched on Auth:
security:
authorization: "enabled"
Restarting mongod (on Ubuntu 18.04) gave an error:
Scalar option 'net.bindIp' must be a single value
Therefore I had to do the bindIp: 0.0.0.0.
Maybe somebody has a clue how to combine bindIp and Security
This is a notation that only works on Ubuntu: (watch out on spaces, symbols)
bind_ip=[127.0.0.1,22.33.44.99,88.77.55.66]
22.33.44.99 - my static ip of server, 88.77.55.66 - my static ip of laptop. That gave me an opportunity to access to mongodb out from internet. Don't forget to add a rule - open port 27017 to ufw.
work for me for ubuntu 18 and the mongo --version 4.x.xx:
1 - in etc/mongod.conf -net add
bindIp: "127.0.0.1,0.0.0.0"
2 - then use pm2:
sudo apt-get update
sudo apt-get pm2
3 - start the pm2 service to the mongod
pm2 start mongod
PD: you need to erase 0.0.0.0 in production scenario
What got me is that I thought that I needed to list IP addresses of other machines that could access the DB. This is not the case. You just need to list IP addresses of the serer that runs mongoDB. For me after changing from:
bindIp: localhost
To:
bindIp: localhost,127.0.0.1,<actual IP of my server>
Then I could access MongoDB from other machines in my network. I just wasn't thinking about this the right way. Posting this here in case others have the same brain fart as me.
In my case
OS: Windows 8 (MongoDB) OS: Raspbian Linux (Client)
Tried all methods, below configuration worked for me
Updating mongod.conf file as
bindIp: 0.0.0.0
Opening firewall port in DB machine windows, don't skip this.
https://www.windowscentral.com/how-open-port-windows-firewall
work for me for ubuntu 18 and the mongo --version 4.x.xx:
1 - in etc/mongod.conf -net add
bindIp: "127.0.0.1,0.0.0.0"
2 - then use pm2:
sudo apt-get update
sudo apt-get pm2
3 - start the pm2 service to the mongod
pm2 start mongod
PD: you need to erase 0.0.0.0 in production scenario
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.
I'm trying to install MongoDB on my Ubuntu 11.04 AMI in Amazon EC2.
I allowed 27017 port in security option.
The problem is that I can use "mongo OTHER IP ADDRESS" to access to other mongoDB.
But simply "mongo" does not simply give me access to localhost mongoDB.
mongoDB shell shows like it is about to work but it stops at
MongoDB shell version: 1.8.3
connecting to: test
If it is truly connected, it should give me ">" but it doesn't and freezes.
I couldn't find answer on freezing problem by googling myself.
Also any tips on running mongoDB as daemon or running in background?
Be sure to use 0.0.0.0 as the IP in the security group. This will include your public IP and localhost IP. If this is not the issue, try this for debugging.
See what interface or IP range mongo is listening on:
netstat -pan | grep mongod
I imagine that it's listening on your public IP address (OTHER IP ADDRESS as you call it). If this is the case, try starting mongod without bind_ip. If you didn't specify the bind_ip argument then it's probably something related to EC2.
Another test:
Try telnet localhost 27017 (hit Ctrl-], type quit to quit). This should also fail because this is the same thing the mongo client is doing.