How to Install MongoDB on Ubuntu 20.04
How to Install MongoDB on Ubuntu 20.04

How to Install MongoDB on Ubuntu 20.04 Easily

This tutorial post will show you How to Install MongoDB on Ubuntu 20.04 with step by step procedure.

Steps Involved:

How to Enable Official MongoDB Repository?

We need to first identify the latest version of the MongoDB package to import Official GPG key of MongoDB repository before installing MongoDB.

Checkout this site to get the latest MongoDB Server version : https://pgp.mongodb.com/

At the time of creating this post, it was 5.0. So will use 5.0 as the MongoDB version throughout this tutorial post.

Use “curl” and “apt-key” together to import MongoDB GPG key.

congo@ubuntu:~$ sudo curl -fsSL https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

Once GPG key is added, we can enable the MongoDB official repository as below. Make sure to change the latest version you have got from their site.

congo@ubuntu:~$ sudo echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list

How to Install MongoDB on Ubuntu 20.04

As we have recently enabled MongoDB repository, we need to re-update the package index for the latest packages.

congo@ubuntu:~$ sudo apt-get update

Lets use “apt-get” command to install MongoDB on Ubuntu 20.04.

congo@ubuntu:~$ sudo apt-get install -y mongodb-org

Output Trimmed:

Setting up mongodb-mongosh (1.3.1) ...
Setting up mongodb-org-server (5.0.7) ...
Adding system user `mongodb' (UID 114) ...
Adding new user `mongodb' (UID 114) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 121) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org-shell (5.0.7) ...
Setting up mongodb-database-tools (100.5.2) ...
Setting up mongodb-org-mongos (5.0.7) ...
Setting up mongodb-org-database-tools-extra (5.0.7) ...
Setting up mongodb-org-database (5.0.7) ...
Setting up mongodb-org-tools (5.0.7) ...
Setting up mongodb-org (5.0.7) ...
Processing triggers for man-db (2.9.1-1) ...

Our main task to install MongoDB on ubuntu 20.04 is success. Lets enable and start MongoDB service.

How to Start and Enable MongoDB service on Ubuntu?

Let’s use “systemctl” command to start and enable MongoDB service as below.

congo@ubuntu:~$ sudo systemctl start mongod && sudo systemctl enable mongod

Now check the MongoDB service status to make sure the MongoDB server is running or not.

congo@ubuntu:~$ sudo systemctl status mongod

Ouput:

mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-04-12 19:04:04 IST; 58s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 5288 (mongod)
     Memory: 62.2M
     CGroup: /system.slice/mongod.service
             └─5288 /usr/bin/mongod --config /etc/mongod.conf

Apr 12 19:04:04 ubuntu systemd[1]: Started MongoDB Database Server.

Let’s test MongoDB Database Server is working or not.

How to Test MongoDB Database Server is functional?

We can use “mongo” command to connect to the database server and run diagnostic command to get more informations about MongoDB Database server as below.

congo@ubuntu:~$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'

Output:

MongoDB shell version v5.0.7
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("8f7e1bd6-1cb5-46a1-8f3c-4fd0b43b58e1") }
MongoDB server version: 5.0.7
{
        "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
        },
        "ok" : 1
}

That’s it for this post, Hope this tutorial helped you to understand how to install MongoDB on Ubuntu 20.04.

If you are interested in learning, Request you to go through the below recommended tutorial.

DevOps Full Course Tutorial for Beginners – DevOps Free Training Online
Docker Full Course Tutorial for Beginners – Docker Free Training Online
Kubernetes Full Course Tutorial for Beginners – Kubernetes Free Training Online
Ansible Full Course Tutorial for Beginners – Ansible Free Training Online
Openstack Full Course Tutorial for Beginners – Openstack Free Training Online

Keep practicing and have fun. Leave your comments if any.

Support Us: Share with your friends and groups.

Stay connected with us on social networking sites, Thank you.

YouTube | Facebook | Twitter | Pinterest | Telegram