44

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.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
banruosheng
  • 483
  • 2
  • 6
  • 8
  • 1
    I can't get either of these bind options to work, spaces, no spaces, commas, brackets etc – Tony Mar 13 '18 at 18:45
  • None of the solutions below at the time of posting worked for me. I don't see why it works for 127.0.0.1 or 0.0.0.0, but if I set it to the IP address of the second machine on the LAN mongod fails to start. – John Forbes Nov 06 '19 at 05:18
  • Consider the following answer - https://stackoverflow.com/a/67605000/3665178 – Manuel Jordan May 19 '21 at 22:28

14 Answers14

44

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.

Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
  • 4
    It is generally a wrong idea to accept connections from 0.0.0.0 – Dushan Savich Feb 18 '17 at 00:00
  • 1
    @DushanSavich I dare to object. For large clusters, in which dynamically assigned IP addresses might be the tool of choice (setting up the machines with DHCP, BootP and Kickstart for example), it might well become reasonable to listen to 0.0.0.0, as the systems IP address would be indeterminable while writing the setup. While I agree it is important to be very careful when listening on 0.0.0.0, I can not agree that this is a bad thing per se. – Markus W Mahlberg Feb 19 '17 at 16:54
  • I would also disagree. I do want my remote VPN development machines to be able to connect on the VPN interface, and I do want localhost to connect as well. I do not want it to listen on the internet-connected interface. Is my (admittedly very compact) system design screwed? – Ian Gibbs Mar 13 '17 at 08:00
  • @IanGibbs: I do not see the point of development machines connecting to a central instance, given the fact that setting up MongoDB is no rocket science, much less when using docker or some virtual machine. – Markus W Mahlberg Mar 13 '17 at 10:02
  • OK, call them admin machines instead. – Ian Gibbs Mar 13 '17 at 17:06
  • @IanGibbs Which should be using ssh over VPN to a jump server (can be a very small VM) in the same subnet as the cluster and do the admin from there. As per the systems design: The cluster itself should be in a private subnet reachable only for the application servers and the jump server from the internet side. This gives you number of application servers +1 nodes to administrate the cluster without the need to use a potentially dangerous configuration should the network change. – Markus W Mahlberg Mar 15 '17 at 08:07
  • 1
    I would recommend to set `bindIpAll: true` Using `bindIp` with an IP address is useful when you have a machine with several network interfaces, otherwise it does not make much sense in my opinion. Exception is `bindIp: localhost` when you like to prevent any external connection, typically used while maintenance work. – Wernfried Domscheit Dec 02 '22 at 07:35
  • 1
    If you want to protect your MongoDB from "bad" connections, then you have to setup a firewall, not by setting `bindIp` – Wernfried Domscheit Dec 02 '22 at 07:38
  • @WernfriedDomscheit That depends on the general system setup, but as a rule of thumb, I agree. – Markus W Mahlberg Dec 04 '22 at 06:49
16

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.

Gabber
  • 7,169
  • 3
  • 32
  • 46
7

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
John DiNapoli
  • 71
  • 1
  • 2
5

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).

5

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)

jjmerelo
  • 22,578
  • 8
  • 40
  • 86
2

The documentation said

"You may concatenate a list of comma separated values to bind mongod to multiple IP addresses."

So, it's not true...

Shodan
  • 21
  • 1
1

I had same problem with AWS EC2 Ubuntu 20. This problem got solved by placing Private IPv4 addresses instead of Public IPv4 address.

0

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 ....

Raja Rama Mohan Thavalam
  • 8,131
  • 2
  • 31
  • 30
0

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

BertC
  • 2,243
  • 26
  • 33
0

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.

0

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

0

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.

Kirk Liemohn
  • 7,733
  • 9
  • 46
  • 57
0

In my case

OS: Windows 8 (MongoDB) OS: Raspbian Linux (Client)

Tried all methods, below configuration worked for me

  1. Updating mongod.conf file as

    bindIp: 0.0.0.0
    
  2. Opening firewall port in DB machine windows, don't skip this.

     https://www.windowscentral.com/how-open-port-windows-firewall
    
Alpesh Patil
  • 1,672
  • 12
  • 15
-1

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