MongoDB –允许远程访问

在本教程中,我们将向您展示如何启用对MongoDB服务器的远程访问。 这是经过测试的环境:

1. MongoDB服务器

  • 专用IP – 192.168.161.100
  • 公用IP – 45.56.65.100
  • MongoDB 2.6.3,端口27017
  • iptables防火墙

2.应用服务器(同一局域网)

  • 专用IP – 192.168.161.200
  • 公共IP –不相关

3.在家中的开发人员(不同的LAN网络,WAN)

  • 公用IP – 10.0.0.1

PS默认情况下,MongoDB不允许远程连接。

1.绑定IP

$ vim /etc/mongod.conf

# /etc/mongod.conf

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1

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

1.1允许来自Application Server的LAN连接。
由于两者都在同一个LAN网络中,因此您只需要将MongoDB绑定到其自己的私有IP接口即可。

$ vim /etc/mongod.conf

# /etc/mongod.conf

# Listen to local and LAN interfaces.
bind_ip = 127.0.0.1,192.168.161.100

常见的错误
不要将Application Server 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接口。

$ vim /etc/mongod.conf

# /etc/mongod.conf

# Listen to local, LAN and Public interfaces.
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.

2. IpTables防火墙

如果您有防火墙,请允许端口27017 (MongoDB的默认端口)上的连接。

2.1任何连接都可以在端口27017上连接到MongoDB

iptables -A INPUT -p tcp --dport 27017 -j ACCEPT

2.2仅某些IP可以在端口27017上连接到MongoDB

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防火墙文档

2.3这是我的一台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

参考文献

  1. MongoDB –配置文件选项
  2. 为MongoDB配置Linux iptables防火墙
  3. Ubuntu:IptablesHowTo
  4. Linode –保护服务器安全

翻译自: https://mkyong.com/mongodb/mongodb-allow-remote-access/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值