Related
I have installed MongoDb 4.0 on Windows 10. MongoDB currently runs on 'localhost'. I am able to connect to MongoDB by using 'mongodb://localhost:27017' in my nodeJS. However, I have a need to access this MondoDB instance through IP address. So, if I access MondoDB using mongodb://193.168.16.1:27017, then I am not able to connect to it. I tried to connect through MongoDB atlas, but I am having the same problem.
I have gone through the following threads on stackoverflow before posting it here.
Bind MongoDB to IP address when running as Service
Windows mongodb server bind_ip configuration
I have already tried to change the bindIp in mongod.cfg with the following values, with no success. I restarted 'MongoDB server' service after making these changes.I have also enabled all connections on port 27017 in windows firewall
bindIpAll: true #no success
bindIp: 127.0.0.1,193.168.16.1 #no success
bindIp: 193.168.16.1 #no success
bindIp: 0.0.0.0 #no success
I think you need to start your mongodb server something like this,
mongod --bind_ip 0.0.0.0 -v
And now you will be able to connect to mongodb using ipAddress.
Firstly please kill MongoDB and run this:
mongod --bind_ip 0.0.0.0 -v
Then as you want to connect through IP so run this command:
mongo --host 193.168.16.1 --verbose
Hope this will solve your problem...
None of the above suggestions worked for me. One of the combinations that worked for me is as follows
bindIp: 0.0.0.0,192.168.16.1
I am not sure why other combinations are not working. It could possibly be due to some issue with the MogoDb service, because I had to restart the service twice to get it working.
For me, it was not reading the config file until I removed the service and created it again specifying the configuration file path.
mongod --remove
"C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg" --service
I had to comment #mp: at the end of the configuration file before I could run the service.
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 one amazon linux server where someone installed mongodb.
So netstat -tulpn is showing 2 ports open for mongod process one is default 27017 and other is 37017 but i am not getting in which file he mentioned these ports, My /etc/mongod.conf showing only default port 27017 only.
Any suggestion please...
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
I am a newbie for setting up the server environment and mongoDB. This may sounds something really simple, however, I really need your help on it.
I am trying to connect to my virtual machine which runs the mongodb instance from the local machine, which I believe should be similar to the production environment when I run it on a separate remote server.
The environment is as following:
Private IP for virtual machine: 192.168.184.155
Public IP for both local machine and virtual machine: 96.88.169.145
I changed the bind_ip in /etc/mongod.conf file from
bind_ip = 127.0.0.1
to
bind_ip = 127.0.0.1,192.168.184.155,96.88.169.145
After I restarted the mongod service, neither the virtual machine nor the local machine can access mongodb through mongodb command and giving me the following error.
MongoDB shell version: 3.0.1
connecting to: test
2015-03-17T16:02:22.705-0400 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-03-17T16:02:22.707-0400 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:179:14)
at (connect):1:6 at src/mongo/shell/mongo.js:179
exception: connect failed
However, if I change the
bind_ip = 192.168.184.155
and restart the service, it works and I can access using mongo from my local machine. It seems just not work with multiple ip addresses. I tried to do look up in the mongodb document, however, they does mention that bind_ip takes a comma separated list, which really confused me.
Thanks for your help in advance.
Wrap the comma-separated-Ips with brackets works in mongo 3.2.7 for me:
bindIp = [127.0.0.1, 192.168.184.155, 96.88.169.145]
With the following version of MongoDB:
MongoDB shell version v3.6.10
Reproducing Problem:
When [127.0.0.1,xxx.xxx.xxx.xxx] is used we get the following error.
Scalar option 'net.bindIp' must be a single value
try 'mongod --help' for more information
Analysis
This is because, according to MongoDB Documentation: https://docs.mongodb.com/manual/reference/configuration-options/#net.bindIP
net.bindIP is of type "string".
Solution for binding multiple IP Addresses
bindIp: "127.0.0.1,xxx.xxx.xxx.xxx"
Note: No spaces after commas
You can do that by:
bindIp: [172.31.60.184,127.0.0.1]
Remember to not put a space after the comma.
The default /etc/mongod.conf file has this entry for network interfaces:
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
This corresponds to the --bind-ip arg parameter on the command line. Notice the dash notation converted to camel case. To expose the database running in a VM to the host, it's simplest to bind the database to all interfaces for testing purposes. This will cover situations where the VM is using a NAT interface or bridged interface. The command line parameter is --bind_ip_all which does not have a parameter, it's existence corresponds to the bindIPAll: true parameter in the configuration file. Therefore, the configuration file should be changed to:
# network interfaces
net:
port: 27017
bindIpAll: true
Notice the bindIp parameter is removed.
It should also be noted that it is possible to add parameters to the command line that override parameters in the config file. Normally, mongod is run with the --config /etc/mongod.conf parameter. It's easier to change the config file instead of hunting down the script what runs it as a daemon and getting systemd to reload and use it.
For details on the YAML-ness of the config file vs. parameters on the command line, see the documentation currently at https://docs.mongodb.com/manual/reference/configuration-options/
If you are running mongod on a VM using NAT, the default port 27017 will not be exposed to the host. It will be necessary to map a port on the host to this port in the guest VM. That's beyond the scope of this answer, but a little research will provide a method used for the VM software you are using (VirtualBox, VMware, etc.)
In my case, with mongoDB 4.4.6 following solution worked -
net:
port: 27017
bindIp: 127.0.0.1;<private_host_ip>
After making changes, restart mongoDB with following command -
sudo systemctl restart mongod
To Check mongoDB status, run -
sudo systemctl status mongod
Thanks for #wdberkeley and #anhlc brought up the clue.
I looked at the log file under /var/log/mongodb/mongod.log. It shows the failing reason for the problem.
2015-03-17T16:08:17.274-0400 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1, 192.168.184.155" }, storage: { dbPath: "/var/lib/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2015-03-17T16:08:17.332-0400 I NETWORK [initandlisten] getaddrinfo(" 192.168.184.155") failed: Name or service not known
So the mongo.conf file is sensitive to space, which I happened to add and should have noticed. Also just as #anhlc pointed out, 96.88.169.145 is not a valid IP address for VM. So that one also contribute to the error.
Great thanks for both of your help! Hope this may help if someone happened to run into the same problem.
I successfully added a second ip on my version 3.2 service using a comma, no spaces and an FQDN
net:
port: 27017
bindIp: localhost,dev-2.office.sampleorg.com
I am running 3.6 on SUSE 12.x and had an issues using comma separated IP lists. I fixed the issue by
bindIp: 0.0.0.0
In my case the solution was to put the comma separated IP and without any spaces.
bind_ip=192.168.2.29,127.0.0.1
#port = 27017
That way worked:
2018-10-02T07:49:27.952+0000 I CONTROL [initandlisten] options: { config: "/etc/mongodb.conf", net: { bindIp: "192.168.2.29,127.0.0.1", unixDomainSocket: { pathPrefix: "/run/mongodb" } }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongodb.log" } } 2018-10-02T07:49:27.954+0000 I - [initandlisten] Detected data files in /var/lib/mongodb created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
Mongo 3.6.3 here.
mongod --version
db version v3.6.3
git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
OpenSSL version: OpenSSL 1.1.0g 2 Nov 2017
allocator: tcmalloc
modules: none
build environment:
distarch: x86_64
target_arch: x86_64
MongoDB 6.0.1
bindIp: 127.0.0.1;192.168.1.0
After change just restart mongodb server:
sudo systemctl restart mongod
In Mongo 3.*,
use a bracket such as
net:
port: 27017
bindIp : [127.0.0.1,10.0.0.2,10.0.0.3]
The case in mongodb version 3.6 on my Ubuntu16.04 LTS is that you do not need to put the IP addresses in the square brackets "[]". Delete the space after the comma solve the failed connection problem in the mongod log (/var/log/mongodb/mongod.log)
NETWORK [initandlisten] getaddrinfo(" xxx.xxx.xxx.xxx") failed: Name or service not known
After modify the bindIp: 127.0.0.1 to bindIp: 127.0.0.1,xxx.xxx.xxx.xxx (notice no comma between IPs), the host IP is listening as below:
xxx#xxxx:/var/log/mongodb$ sudo netstat -plnt |egrep mongod
tcp 0 0 xxx.xxx.xxx.xxx:27017 0.0.0.0:* LISTEN 30747/mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 30747/mongod
The bindIp rule with comma separated IP addresses is only meant for known ethernet interfaces, or for the 0.0.0.0 and 127.0.0.1 rules. You can only use the ip addresses of your system's Network Interface Cards. If this list contains an unknown IP address, i.e. an IP of another system, your MongoDb instance will not restart and hence you're not able to connect. To check if your MongoDb instance is running, run the following:
$ sudo systemctl status mongod
If you would like to make your MongoDb instance available to other systems on your network, you'll want to bind the local IP associated with the private network. To determine network interface configuration easily, just run an ifconfig from the command line:
$ ifconfig
This will list all available IP addresses that you can use in the bindIp rule. Most certainly, the IP address 96.88.169.145 that you use is not a valid IP address or unknown by your system.
Amazon-linux2 | Cent OS | MongoDB shell version: 3.0.15
This is how it needs to be done in the latest config
#network interfaces
net:
port: 27017
bindIp: [xxx.xxx.xxx.xxx,127.0.0.1] # Listen to local interface only, comment to listen on all interfaces.
Cheers!!
For 4.4.6, so - lets assume for series 4.x (perhaps for some of series 3.x)
The correct syntax is:
bindIp: 127.0.0.1,<private_host_ip>,<public_host_ip>
For each new IP just use , and without any spaces
For LAN access must be used the private_host_ip, you can use ipconfig or ip a for Windows and Linux respectively to know the ip value.
Of course for public WAN access must be used <public_host_ip>, it about your ISP
Warning both ips - private_host_ip and <public_host_ip> - are the locations where your MongoDB server is running, not the remote clients. Therefore exists the wrong assumption to add each new ip where each one represents the remote client IP address (other different than the server IP address - LAN and/or WAN), it is not correct.
Note To avoid problems is better for both use static ips, the private through your router and the second through your ISP.
Ubuntu 16.04 -> MongoDB shell version: 2.6.10
For me the following works:
bind_ip = [127.0.0.1;X.X.X.X]
Notice I have a ; not ,
With MongoDB server version: 3.6.8 plain comma separated list worked for me. With or without spaces after comma.
bind_ip = 127.0.0.1, 192.168.0.104
bind_ip = 127.0.0.1,192.168.0.104
However, adding enclosing in [] fails with following error in MongoDB log
bind_ip = [127.0.0.1,192.168.0.104]
getaddrinfo("[127.0.0.1") failed: Name or service not known
bind_ip = [127.0.0.1, 192.168.0.104]
getaddrinfo("[127.0.0.1") failed: Name or service not known
This worked for my mongo v4.2.15:
vi /etc/mongod.conf
net:
port: 27017
bindIp: 127.0.0.1;X.X.X.X;Y.Y.Y.Y
sudo systemctl restart mongod
If all you want to do is connect to this machine over the network you do NOT need to modify the bind_ip value.
In your case you need to follow the following steps.
Setup the remote machine to block all connections to port 27017
Enable remote machine to only accept connections from your local machine
Setup credentials with MongoDB
Connect with client using credentials.
If you are not sure how to do any of this steps. Check out a blog post that I wrote that goes more in details how to do this.
Blog Post
Hope this helps.