How to allow remote connections to 🍃 MongoDB

How to allow remote connections to 🍃 MongoDB

If you are using MongoDB for development or you are running MongoDB database on the same server as your application, you probably do not want to expose MongoDB to the outside the local network.

We can use bindIp option to allow remote access to the MongoDB Server or listen to a specific network interface.

mongo --host 192.168.1.100

How to allow remote connections in Linux / Ubuntu / CentOS

In Linux, including Ubuntu and CentOS 8, bindIp is by default is set to 127.0.0.1 in /etc/mongod.conf. This means mongod process only listen on the local loopback interface.

If you set value of the bindIp to 0.0.0.0 or remove the bindIp option, mongod process will listen on all interfaces.

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

Or bind mongod process to a specific IP:

net:
  port: 27017
  bindIp: 192.168.1.100

Bind multiple IP addresses:

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,192.168.1.100

Allow MongoDB remote ronnections from Firewall

Your firewall may still block remote access to mongodb server. To allow access you need to open TCP port 27017 from your firewall settings.

For example, CentOS 7 by default use the firewalld.

sudo firewall-cmd --permanent --add-port=27017/tcp
sudo firewall-cmd --reload
whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.