IP BindingIP绑定

On this page本页内容

Overview概述

Starting in MongoDB 3.6, MongoDB binaries, mongod and mongos, bind to localhost by default.从MongoDB 3.6开始,MongoDB二进制文件,mongodmongos默认绑定到localhostIf the net.ipv6 configuration file setting or the --ipv6 command line option is set for the binary, the binary additionally binds to the localhost IPv6 address.如果net.ipv6如果为二进制文件设置了配置文件设置或--ipv6命令行选项,则二进制文件还绑定到localhost IPv6地址。

Considerations注意事项

Warning

Make sure that your mongod and mongos instances are only accessible on trusted networks.确保mongodmongos实例只能在受信任的网络上访问。If your system has more than one network interface, bind MongoDB programs to the private or internal network interface.如果您的系统有多个网络接口,请将MongoDB程序绑定到专用或内部网络接口。

To override and bind to other ip addresses, you can use the net.bindIp configuration file setting or the --bind_ip command-line option to specify a list of hostnames or ip addresses.要覆盖并绑定到其他ip地址,可以使用net.bindIp文件配置文件设置或--bind_ip命令行选项来指定主机名或ip地址的列表。

Warning

Before binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured your cluster from unauthorized access.在绑定到非本地主机(例如,公共访问)IP地址之前,请确保已保护集群不受未经授权的访问。For a complete list of security recommendations, see Security Checklist.有关安全建议的完整列表,请参阅安全性检查列表At minimum, consider enabling authentication and hardening network infrastructure.至少,请考虑启用身份验证强化网络基础设施

For example, the following mongod instance binds to both the localhost and the hostname My-Example-Associated-Hostname, which is associated with the ip address 198.51.100.1:例如,以下mongod实例绑定到localhost和主机名My-Example-Associated-Hostname,后者与ip地址198.51.100.1关联:

mongod --bind_ip localhost,My-Example-Associated-Hostname

In order to connect to this instance, remote clients must specify the hostname or its associated ip address 198.51.100.1:为了连接到此实例,远程客户端必须指定主机名或其关联的ip地址198.51.100.1

mongo --host My-Example-Associated-Hostname

mongo --host 198.51.100.1

To bind to all IPv4 addresses, you can specify the bind ip address of 0.0.0.0.要绑定到所有IPv4地址,可以将绑定ip地址指定为0.0.0.0To bind to all IPv4 and IPv6 addresses, you can specify the bind ip address of ::,0.0.0.0 or alternatively, use the new net.bindIpAll setting or the new command-line option --bind_ip_all.要绑定到所有IPv4和IPv6地址,可以指定绑定ip地址::,0.0.0.0,或者使用新的net.bindIpAll设置或新的命令行选项--bind_ip_all