How to Install MongoDB on Ubuntu 20.04

MongoDB is a free and open-source NoSQL database. Unlike the traditional Database Management Systems (DBMS) i.e. SQL databases, it stores the data in the form of documents. It doesn’t use the scheme to create the database. MongoDB is widely used in web and mobile applications because it can be integrated easily with such applications. This guide explains how to install MongoDB on Ubuntu 20.04.

Install MongoDB on Ubuntu 20.04

MongoDB is present in Ubuntu 20.04 repository. We can easily install it from the Ubuntu 20.04 repository. Follow the below-given steps to install MongoDB on Ubuntu 20.04:

Step 1: Update apt cache

Before installing any application or package, it is always recommended to update the apt repository cache first using the following command:

$ sudo apt update

Step 2: Install MongoDB

After the successful update of the apt repository cache, now we are ready to install MongoDB with the following command:

$ sudo apt install mongodb

During MongoDB installation, the command line will prompt with a yes and no option. To continue the installation process, you are supposed to press y on the terminal.

Step 3: Check MongoDB status

After the successful installation of MongoDB, execute the command to check MongoDB status:

$ sudo systemctl status mongodb

The output shows that MongoDB is active. It also verifies that MongoDB is installed successfully. 

MongoDB useful commands

Various MongoDB-associated useful commands are used to start, stop, and restart the MongoDB on Ubuntu 20.04.

All the below-given commands require root privileges.

Start MongoDB

The MongoDB can be started with the following command:

$ sudo systemctl start mongodb

Stop MongoDB

We can stop MongoDB service anytime with the following command:

$ sudo systemctl stop mongodb

Restart MongoDB

During the execution of MongoDB, if any error occurs, we can restart the MongoDB using the command:

$ sudo systemctl restart mongodb

Open MongoDB shell

We need to open the MongoDB shell to create and work on databases. The MongoDB shell can be opened using the following command:

$ sudo mongo

Uninstall or remove MongoDB

At any point, if you want to uninstall or remove MongoDB from Ubuntu 20.04, you can easily do it. Before removing MongoDB, we need to stop its service using the following command:

$ sudo systemctl stop mongodb

Now run the below-given commands one by one to remove MongoDB from your Ubuntu 20.04 system:

$ sudo apt purge mongodb
$ sudo apt autoremove

The MongoDB will be uninstalled or removed successfully.

Conclusion

MongoDB is a popular, free, and open-source NoSQL database. It is used worldwide by many organizations to store and retrieve data. This article explains how to install MongoDB on Ubuntu 20.04.