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

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

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

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

因为都是在同一个局域网网络,你只需要将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)
1.2 允许开发人员远程访问

开发人员将通过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.

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
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 Here is the firewall rules using in one of my MongoDB servers.
/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服务器。这是测试环境:

  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
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VCHH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值