赞
踩
在本教程中,将展示如何启用远程访问到MongoDB服务器。这是测试环境:
MongoDB Server
Application Server (Same LAN network)
Developers at home (Different LAN network, WAN)
- $ vim /etc/mongod.conf
-
- # /etc/mongod.conf
-
- # 只监听本地接口。注释掉听在所有接口。
- bind_ip = 127.0.0.1
默认情况下,MongoDB绑定到本地接口,它将限制远程连接。如果你不关心安全,只是注释掉接受任何远程连接(不推荐)。
因为都是在同一个局域网网络,你只需要将MongoDB绑定到它自己的私有IP接口。
- $ vim /etc/mongod.conf
-
- # /etc/mongod.conf
-
- # 监听本地和局域网接口。
- 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)
开发人员将通过MongoDB公共IP 45.56.65.100远程访问,允许,将公共IP绑定接口。
- $ vim /etc/mongod.conf
-
- # /etc/mongod.conf
-
- # 监听本地,局域网和公共接口。
- bind_ip = 127.0.0.1,192.168.161.100,45.56.65.100
提示
为开发人员在国内,建议建立VPN连接,而不是打开MongoDB公共IP连接,它是容易被攻击的。
重新启动MongoDB生效。
- $ sudo service mongod restart
- [ ok ] Restarting database: mongod.
如果你有防火墙,允许在端口27017上的连接,MongoDB缺省端口。
iptables -A INPUT -p tcp --dport 27017 -j ACCEPT
- iptables -A INPUT -s <ip-address> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
- iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
-
- iptables -A INPUT -s 192.168.161.200 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
- iptables -A OUTPUT -d 192.168.161.200 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
提示
查阅MongoDB防火墙文档
/etc/iptables.firewall.rules
- *filter
-
- -A INPUT -i lo -j ACCEPT
- -A INPUT -d 127.0.0.0/8 -j REJECT
- -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- -A OUTPUT -j ACCEPT
-
- # Allow HTTP and HTTPS connections from anywhere
- -A INPUT -p tcp --dport 80 -j ACCEPT
- -A INPUT -p tcp --dport 8080 -j ACCEPT
- -A INPUT -p tcp --dport 27017 -j ACCEPT
-
- #-A INPUT -s <ip address> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
- #-A OUTPUT -d <ip address> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
-
- # Allow SSH connections
- -A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
-
- # Allow ping
- -A INPUT -p icmp -j ACCEPT
-
- # Log iptables denied calls
- -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
-
- # Drop incoming connections if IP make more than 15 connection attempts to port 80 within 60 seconds
- -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --set
- -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --update --seconds 60 --hitcount 15 -j DROP
-
- # Drop all other inbound - default deny unless explicitly allowed policy
- -A INPUT -j DROP
- -A FORWARD -j DROP
-
- COMMIT

更新iptables规则
- sudo vim /etc/iptables.firewall.rules
- sudo iptables-restore < /etc/iptables.firewall.rules
在本教程中,将展示如何启用远程访问到MongoDB服务器。这是测试环境:
MongoDB Server
Application Server (Same LAN network)
Developers at home (Different LAN network, WAN)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。