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.bindIp和net.bindIpAll是互斥的。也就是说,您可以指定其中一个,但不能同时指定两者。
可直接设置bindIpAll: true,即允许所有的IPv4和IPv6地址访问
# network interfaces
net:
  port: 27017
  bindIpAll: true
1234
也可以根据后面注释提示,将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.
1234

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
1234
[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.
12345
启动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域套接字路径,mongos和mongod应该监听客户端连接。 您可以将mongos和mongod附加到任何界面。 要绑定到多个地址,请输入逗号分隔值列表。
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
1234
例:
[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.
123456789
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
---------------------
版权声明:本文为CSDN博主「LifeIsForSharing」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sl1992/article/details/83964310
 posted on 2019-08-16 14:47  xibuhaohao  阅读(10512)  评论(0编辑  收藏  举报