Database

Allow Remote Access to MongoDB

Out of the box, MongoDB doesn’t allow remote connections, because by default it has no authentication enabled and is listening on localhost only.

If you try to connect to MongoDB without remote access being allowed, you will get this error:

Error: couldn’t connect to server

$MongoDB:$Port, connection attempt failed:SocketException: Error connecting to $MongoDB:$Port :: caused by :: Connection refused … exception: connect failed

In this article i will show how to allow remote access to MongoDB.

Allow Remote Access to MongoDB

By default, MongoDB is listening on 127.0.0.1:27017 only:

$ sudo netstat -tnlp Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1106/mongod

Open MongoDB configuration file /etc/mongod.conf and change bindIp by adding required LAN interfaces or configure it to bind to all interfaces, for example:

# network interfaces net: port: 27017 bindIp: 127.0.0.1,192.168.0.100 # Enter 0.0.0.0,:: to bind to all interfaces

Restart mongod to apply modifications:

 $ sudo service mongod restart

Now mongod is listening on configured interfaces and can be accessible remotely:

$ sudo netstat -tnlp Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 19757/mongod tcp 0 0 192.168.0.100:27017 0.0.0.0:* LISTEN 19757/mongod

Source: https://www.shellhacks.com/mongodb-allow-remote-access/

Share:
Leave a Comment
Share
Published by
Admin
Tags: MongoDB

Recent Posts

Configure IP Network with ‘nmtui’ Tool

An alternative for the nmcli is the nmtui, short for Network Manager Text User Interface,…

4 years ago

Install MariaDB on Debian 9 (Stretch)

MariaDB is an enhanced, drop-in replacement for MySQL. MariaDB can be a better choice for…

5 years ago

Install MariaDB on Ubuntu 18.04 LTS

MariaDB is an enhanced, drop-in replacement for MySQL. MariaDB can be a better choice for…

5 years ago

Sync Files Directories from Different Cloud Storage with Rclone

Rclone is a command line program written in Go language, used to sync files and…

5 years ago

Enable Authentication – Create Admin/Root User in MongoDB

By default, authentication is disabled in MongoDB, but this is not so critical as, out…

5 years ago

Python Pip Basic Commands

What is Pip? pip is a tool for installing and managing Python packages. With pip…

5 years ago