mongodb 如何使用多个IP地址设置mongod.conf bind_ip
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29109134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set mongod.conf bind_ip with multiple ip address
提问by yeelan
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.
我是设置服务器环境和 mongoDB 的新手。这听起来可能很简单,但是,我真的需要你的帮助。
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.
我正在尝试连接到我的虚拟机,该虚拟机从本地机器运行 mongodb 实例,我认为当我在单独的远程服务器上运行它时,它应该类似于生产环境。
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
虚拟机私有IP:192.168.184.155
本地机和虚拟机的公网IP:96.88.169.145
I changed the bind_ipin /etc/mongod.conf filefrom
我改变了bind_ip在/etc/mongod.conf文件从
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.
我重启mongod服务后,无论是虚拟机还是本地机都无法通过mongodb命令访问mongodb,报错如下。
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.
并重新启动服务,它可以工作,我可以从本地机器使用 mongo 进行访问。它似乎不适用于多个 IP 地址。我试图在 mongodb 文档中查找,但是,他们确实提到 bind_ip 需要一个逗号分隔的列表,这让我很困惑。
Thanks for your help in advance.
提前感谢您的帮助。
回答by Joe Cheng
Wrap the comma-separated-Ips with brackets works in mongo 3.2.7 for me:
用括号包裹逗号分隔的 Ips 对我来说适用于 mongo 3.2.7:
bindIp = [127.0.0.1, 192.168.184.155, 96.88.169.145]
回答by pungoyal
You can do that by:
你可以这样做:
bindIp: [172.31.60.184,127.0.0.1]
Remember to not put a space after the comma.
记住不要在逗号后面加空格。
回答by Sachin PC
With the following version of MongoDB: MongoDB shell version v3.6.10
使用以下版本的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
重现问题:当使用 [127.0.0.1,xxx.xxx.xxx.xxx] 时,我们得到以下错误。标量选项 'net.bindIp' 必须是单个值,请尝试 'mongod --help' 以获取更多信息
AnalysisThis is because, according to MongoDB Documentation: https://docs.mongodb.com/manual/reference/configuration-options/#net.bindIP
分析这是因为,根据 MongoDB 文档:https://docs.mongodb.com/manual/reference/configuration-options/#net.bindIP
net.bindIP is of type "string".
net.bindIP 是“字符串”类型。
Solution for binding multiple IP Addresses
绑定多个IP地址的解决方案
bindIp: "127.0.0.1,xxx.xxx.xxx.xxx"
Note: No spaces after commas
注意:逗号后不能有空格
回答by yeelan
Thanks for @wdberkeley and @anhlc brought up the clue.
感谢@wdberkeley 和@anhlc 提供了线索。
I looked at the log file under /var/log/mongodb/mongod.log. It shows the failing reason for the problem.
我查看了 /var/log/mongodb/mongod.log 下的日志文件。它显示了问题的失败原因。
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.
所以 mongo.conf 文件对空间敏感,我碰巧添加了并且应该注意到。同样正如@anhlc 指出的那样,96.88.169.145 不是虚拟机的有效 IP 地址。所以这也导致了错误。
Great thanks for both of your help! Hope this may help if someone happened to run into the same problem.
非常感谢您的帮助!如果有人碰巧遇到同样的问题,希望这可能会有所帮助。
回答by jprism
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
我在 SUSE 12.x 上运行 3.6 并且在使用逗号分隔的 IP 列表时遇到问题。我通过 bindIp 解决了这个问题:0.0.0.0
回答by KyungHoon Kim
In Mongo 3.*,
在 Mongo 3.* 中,
use a bracket such as
使用括号,例如
net:
port: 27017
bindIp : [127.0.0.1,10.0.0.2,10.0.0.3]
回答by Bruce Yo
The case in mongodb version 3.6on my Ubuntu16.04 LTS is that you do not need to put the IP addresses in the square brackets "[]". Delete the space after the commasolve the failed connection problem in the mongod log (/var/log/mongodb/mongod.log)
在我的 Ubuntu16.04 LTS 上mongodb 3.6 版的情况是您不需要将 IP 地址放在方括号“[]”中。删除逗号后的空格解决mongod日志(/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:
修改后,bindIp: 127.0.0.1
以bindIp: 127.0.0.1,xxx.xxx.xxx.xxx
(通知IP地址之间没有逗号),主机IP侦听如下:
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
回答by Gonzalo Aguilar Delgado
In my case the solution was to put the comma separated IP and without any spaces.
在我的情况下,解决方案是放置逗号分隔的 IP 并且没有任何空格。
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.
Mongo 3.6.3 在这里。
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
mongod --version db 版本 v3.6.3 git 版本:9586e557d54ef70f9ca4b43c26892cd55257e1a5 OpenSSL 版本:OpenSSL 1.1.0g 2017 年 11 月 2 日分配器:tcmalloc 模块:无目标构建环境:x686:x686
回答by Joshua Goldstein
I successfully added a second ip on my version 3.2 service using a comma, no spaces and an FQDN
我使用逗号、无空格和 FQDN 在我的 3.2 版服务上成功添加了第二个 ip
net:
port: 27017
bindIp: localhost,dev-2.office.sampleorg.com
回答by Kurt Van den Branden
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:
带有逗号分隔 IP 地址的 bindIp 规则仅适用于已知的以太网接口,或 0.0.0.0 和 127.0.0.1 规则。您只能使用系统网络接口卡的 IP 地址。如果此列表包含未知 IP 地址,即另一个系统的 IP,您的 MongoDb 实例将不会重新启动,因此您无法连接。要检查您的 MongoDb 实例是否正在运行,请运行以下命令:
$ 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:
如果您想让您的 MongoDb 实例可用于您网络上的其他系统,您需要绑定与专用网络关联的本地 IP。要轻松确定网络接口配置,只需从命令行运行 ifconfig:
$ 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.
这将列出您可以在 bindIp 规则中使用的所有可用 IP 地址。可以肯定的是,您使用的 IP 地址 96.88.169.145 不是有效的 IP 地址或您的系统未知。