MongoDB bindIp 與 bindIpAll



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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM