[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[转帖]mongodb允许远程访问

上一篇:[备忘]ConnectException和SocketTimeoutException的区别
下一篇:[整理]spring-data-mongodb的简单使用

添加日期:2016/3/9 16:07:14 快速返回   返回列表 阅读2372次
原地址:http://ju.outofmemory.cn/entry/150644
----------------------------------------
在本教程中,将展示如何启用远程访问到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防火墙文档
http://docs.mongodb.org/manual/tutorial/configure-linux-iptables-firewall/

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

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved