Bobcares

How to configure Remote Access for MongoDB on Ubuntu 18.04

by | Oct 12, 2020

Willing to configure MongoDB installation that securely allows remote access on Ubuntu? We can help you with it.

Here at Bobcares, we have seen several such Ubuntu related installations as part of our Server Management Services for web hosts and online service providers.

Today we’ll see how to configure remote access for MongoDB on Ubuntu 18.04

 

Know more about MongoDB

MongoDB is a free and open-source NoSQL document database. It is also known as Mongo which is mainly used in modern web applications.

Here, the data objects are stored as separate documents inside a collection. So you need not worry about the data structure such as the number of fields or types of fields to store values.

General distributions for MongoDB support Windows, Linux, Mac OS X, and Solaris.

 

Configure remote access for MongoDB on Ubuntu 18.04

Now let’s see the process of configuring the MongoDB to allow remote access on Ubuntu.

Before getting into the procedure, make sure that you have a server running Ubuntu 18.04, MongoDB installed on your server, another computer from which you’ll access your MongoDB instance.

 

1. Adjusting the Firewall

Make sure to enable a UFW firewall on the server. If you wish to use MongoDB locally with the applications running on the same server, then this is the recommended setting. However, if you wish to connect to your MongoDB server from a remote location, you have to allow incoming connections to the port where the database is listening by adding a new UFW rule.

First, check the port to which the MongoDB is listening. For that, run the below command.

sudo lsof -i | grep mongo

Here is an example output that shows that the MongoDB process is listening for connections on its default port, 27017.

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

Run the below command that will open up access to MongoDB’s default port while explicitly only allowing the IP address of the other trusted server. Also, make sure to change trusted_server_ip to the IP address of the trusted remote machine you’ll use to access your MongoDB instance.

sudo ufw allow from trusted_server_ip to any port 27017

Moreover, if you wish to access the MongoDB from another machine then run this command with the new machine’s IP address in place of trusted_server_ip

Run the below command to verify the change in firewall settings with ufw.

sudo ufw status

As a result, the output will show that traffic to port 27017 from the remote server is now allowed.

Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
27017 ALLOW trusted_server_ip
OpenSSH (v6) ALLOW Anywhere (v6)

 

2. Configuring a Public bindIP

In order to allow the remote connections, the MongoDB configuration file — /etc/mongod.conf must be edited. So that it additionally bind MongoDB to your server’s publicly-routable IP address. This will let your MongoDB installation be able to listen to connections made to your MongoDB server from remote machines.

Now open the MongoDB configuration file using a text editor.

sudo nano /etc/mongod.conf

Find the network interfaces section, then the bindIp value:

. . .
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1

. . .

Then append a comma to this line followed by your MongoDB server’s public IP address.

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

. . .

Then save the file and close it.

Finally, restart MongoDB so that the changes take place.

sudo systemctl restart mongod

 

3. Testing Remote Connectivity

Now we have configured the MongoDB installation to listen for connections that originate on its publicly-routable IP address and grant remote machine access through the server’s firewall to Mongo’s default port.

You can use nc command to test that the trusted remote server is able to connect to the MongoDB instance. nc is hort form of netcat.

For that, login to the server by running the below command.

ssh sammy@trusted_server_ip

Then run the below nc command. Make sure to replace mongodb_server_ip with the IP address of the server on which you installed MongoDB.

nc -zv mongodb_server_ip 27017

If the trusted server accesses the MongoDB daemon, its output will indicate that the connection was successful:

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

Now, connect to the MongoDB instance with a connection string URI, like this:

mongo "mongodb://mongo_server_ip:27017"

This confirms that your MongoDB server can accept connections from the trusted server.

[Need any further assistance with Ubuntu queries? – We are here to help you.]

 

Conclusion

In today’s writeup, we saw how our Support Engineers configure remote access for MongoDB on Ubuntu 18.04

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.