当前位置:   article > 正文

mongodb允许远程访问

mongodb允许远程访问

在本教程中,将展示如何启用远程访问到MongoDB服务器。这是测试环境:

  1. MongoDB Server

    • 私有IP – 192.168.161.100
    • 公共IP – 45.56.65.100
    • MongoDB 2.6.3, port 27017
    • IpTables Firewall
  2. Application Server (Same LAN network)

    • 私有IP – 192.168.161.200
    • 公共IP – irrelevant
  3. Developers at home (Different LAN network, WAN)

    • 公共IP – 10.0.0.1

1. 绑定IP

  1. $ vim /etc/mongod.conf
  2. # /etc/mongod.conf
  3. # 只监听本地接口。注释掉听在所有接口。
  4. bind_ip = 127.0.0.1

默认情况下,MongoDB绑定到本地接口,它将限制远程连接。如果你不关心安全,只是注释掉接受任何远程连接(不推荐)。

1.1 从应用服务器允许局域网连接

因为都是在同一个局域网网络,你只需要将MongoDB绑定到它自己的私有IP接口。

  1. $ vim /etc/mongod.conf
  2. # /etc/mongod.conf
  3. # 监听本地和局域网接口。
  4. bind_ip = 127.0.0.1,192.168.161.100

常见的错误
不要把应用服务器IP在bind_ip选项。这bind_ip选项告诉MongoDB接受连接本地网络接口,而不是“远程IP地址”。

默认连接失败

AS (192.168.161.200) <-- LAN --> MongoDB(192.168.161.100) <--> bind_ip (127.0.0.1)

现在连接成功

AS (192.168.161.200) <-- LAN --> MongoDB(192.168.161.100) <--> bind_ip (192.168.161.100, 127.0.0.1)
1.2 允许开发人员远程访问

开发人员将通过MongoDB公共IP 45.56.65.100远程访问,允许,将公共IP绑定接口。

  1. $ vim /etc/mongod.conf
  2. # /etc/mongod.conf
  3. # 监听本地,局域网和公共接口。
  4. bind_ip = 127.0.0.1,192.168.161.100,45.56.65.100

提示
为开发人员在国内,建议建立VPN连接,而不是打开MongoDB公共IP连接,它是容易被攻击的。

重新启动MongoDB生效。

  1. $ sudo service mongod restart
  2. [ ok ] Restarting database: mongod.

2. IpTables防火墙

如果你有防火墙,允许在端口27017上的连接,MongoDB缺省端口。

2.1 Any connections can connect to MongoDB on port 27017
iptables -A INPUT -p tcp --dport 27017 -j ACCEPT
2.2 Only certain IP can connect to MongoDB on port 27017
  1. iptables -A INPUT -s <ip-address> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
  2. iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
  3. iptables -A INPUT -s 192.168.161.200 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
  4. iptables -A OUTPUT -d 192.168.161.200 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

提示
查阅MongoDB防火墙文档

2.3 Here is the firewall rules using in one of my MongoDB servers.
/etc/iptables.firewall.rules
  1. *filter
  2. -A INPUT -i lo -j ACCEPT
  3. -A INPUT -d 127.0.0.0/8 -j REJECT
  4. -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  5. -A OUTPUT -j ACCEPT
  6. # Allow HTTP and HTTPS connections from anywhere
  7. -A INPUT -p tcp --dport 80 -j ACCEPT
  8. -A INPUT -p tcp --dport 8080 -j ACCEPT
  9. -A INPUT -p tcp --dport 27017 -j ACCEPT
  10. #-A INPUT -s <ip address> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
  11. #-A OUTPUT -d <ip address> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
  12. # Allow SSH connections
  13. -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
  14. # Allow ping
  15. -A INPUT -p icmp -j ACCEPT
  16. # Log iptables denied calls
  17. -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
  18. # Drop incoming connections if IP make more than 15 connection attempts to port 80 within 60 seconds
  19. -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --set
  20. -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --update --seconds 60 --hitcount 15 -j DROP
  21. # Drop all other inbound - default deny unless explicitly allowed policy
  22. -A INPUT -j DROP
  23. -A FORWARD -j DROP
  24. COMMIT

更新iptables规则

  1. sudo vim /etc/iptables.firewall.rules
  2. sudo iptables-restore < /etc/iptables.firewall.rules

在本教程中,将展示如何启用远程访问到MongoDB服务器。这是测试环境:

  1. MongoDB Server

    • 私有IP – 192.168.161.100
    • 公共IP – 45.56.65.100
    • MongoDB 2.6.3, port 27017
    • IpTables Firewall
  2. Application Server (Same LAN network)

    • 私有IP – 192.168.161.200
    • 公共IP – irrelevant
  3. Developers at home (Different LAN network, WAN)

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

闽ICP备14008679号