除非设置为 0.0.0.0,否则 MongoDB bind_ip 将无法工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30884021/
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
MongoDB bind_ip won't work unless set to 0.0.0.0
提问by banruosheng
I really tried, even reinstall the MongoDB.
我真的尝试过,甚至重新安装了MongoDB。
And it's the same to MongoDB bind_ip error: bind() failed errno:99 Cannot assign requested address for socket
和MongoDB bind_ip 错误相同:bind() failed errno:99 无法为套接字分配请求的地址
It works if set bind_ip to: 0.0.0.0, or 127.0.0.1
如果将 bind_ip 设置为:0.0.0.0 或 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
如果设置bing_ip
为: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
/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.
提前致谢。
回答by Markus W Mahlberg
Edit:I do not know wether I was simply wrong with my answer or if the behavior of bind_ip
was changed, but it ispossible to bind to multiple, distinct IPs
编辑:我不知道我阉了根本不对我的回答,或者如果行为bind_ip
改变了,但它是可以绑定到多个不同的IP地址
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.
因此,最有可能的是,mongod 分配给绑定的 IP 地址之一在相关机器上不存在。
You can bind mongod
only to one IP, with 0.0.0.0
being the alias for "listen on all available network interfaces".
您只能绑定mongod
到一个 IP,它0.0.0.0
是“侦听所有可用网络接口”的别名。
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
只收听该 IP或
bind_ip=0.0.0.0
to listen to allavailable IPs on the system.
侦听系统上所有可用的 IP。
If you need to listen to several specific IPs, it is very likely that your system design is somehow screwed.
如果您需要监听多个特定的 IP,则您的系统设计很可能在某种程度上被搞砸了。
回答by Gabber
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
用于绑定到多个 ips
bindIp: [127.0.0.1,10.128.0.2]
hopefully this answer helpful for someone.
希望这个答案对某人有帮助。
回答by John DiNapoli
Mongo 3.6.2 Community
Mongo 3.6.2 社区
The solution for me was to edit the section of /etc/mongod.conf
我的解决方案是编辑 /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.
现在使用您最喜欢的 Mongo 工具或命令行进行连接。
Some results of different formatting in /etc/mongod.conf
/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
- 逗号和空格会导致仅绑定第一个 IP。
- 仅空格分隔符导致仅绑定第一个 IP
- [] 环绕导致无法启动 mongod
回答by Teoti Nathan'El
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:
我花了几个小时来解决这个问题。最终,查看日志并使用谷歌搜索我在那里找到的东西让我找到了某个地方(在谷歌搜索“mongo bindIp multipl”(等)时我得到的只是一堆像这样的页面,其中的答案没有帮助)。首先,/etc/mongod.conf 中对我有用的块是:
# 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).
没有空格,没有引号,没有括号……但即使正确重启 mongodb 也会出错,然后它拒绝启动。我花了几个小时尝试各种其他不正确的配置(这令人沮丧,因为这条线的正确性实际上并没有解决问题,我不知道还有另一个)。
I was able to solve it by deleting the mongodb socket file:
我能够通过删除 mongodb 套接字文件来解决它:
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.)
工作没有错误。有趣的事情(部分让它变得非常令人沮丧)是,在试错过程中,如果我将 bindIP 设置回 127.0.0.1 并重新启动 mongod 它工作,这让我认为那条线没问题,问题是使用我正在尝试的替代条目/语法。(我最好的猜测是套接字文件中的某些内容引用了 ips?我不熟悉该编码元素。)
After deleting the socket I was then able to shell into mongo like so (options required with authentication enabled):
删除套接字后,我可以像这样进入 mongo(启用身份验证所需的选项):
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).
它确定 127.0.0.1 可以工作,并且还可以从我的远程应用程序连接(在我当前的场景中,我正在设置 nodebb 测试实例)。
回答by Shodan
The documentation said
文档说
"You may concatenate a list of comma separated values to bind mongod to multiple IP addresses."
“您可以连接逗号分隔值列表,以将 mongod 绑定到多个 IP 地址。”
So, it's not true...
所以,这不是真的……
回答by Andrea Morales Garzón
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)
我在 Ubuntu 16.4.5 (LTS) 上有 MongoDB 2.6.10
回答by Andriy Balitskyy
This is a notation that only works on Ubuntu: (watch out on spaces, symbols)
这是一个仅适用于 Ubuntu 的符号:(注意空格、符号)
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.
22.33.44.99 - 我的服务器静态 IP,88.77.55.66 - 我的笔记本电脑静态 IP。这让我有机会从互联网访问 mongodb。不要忘记添加规则 - 向 ufw 开放端口 27017。
回答by Dimitar Atanasov
For those who still wondering - problem is not in the syntax, but the addressesyou put in.
对于那些仍然想知道的人 -问题不在于语法,而在于您输入的地址。
In order to receive the remote client connections you need to add the server public IPas well. So you must add:
为了接收远程客户端连接,您还需要添加服务器公共 IP。所以你必须添加:
- localhost
- 127.0.0.1 // Add both localhost and 127.0.0.1 to ensure local accessibility
- server_public_ip // This is important one. Add the public server IP address.
- remote_client_ip1
- remote_client_ip2
- remote_client_ip3 // As many client IPs as you want to grant access to
So the configuration should look like this:
所以配置应该是这样的:
bind_ip=[localhost,127.0.0.1,server_public_ip,remote_client_ip]
bind_ip=[localhost,127.0.0.1,server_public_ip,remote_client_ip]
where we put both 127.0.0.1 and localhost to ensure local accessibility, because different configurations can work with only one of the settings.
我们同时放置 127.0.0.1 和 localhost 以确保本地可访问性,因为不同的配置只能使用其中一种设置。
Note:bind_ip
accepts multiple syntaxes. If you have syntax error mongodb service will not run. You can check it with service mongodb status
.
注意:bind_ip
接受多种语法。如果您有语法错误 mongodb 服务将不会运行。您可以使用service mongodb status
.
DON'Tever place 0.0.0.0 as you can be a victim of one of the regular RANSOME attacks happening every few months. Read about it
永远不要放置 0.0.0.0,因为您可能成为每隔几个月发生的常规 RANSOME 攻击之一的受害者。阅读它
回答by osiris rodriguez
work for me for ubuntu 18 and the mongo --version 4.x.xx:
对我来说有效 ubuntu 18 和 mongo --version 4.x.xx:
1 - in etc/mongod.conf -net add
1 - 在 etc/mongod.conf -net 添加
bindIp: "127.0.0.1,0.0.0.0"
bindIp:“127.0.0.1,0.0.0.0”
2 - then use pm2:
2 - 然后使用 pm2:
sudo apt-get update sudo apt-get pm2
须藤 apt-get 更新须藤 apt-get pm2
3 - start the pm2 service to the mongod
3 - 启动 pm2 服务到 mongod
pm2 start mongod
pm2 启动 mongod
PD: you need to erase 0.0.0.0 in production scenario
PD:生产场景需要擦除0.0.0.0
回答by osiris rodriguez
work for me for ubuntu 18 and the mongo --version 4.x.xx:
对我来说有效 ubuntu 18 和 mongo --version 4.x.xx:
1 - in etc/mongod.conf -net add
1 - 在 etc/mongod.conf -net 添加
bindIp: "127.0.0.1,0.0.0.0"
bindIp:“127.0.0.1,0.0.0.0”
2 - then use pm2:
2 - 然后使用 pm2:
sudo apt-get update
sudo apt-get pm2
3 - start the pm2 service to the mongod
3 - 启动 pm2 服务到 mongod
pm2 start mongod
PD: you need to erase 0.0.0.0 in production scenario
PD:生产场景需要擦除0.0.0.0