Install MongoDB on Ubuntu 18.04 With Examples

MongoDB is a free and open-source NoSQL Database used commonly in today’s web applications.

In this tutorial, we will show you how to install and setup MongoDB on your Ubuntu 18.04 machine in a few simple steps!


Step 1: Install MongoDB on Ubuntu Using Apt

Since the MongoDB package is available in Ubuntu’s official repositories, we can simply use apt to install MongoDB on our system.

But before that, update all system critical packages first.

Now, install the MongoDB package using:

mongodb-ubuntu-install-1024x393

This will install the latest version of MongoDB on our system.

As for any Database System, there is a server and a service associated with the task of Database Management. The MongoDB server is automatically started after the installation.

To ensure that our installation went smoothly, we need to ensure that the MongoDB service and the server are working correctly.

Step 2: Check the MongoDB Service

To check the status of the MongoDB service, use systemctl:

You’ll get an output similar to the below screenshot.

mongodb-ubuntu-check-service

Mongodb Ubuntu Check Service

So now that we know that our service is running, let’s verify if our MongoDB Server is responding to our requests or not.

Step 3: Test the MongoDB Server

By default, the MongoDB Server is located at the localhost and uses port 27017. So the address of the server is:

Let’s test our server by running this command to find the Connection Status using mongo command:

You should get a response like the below screenshot, having the “ok” field as 1, ensuring that our Connection is valid.

mongodb-ubuntu-check-connection-status

Mongodb Ubuntu Check Connection Status

Step 4: Allow remote access to the Database

By default, the MongoDB server is only allowed by the ufw firewall to operate locally.

To allow access to MongoDB on it’s port 27017 from anywhere, we need to allow incoming connections on this port.

But this is very insecure, since any person can access the Database. To prevent this, only allow IP Addresses which correspond to trusted machines/servers.

Replace TRUSTED_IP with any trusted IP address in the above command.

This allows connections from your trusted server to reach the MongoDB Database, since ufw now allows these connections.

But we still need to ensure that the MongoDB server does not only listen locally. Therefore, we need to add our trusted machine’s IP to the mongodb.conf configuration file.

Use your favorite text editor and edit /etc/mongodb.conf.

Go to the bind_ip value.

Mongodb Ubuntu Config File

Add your trusted machine’s public IP Address here, by appending to the field using a comma (,).

The modified file will look something like this:

Mongodb Ubuntu Add Server Address

Exit the editor, after saving changes


Step 5: Restart the MongoDB Service

Now, we are almost done! We simply need to restart the mongodb service to apply our changes.

We have now successfully configured our MongoDB Server to accept remote connections!


Conclusion

In this tutorial. we showed you how you can install and configure the latest version of MongoDB on your Ubuntu 18.04 system. We also made sure that we can accept specific remote connections to our database from another trusted server.

I hope this served you well and cleared any of your remaining doubts regarding MongoDB installation!


By admin

Leave a Reply

%d bloggers like this: