How To Install MongoDB 4.4 on Ubuntu 20.04

In this tutorial, we will learn how to install MongoDB 4.4 on Ubuntu 20.04 LTS. We will also learn to configure and secure our MongoDB 4.4 installation

Introduction

In this tutorial, we will learn how to install MongoDB 4.4 on Ubuntu 20.04 LTS (Focal Fossa). We will also learn to configure and secure our MongoDB 4.4 installation

Prerequisites

  • Ubuntu 20.04 LTS (Focal Fossa) with sudo access.

The tutorial to install MongoDB 4.4 also available for different operating systems below:

  • How To Install MongoDB 4.4 on Ubuntu 18.04
  • How To Install MongoDB 4.4 on CentOS 8
  • How To Install MongoDB 4.4 on CentOS 7
  • How To Install MongoDB 4.4 on CentOS 6
  • How To Install MongoDB 4.4 on AlmaLinux 8
  • How To Install MongoDB 4.4 on Rocky Linux 8

Add MongoDB Public Key

First of all, let’s add the MongoDB public key. This key is used by package management tool like apt to ensure the consistency and authenticity of the package.

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

The output should be similar to the text below

Create Repository Configuration For Mongodb 4.4

Create new file /etc/apt/sources.list.d/mongodb-org-4.4.list that contain MongoDB 4.4 repository info using the command below.

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list > /dev/null

Reload apt package database using the command below.

sudo apt-get update

Install MongoDB 4.4 on Ubuntu 20.04

To install the latest stable version of MongoDB 4.4 you can use the command below.

sudo apt-get install mongodb-org

At the time of this writing, the latest stable version of MongoDB 4.4 is 4.4.4.

To install a previous stable version of MongoDB 4.4 you need to specify a version for each package. For example, if you need to install MongoDB 4.4.3 you can use the command below.


sudo apt-get install -y \
    mongodb-org=4.4.3 \
    mongodb-org-server=4.4.3 \
    mongodb-org-shell=4.4.3 \
    mongodb-org-mongos=4.4.3 \
    mongodb-org-tools=4.4.3

Managing MongoDB Service

After MongoDB Server has been installed, we can check MongoDB service using the command below.

sudo service mongod status

We can also use systemctl command to check status of mongod service.

sudo systemctl status mongod

We will get output similar to below which inform that mongod is not running.

To start MongoDB service we can use command below:

sudo service mongod start

or we can also use systemctl to start MongoDB service

sudo systemctl start mongod

We will see output similar to output below

MongoDB service already started but it’s not enabled by default by seeing this line


Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)

Let’s enable MongoDB service on boot by running

sudo systemctl enable mongodb

Now if we check MongoDB service status we will see that the service is enabled.

Checking MongoDB Service

Beside using service or systemctl command, we can use multiple tools to check status of MongoDB service.

To check where MongoDB service listening to we can use netstat

sudo netstat -naptu | grep 27017

In the command above, we grep MongoDB default port 27017.

As alternative we can also grep mongod application name

sudo netstat -naptu | grep mongod

We can also use ss to do similar check like netstat

ss -at  | grep 27017

If we want to know MongoDB process details we can use ps command

ps aux | grep -m1 mongod

We use option -m1 to so we only show the first line of grep since grep will also our grep process that contain mongod word.

top command can be used to see more detailed and real time resource usage of MongoDB process. Use command below to use top but filter only for process run by mongodb user.

top -u mongodb

Create MongoDB root and admin users

MongoDB user management is different compared to RDBMS user management like MySQL or PostgreSQL.

In MongoDB the user is managed per database. If you want to create administrative user you need to create user in admin database.

Connect to MongoDB using mongo client. The command below will make a connection to the admin database.

mongo admin

Use command below to create user root with root role. Don’t forget to change the password.

db.createUser({user:"root", pwd:"changemeplease123123123", roles:[{role:"root", db:"admin"}]})

Generate Random Password Using Command Line

Generate random string for password on command line using command below

uuidgen | sha256sum | awk {'print $1'}

Besides using awk we can also use cut utility to only get the randomly generated password.

uuidgen | sha256sum | cut -d ' ' -f 1

Enabling Authentication on MongoDB 4.4

There are two ways to enable MongoDB authentication, by updating systemd service file or updating mongodb.conf file.

I recommend using the second method since mongodb service file might be overwritten by apt when we upgrade mongodb package.

Updating mongod.service file

Open /lib/systemd/system/mongod.service file.

Find line

ExecStart=/usr/bin/mongod --config /etc/mongod.conf

Replace the line with

ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf

Reload systemd daemons using command below

sudo systemctl daemon-reload

Restart MongoDB service using command below

sudo systemctl restart mongod

Updating mongodb.conf file

Open /etc/mongodb.conf file

Find line

#security:

Replace it with

security:
  authorization: enabled

Restart MongoDB service to enable authentication

sudo service mongod restart

After enabling authentication we can connect using root user that we just created on previous step.


mongo -uadmin admin -p

Uninstall MongoDB 4.4 on Ubuntu 20.04

In this section we’ll learn how to uninstall MongoDB 4.4 from Ubuntu 20.04. Please be really careful when running command on this section.

Before we uninstall MongoDB 4.4 we need to stop MongoDB service first.

sudo service mongodb stop

To uninstall MongoDB 4.4 we can use command below

sudo apt-get purge mongodb-org*

The command above only remove MongoDB packages.

We can run the command below to remove MongoDB 4.4 log directory.

sudo rm -r /var/log/mongodb

To remove MongoDB data directory use command below

WARNING : command below will remove your data and cannot be restored. be very very very careful when you're running command below.

sudo rm -r /var/lib/mongodb

MongoDB 4.4 References

You can find references related to MongoDB 4.4 below

Conclusion

In this tutorial, we set up MongoDB 4.4 on Ubuntu 20.04 LTS (Focal Fossa). We also learn how to manage MongoDB 4.4 service, check MongoDB service status using multiple tools, create root user and also enable authentication.

At the end we learned how to uninstall MongoDB 4.4 from Ubuntu 20.04.

Now you can start building your application using MongoDB as a database.