当前位置:   article > 正文

MongoDB允许其它IP地址访问_mongo设置ip

mongo设置ip


Linux服务器上安装MongoDB后,允许其它客户端IP访问MongoDB服务器。

阿里云服务器需要设置入站规则,可参见使用外网访问阿里云服务器ZooKeeper

1.允许所有地址访问

NOTE:

net.bindIp and net.bindIpAll are mutually exclusive. That is, you can specify one or the other, but not both.
net.bindIpnet.bindIpAll是互斥的。也就是说,您可以指定其中一个,但不能同时指定两者。

可直接设置bindIpAll: true,即允许所有的IPv4和IPv6地址访问

# network interfaces
net:
  port: 27017
  bindIpAll: true
  • 1
  • 2
  • 3
  • 4

也可以根据后面注释提示,将bindIp修改为0.0.0.0,::

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0,::  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
  • 1
  • 2
  • 3
  • 4

2.绑定内网IP

阿里云服务器可以只绑定内网IP,阿里云服务器外网配置入站规则,由阿里云服务器的外网向内网转发。

配置主机名和内网IP的映射
sudo vi /etc/hosts,添加配置项192.168.1.200 solang

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.200  solang
  • 1
  • 2
  • 3
  • 4
[solang@solang ~]$ sudo vi /etc/mongod.conf
# network interfaces
net:
  port: 27017
  bindIp: solang  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
  • 1
  • 2
  • 3
  • 4
  • 5

启动MongoDB服务时,访问外网端口是通的,服务器上使用mongo shell时需要指定对应的地址,即mongo solang:27017

3.绑定多个IP

The hostnames and/or IP addresses and/or full Unix domain socket paths on which mongos and mongodshould listen for client connections. You may attach mongos and mongod to any interface. To bind to multiple addresses, enter a list of comma-separated values.
主机名和/或IP地址和/或完整的Unix域套接字路径,mongosmongod应该监听客户端连接。 您可以将mongosmongod附加到任何界面。 要绑定到多个地址,请输入逗号分隔值列表。

EXAMPLE

localhost,/tmp/mongod.sock
  • 1

MongoDB4.0修改bindIp值,只允许按照如上方式设置,即域名或IP,文本地址列表,文本地址列表可自定义名称,多个地址需要用逗号分隔。

配置主机名和内网IP的映射
sudo vi /etc/hosts,添加配置项192.168.1.200 solang

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.200  solang
  • 1
  • 2
  • 3
  • 4

例:

[solang@solang ~]$ vi /usr/local/app/mongod.socks

输公网IP

[solang@solang ~]$ sudo vi /etc/mongod.conf
# network interfaces
net:
  port: 27017
  bindIp: solang,/usr/local/app/mongod.socks  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

TIP

When possible, use a logical DNS hostname instead of an ip address, particularly when configuring replica set members or sharded cluster members. The use of logical DNS hostnames avoids configuration changes due to ip address changes.
如果可能,请使用逻辑DNS主机名而不是IP地址,尤其是在配置副本集成员或分片集群成员时。 逻辑DNS主机名的使用避免了由于IP地址更改而导致的配置更改。

本文参考:
https://docs.mongodb.com/manual/reference/configuration-options/#net-options

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/664791
推荐阅读
相关标签
  

闽ICP备14008679号