×


MongoDB remote access configuration for Ubuntu

Are you trying to configure MongoDB remote access for Ubuntu? Our Server Administrators can help you today.


As part of our Server Management Services here at Ibmi Media, we regularly help our customers to perform Ubuntu related tasks.

In this context, we will look into how to configure remote access for MongoDB on Ubuntu 18.04.

More about MongoDB?

MongoDB is a free and an open-source document database. It is a leading NoSQL database program which uses JSON-like documents with optional schemas. It is used in Modern web applications.

In, MongoDB, data objects are stored in a collection as a separate document. Therefore , it makes it very easy in terms of data structures unlike other Database management system.

It supports Most Operating systems such as Linux, Windows, Solaris and Mac OS X.

How to configure remote access for MongoDB on Ubuntu 18.04

We shall look into the steps to take in order to configure MongoDB to enable remote access on an Ubuntu Server.

Before getting started, you have to have a Server with Ubuntu 18.04, MongoDB installed on the server and a different server which you will access from your MongoDB.

i. Setting the Firewall

Set up the firewall by enabling the UFW firewall on the server. To do this, check the port which MongoDB is listening on. Run the following commands to see;

sudo lsof -i | grep mongo


Lets take a look at the following output;

mongod 82221 mongodb 11u IPv4 913411 0t0 TCP localhost:27017 (LISTEN)


From the above report, MongoDB is listening on port 27017 which is the default port for  MongoDB service.

Now to make this port accessible for MongoDB from the external server, run the following command;

sudo ufw allow from external_server_ip to any port 27017


Note that "external_server_ip" stands for the external server ip address. So change it as per your case.

To verify the firewall settings change with ufw, use the command below;

sudo ufw status


You will get an output such as the following;

Output
Status: active

To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
27017 ALLOW trusted_server_ip
OpenSSH (v6) ALLOW Anywhere (v6)



ii. Configuring the Server with a Public bindIP

To allow remote connections, the MongoDB configuration file "/etc/mongod.conf" must be modified. A public routable Server IP address can be binded with MongoDB. This will make it possible for MongoDB to listen to connections within the MongoDB server and the remote machine.

Start by editing MongoDB configuration file with the command below;

sudo nano /etc/mongod.conf


In the network interfaces section of this file, look out for the bindip value as shown below;

# network interfaces
net:
port: 27017
bindIp: 127.0.0.1


Here you can append a comma to this line followed by your MongoDB server's IP address as addressed below;

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


After making this change, save the file and exit.

Now you can restart MongoDB service to effect changes with the command below;

sudo systemctl restart mongod



iii. Testing the Remote Connectivity for the Server

Now that the configuration of MongoDB for remote access has been completed on Ubuntu Server, simply use the nc command to test the connection to see if it going to work.

nc signified "netcat"


First log into the server via an SSH tool such as putty and run the command below;

nc -zv mongodb_server_ip 27017


Ensure that you change "mongodb_server_ip" as per your Server IP address where MongoDB was installed.

Once the connection is successful, you will get a positive response such as;

Output
Connection to mongodb_server_ip 27017 port [tcp/*] succeeded!


Now you can make a connection to the MongoDB instance with the connection string URI with the command below;

mongo "mongodb://mongo_server_ip:27017"


Now you will confirm that your MongoDB server can accept connections from the specified external server.

Need support in performing Ubuntu and Debian Server tasks? We are available to help you today.


Conclusion

This article will guide you through the steps you can take to configure remote access for MongoDB on Ubuntu 18.04.