How to set mongod.conf bind_ip with multiple ip address
Asked Answered
G

21

58

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.

Gripper answered 17/3, 2015 at 20:32 Comment(7)
What if you use bind_ip = 127.0.0.1,192.168.184.155? To me 96.88.169.145 is not a valid IP on your VMYeld
@Yeld It's the same result. Once I have added more than one ip address, none of them works. Any ideas?Gripper
Can you get the parsed command line options from the log when you boot mongod? It really should work, so I wonder if there's some weird VM networking obstacle here?Carnahan
Hi @wdberkeley, thank you so much for bring it up! I checked the log file and find out the problem! It seems I happened to add a space before the next ip address, which crash the whole thing.Gripper
I agree with wdberkeley, it's always worth checking the log file, to see if there are any clues there about what is going wrong.Sandoval
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.Peipus
A public IP is typically achieved by your router using NAT, i.e. your local computer does not know this address and thus it does not make any sense to bind such an IP.Peipus
B
62

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]
Borecole answered 12/7, 2016 at 9:30 Comment(8)
Don't know why this was downvoted but this seems to be norm nowTarra
there shouldn't be the brackets or spaces after the commas anymore. it's also bind_ip instead of bindIp. this is for ubuntu might be different for other systems...Drift
Both joe-cheng's and @puneet-goyal's solutions work. With spaces brackets are needed, without spaces just a comma-separated list works.Tody
In my case it didn't work 3.6 here. It added bracked as part of the IP.Phiona
bindIp: 127.0.0.1, 10.8.0.1 # works for centos 7.4, mongo 3.6Suppression
I get: Jun 02 19:04:31 mongod[23698]: Scalar option 'net.bindIp' must be a single value Jun 02 19:04:31 mongod[23698]: try '/usr/bin/mongod --help' for more informationDecagon
I am using latest version. The above solution did not work for meCampus
none of these work on mongodb 6Blowfish
F
25

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

Fawne answered 14/2, 2019 at 17:56 Comment(4)
it worked for mongo 4.4.8 versionAboveboard
doesnt work for mongo 6.0Blowfish
still work for mongo 6.0Quinidine
Not works for MongoDB 7.0 (demands ; - semicolon divider)Complect
D
17

You can do that by:

bindIp: [172.31.60.184,127.0.0.1]

Remember to not put a space after the comma.

Dirt answered 6/4, 2016 at 11:50 Comment(3)
What version of MongoDb are you using? I'm trying with 2.6 and this does not seem to work (list with comma, no space) nor the version with brackets.Shunt
This does not work on db version v3.2.12 git version: ef3e1bc78e997f0d9f22f45aeb1d8e3b6ac14a14 Ubuntu 16.04. Using the brackets in answer below worksVite
for some reason this '[172.31.60.184,127.0.0.1]' does not work in version 3.6.8. what worked for me was modifying /etc/mongodb.conf and inserting: binding_ip = 172.31.60.184,localhost that will bind to the first ip sepecified and to the localhost.Allaround
M
8

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

Mattock answered 8/7, 2020 at 13:36 Comment(0)
R
8

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
Rincon answered 27/5, 2021 at 8:22 Comment(4)
This works with the config. but I am not able to run the mongo shell it is throwing error connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused : connect@src/mongo/shell/mongo.js:372:17Easiness
@KartikGarasia - Please send detailed error log, or may be screenshot of your mongo conf file.Rincon
This works for MongoDB shell version v4.0.28Conjoint
Doesn't appear to work with MongoDB shell version v4.4.9Kibbutznik
B
6

MongoDB 6.0.1

bindIp: 127.0.0.1;192.168.1.0

After change just restart mongodb server:

sudo systemctl restart mongod
Blowfish answered 28/9, 2022 at 10:28 Comment(2)
Didn't work for me using 6.0.0Cupro
as per documentation: "enter a list of comma-separated values". so replace semi-colon. (@Cupro try this way)Dregs
P
4

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

Phiona answered 2/10, 2018 at 7:52 Comment(0)
G
3

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.

Gripper answered 18/3, 2015 at 21:20 Comment(1)
You mean we should write something like this: bind_ip=127.0.0.1 192.168.184.155 96.88.169.145 i.e. space separated instead of comma separated?Turfy
C
3

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
Centesimal answered 11/8, 2017 at 17:22 Comment(0)
A
3

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

Authorship answered 3/11, 2017 at 22:46 Comment(7)
Thanks i was having same problem none of the above and below solution worked but yours +1Bonilla
0.0.0.0 will work because you aren't binding to any server which essentially binds it to your Public IP and can potentially expose your database to the public. Will work, but bad idea.Bose
Don't think so. 0.0.0.0 is a non-routable meta-address. In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine".Authorship
I still think it is a bit hacky. It is basically allowing any IP address that gains access to the local server. For 3.6, the safest way would be to bindIp: 127.0.0.1, xxx.xxx.xxx.xxx and under that bindIpAll: true <-- default value is false.Bose
Not a great idea, it will expose your database.Wooster
Not sure, how? can you give me an example?Authorship
Wow... don't do this ! It will let any IP address to access your DB.Michelmichelangelo
T
2

In Mongo 3.*,

use a bracket such as

net:
    port: 27017
    bindIp : [127.0.0.1,10.0.0.2,10.0.0.3]
Tyus answered 9/12, 2017 at 10:12 Comment(0)
C
1

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 
Colombes answered 2/6, 2018 at 3:19 Comment(0)
S
1

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 ,

Sloan answered 15/11, 2018 at 15:51 Comment(1)
Ubuntu 14 with mongo 2.6 also works like this. Thanks finezzaGaby
M
1

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.

Magnetize answered 26/5, 2019 at 11:9 Comment(0)
D
1

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

Dugger answered 6/7, 2020 at 12:27 Comment(1)
Error: Scalar option 'net.bindIp' must be a single valueFazio
J
1

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.

Juggernaut answered 19/5, 2021 at 14:14 Comment(0)
A
0

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
Allay answered 25/10, 2020 at 6:57 Comment(0)
G
0

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

Gogetter answered 2/8, 2022 at 1:41 Comment(0)
F
0

Here I resolve this issue using bellow code. my mongod version 4.4.21.

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1, <your_server_ip>
  bindIpAll: true

After Changes restart the mongod service

sudo service mongod restart

To verify the connection write bellow code in your local system terminal

nc -zv <your_server_ip> 27017

Output :

Connection to <your_server_ip> port 27017 [tcp/*] succeeded!
Floater answered 8/5, 2023 at 10:27 Comment(0)
H
0
net:
  port: 27017
  bindIp: 127.0.0.1;53.254.16.5

This for MongoDB 4.4, CentOS 7

Historic answered 21/3 at 8:51 Comment(0)
G
-11

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.

  1. Setup the remote machine to block all connections to port 27017
  2. Enable remote machine to only accept connections from your local machine
  3. Setup credentials with MongoDB
  4. 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.

Girard answered 27/1, 2016 at 18:6 Comment(5)
that doesn't make any sense at all.Puke
What part? He wants to access a machine from a public location simple networking.Girard
Make sure that if you downvote this answer you post why? youtu.be/Jsb-9iM_tiwGirard
i downvoted this answer because its wrong. An example why, consider a private network. Network security prevents unauthorized access to the mongo cluster. But the clients need to be bound to the private interface for it work. but, you in circumstance you may need to bind localhost as well for statistics, maintenance etc. bind all would be dangerous, even with credentials. my webservers connect to my mongo cluster over private network interface (local subnet) much safer than public internet.Puke
Your answer and blog post are not relevant to the OP's question - which was about issues with configuring bind_ip on a virtual machine.Sandoval

© 2022 - 2024 — McMap. All rights reserved.