How to Install MongoDB on Ubuntu 20.04

MongoDB is an open-source document database used in many modern web applications. and MongoDB is a document-oriented NoSQL database used for high volume data storage. Instead of using tables and rows as in the traditional relational databases,

Install the dependencies

apt update

apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Download and add the MongoDB GPG key with the following command

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

Output:

root@crowncloud:~# wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
OK

Adding the MongoDB repository with the following command.

add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'

Output:

root@crowncloud:~# add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'
Hit:1 http://gb.archive.ubuntu.com/ubuntu focal InRelease
Ign:2 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 InRelease
Hit:3 http://gb.archive.ubuntu.com/ubuntu focal-updates InRelease
Get:4 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release [5,389 B]
Hit:5 http://gb.archive.ubuntu.com/ubuntu focal-backports InRelease
Get:6 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 Release.gpg [801 B]
Hit:7 http://gb.archive.ubuntu.com/ubuntu focal-security InRelease
Get:8 https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4/multiverse amd64 Packages [5,163 B]
Fetched 11.4 kB in 2s (7,533 B/s)
Reading package lists... Done

Install the MongoDB with the following command.

apt install mongodb-org

Start the MongoDB service and enable it to start automatically after rebooting the system.

systemctl start mongod
systemctl enable mongod

Output:

root@crowncloud:~# systemctl start mongod
root@crowncloud:~# systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.

Now, check the status of the MongoDB service.

systemctl status mongod

Output:

root@crowncloud:~# systemctl status mongod
● mongod.service - MongoDB Database Server
         Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset>
         Active: active (running) since Thu 2020-10-22 13:57:25 UTC; 58s ago
             Docs: https://docs.mongodb.org/manual
     Main PID: 97416 (mongod)
         Memory: 64.8M
         CGroup: /system.slice/mongod.service
                         └─97416 /usr/bin/mongod --config /etc/mongod.conf

To verify whether the installation has completed successfully by running the following command.

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

Output:

root@crowncloud:~# mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("094b401a-28d6-4dc1-bd18-4557044e6505") }
MongoDB server version: 4.4.1
{
                "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
                },
                "ok" : 1
}

Creating Administrative MongoDB User

First, access the MongoDB shell.

mongo

Output:

root@crowncloud:~# mongo
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("07576aba-4020-4f2d-91ae-331bf494edaa") }
MongoDB server version: 4.4.1
Welcome to the MongoDB shell.
For interactive help, type "help".

Connect to the admin database.

use admin

Output:

> use admin
switched to db admin
>

Run the following command to create a new user and set the password for the user.

db.createUser(
    {
        user: "mongoAdmin",
        pwd: "KAb3747d",
        roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    }
)

Output:

Successfully added user: {
                "user" : "mongoAdmin",
                "roles" : [
                                {
                                        "role" : "userAdminAnyDatabase",
                                        "db" : "admin"
                                }
                ]
}
>

Exit the mongo shell.

quit()

To test the changes, access the mongo shell using the created administrative user.

mongo -u mongoAdmin -p --authenticationDatabase admin

Output:

>mongo -u mongoAdmin -p --authenticationDatabase admin
MongoDB shell version v4.4.1
Enter password:

Switch to the admin database.

use admin

Output:

> use admin
switched to db admin

List the users and see if you can list the created user.

show users

Output:

> show users
{
                "_id" : "admin.mongoAdmin",
                "userId" : UUID("6a498a5b-58f8-41ae-95fc-3a60ef0f7c94"),
                "user" : "mongoAdmin",
                "db" : "admin",
                "roles" : [
                                {
                                                "role" : "userAdminAnyDatabase",
                                                "db" : "admin"
                                }
                ],
                "mechanisms" : [
                                "SCRAM-SHA-1",
                                "SCRAM-SHA-256"
                ]
}
>