How to set mongod.conf bind_ip with multiple ip ad

2019-01-23 10:55发布

问题:

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.

回答1:

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]


回答2:

You can do that by:

bindIp: [172.31.60.184,127.0.0.1]

Remember to not put a space after the comma.



回答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



回答4:

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.



回答5:

In Mongo 3.*,

use a bracket such as

net:
    port: 27017
    bindIp : [127.0.0.1,10.0.0.2,10.0.0.3]


回答6:

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 


回答7:

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


回答8:

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



回答9:

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 ,



回答10:

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.