How to connect to MongoDB server from remote machine

How to connect to MongoDB server from remote machine

Posted by Andy on 2015-03-28 07:11:14.0


MongoDB – Allow remote access

Folow the below steps to enable remote access to a MongoDB server. Here is the tested environment :

1. MongoDB Server

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

2. Application Server (Same LAN network)

  • Private IP – 192.168.161.200
  • Public IP – irrelevant

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

  • Public IP – 10.0.0.1

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


$ 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

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 = 127.0.0.1,192.168.161.100


1.2 To allow remote access for from specific server.
Developers will remote access via MongoDB public IP 45.56.65.100, 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 = 127.0.0.1,192.168.161.100,45.56.65.100


Restart MongoDB to take effect.

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


Good Luck!!

 

Posted by Shiv K Sah on 2015-05-13 10:46:55.0