MongoDB

Install MongoDB on Ubuntu

MongoDB is a freely accessible database. A database contains the gathered information at one place so it is easy to access, utilize and manage the stored data. Now if we go ahead, there are different types of databases for example a centralized database, cloud database, NoSQL database etc. all the databases are classified on the basis of properties. Instead of discussing all types we will have a short introduction of NoSQL databases because MongoDB belongs to it. NoSQL database means it can manage the data differently from the other conventional databases such as MySQL. MongoDB is a non-relational database, which can be used more conveniently. There are not a set of rules unlike other databases that you have to follow. You can manage data on a tabular form and can alter it at any time on the rules set by the user itself.

In this tutorial, we are going to discuss how MongoDB can be installed on Ubuntu 20.04. Let’s discuss the installation process in detail:

How MongoDB can be installed on Ubuntu

To install MongoDB on Ubuntu follow the below mentioned procedure:

Accessing the key of MongoDB

We will add the key of MongoDB from the official website of the MongoDB in the Ubuntu repository by using the command wget. It may ask for the permission of the administration by asking for a password so enter the password if required:

$ wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

As an output “OK” will be displayed which means the key has been added successfully in the repository of Ubuntu. After this, we will add the MongoDB repository in our source repository and for this purpose first we will open the sources list with the help of a nano editor:

$ sudo nano /etc/apt/sources.list

As soon as the editor opens, copy the following text and save in the file as shown in the image:

deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse

Exit the editor by saving the file by pressing CTRL+S and then pressing CTRL +X.

Updating and Upgrading Ubuntu Repository

After the downloading of the key of the MongoDB from its official website and addition of the MongoDB repository in the sources list of the Ubuntu repository we will update the repository of Ubuntu:

$ sudo apt update

We will also upgrade the repository so if there are any packages present which need to be upgraded, they can get upgraded at this point:

$ sudo apt upgrade

The packages are updated as well as they are upgraded too.

Installation of MongoDB

For the installation of MongoDB, we will simply use the install command and will install the MongoDB as:

$ sudo apt install mongodb-org -y

Start and Check Status of MongoDB

After the successful installation, we will start the MongoDB by using the systemctl command.

$ sudo systemctl start mongod

As there is no error, the command runs successfully and the MongoDB has been started, but to confirm the success of the above command we will check the status of the MongoDB:

$ sudo systemctl status mongod

The active(running) status and green dot confirms the start command was run successfully and the MongoDB has been running.

Running the MongoDB

We can open the MongoDB by simply typing mongo:

$ mongo

Enabling the MongoDB

We can enable the MongoDB by using the systemctl command as:

$ sudo systemctl enable mongod

Testing of the MongoDB

For the testing of the MongoDB we will run a database diagnostic command by connecting to the server. We can do so by the following command

$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'

The output shows that the MongoDB is not only installed properly but also working.

Conclusion

If you are in the beginning stages of web development, and you are developing the website then prefer to work on the MongoDB as it has no limitations for managing the data and you can manage your data easily by setting your own rules. MongoDB is nowadays being used mostly in web applications. In this tutorial, we have discussed the installation process of the MongoDB on Ubuntu 20.04.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.