问题描述
我无法理解 mongodb 中的 bind_ip
是什么.我可以通过 bind_ip = 0.0.0.0
建立从桌面到 EC2 机器的远程连接,但无法使用 bind_ip = 127.0.0.1
进行连接.
I could not understand what bind_ip
in mongodb is. I could make a remote connection from desktop to the EC2 machine by having bind_ip = 0.0.0.0
, but could not make it work with bind_ip = 127.0.0.1
.
请解释bind_ip是什么以及为什么它适用于0.0.0.0
而不适用于127.0.0.1
.
Please explain me what bind_ip is and why it works for 0.0.0.0
and not for 127.0.0.1
.
参考 mongodb 文档:
bind_ip
默认:所有接口.
设置此选项以配置 mongod 或 mongos 进程以绑定到并侦听来自此地址上的应用程序的连接.您可以将 mongod 或 mongos 实例附加到任何接口;然而,如果您将流程附加到可公开访问的界面,实施适当的身份验证或防火墙限制以保护数据库的完整性.
Set this option to configure the mongod or mongos process to bind to and listen for connections from applications on this address. You may attach mongod or mongos instances to any interface; however, if you attach the process to a publicly accessible interface, implement proper authentication or firewall restrictions to protect the integrity of your database.
您可以连接一个逗号分隔值列表来将 mongod 绑定到多个 IP 地址.
You may concatenate a list of comma separated values to bind mongod to multiple IP addresses.
推荐答案
在将您的服务器绑定到 0.0.0.0 之前,请清楚这些更改的安全含义:您的服务器将公开暴露给所有 IP整个互联网.请务必在您的服务器上启用身份验证!
当您将机器绑定到 EC2 上的 127.0.0.1
时,您将无法访问您的机器.这不是错误,而是由网络接口绑定引起的.
You can't access your machine when you bind it to 127.0.0.1
on EC2. That's not a bug, it's reasoned by the network interface bindings.
127.0.0.1
只会绑定到环回接口(所以你只能在本地访问它),而 0.0.0.0
会绑定到所有网络可用的接口.
127.0.0.1
will only bind to the loopback interface (so you will only be able to access it locally), while 0.0.0.0
will bind it to all network interfaces that are available.
这就是为什么当您将 mongodb 绑定到 0.0.0.0
(因为它现在可以通过 Internet 访问)而不是通过 127.0.0.1
时,您可以访问 EC2 上的 mongodb.
That's why you can access your mongodb on EC2 when you bind it to 0.0.0.0
(as it's available through the internet now) and not via 127.0.0.1
.
对于本地服务器(如 WAMP 或本地 mongodb 服务器),您看起来不会有什么不同,但对于这种情况,您还应该绑定到本地服务器的 0.0.0.0
让它们在所有网络接口上可用(如果没有防火墙,它可能对知道您 IP 的人公开!)
For local servers (like a WAMP or a local mongodb server) that won't look different to you, but for that case you should also thing that binding to 0.0.0.0
for local servers might make them available over all network interfaces (so it might be public for someone who knows your IP, if there is no firewall!)
阅读关于服务器故障的类似问题 在这里.
这篇关于mongodb.conf bind_ip = 127.0.0.1 不起作用但 0.0.0.0 起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!