MongoDB – Allow remote access

In this tutorial, we will show you how to enable remote access to a MongoDB server. Here is the tested environment :

1. MongoDB Server

  • Private IP –
  • Public IP –
  • MongoDB 2.6.3, port 27017
  • IpTables Firewall

2. Application Server (Same LAN network)

  • Private IP –
  • Public IP – irrelevant

3. Developers at home (Different LAN network, WAN)

P.S By default, MongoDB doesn’t allow remote connections.

1. Bind IP

$ vim /etc/mongod.conf

# /etc/mongod.conf

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

By default, MongoDB bind to local interface only, it will restrict the remote connections. If you don’t care about security, just comment out to accept any remote connections (NOT Recommend).

1.1 To allow LAN connections from Application Server.
Since both are in the same LAN network, you just need to bind MongoDB to its own private IP interface.

$ vim /etc/mongod.conf

# /etc/mongod.conf

# Listen to local and LAN interfaces.
bind_ip =,

Common Mistake
Don’t put the Application Server IP in bind_ip option. This bind_ip option tells MongoDB to accept connections from which local network interfaces, not which “remote IP address”.

Default – Connection Fail

AS ( <-- LAN --> MongoDB( <--> bind_ip (

Now – Connection Success

AS ( <-- LAN --> MongoDB( <--> bind_ip (,

1.2 To allow remote access for developers at home.
Developers will remote access via MongoDB public IP, to allow this, bind the public ip interface as well.

$ vim /etc/mongod.conf

# /etc/mongod.conf

# Listen to local, LAN and Public interfaces.
bind_ip =,,

For developers at home, it’s recommended to set up a VPN connection, instead of open up the MongoDB public IP connection, it is vulnerable to people attack.

Restart MongoDB to take effect.

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

2. IpTables Firewall

If you have firewall, allow connections on port 27017, MongoDB default port.

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 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -d -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

2.3 Here is the firewall rules using in one of my MongoDB servers.



-A INPUT -i lo -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


Update the iptables rules

sudo vim /etc/iptables.firewall.rules
sudo iptables-restore &lt; /etc/iptables.firewall.rules


  1. MongoDB - Configuration File Options
  2. Configure Linux iptables Firewall for MongoDB
  3. Ubuntu : IptablesHowTo
  4. Linode - Securing Your Server

nosqldb 于 2016-01-21 21:12 修改
0 回复
需要 登录 后方可回复, 如果你还没有账号你可以 注册 一个帐号。