How To Install MongoDB 5 on Ubuntu 20.04 LTS

By bhagwatchouhan
How To Install MongoDB 5 on Ubuntu 20.04 LTS

MongoDB is the most popular NoSQL database server and it can be used to store JSON documents natively in BSON format. MongoDB is a document-oriented, open-source, and platform-independent Database Management System (DBMS). This tutorial provides all the steps required to install and set up the MongoDB 5 Community Server on Ubuntu 20.04 LTS. The steps should be the same for other versions of MongoDB including MongoDB 4 and Linux systems.

This tutorial provides the steps required to install MongoDB on the server version of Ubuntu. It uses a package manager to install the latest MongoDB 5 Community Edition on Ubuntu 20.04 LTS. You can also follow How To Install MongoDB on Ubuntu Desktop to install MongoDB on the desktop version for development purposes.

At the time of writing this tutorial, the most recent version of MongoDB i.e. 5.0 and onwards provides packages only for the 64-bit versions of Ubuntu 16.04 LTS, Ubuntu 18.04 LTS, Ubuntu 20.04 LTS, and above.

Notes: You can also follow How To Install MongoDB 5 on Windows and How To Install MongoDB 5 on Mac.

 

Remove the Previous Installation

We can check the previous installation and remove it in case it's already installed on the system using the commands shown below.

# Check previous installation
sudo apt list --installed | grep mongodb
# Take the backup of existing databases # Stop the server sudo service mongod stop
# Completely remove previous installation sudo apt remove mongodb sudo apt purge mongodb

You can use the below-mentioned commands to remove the MongoDB installed on the desktop version using the installer downloaded from the official website.

# Remove server
sudo apt remove mongodb-org-server
# Remove shell sudo apt remove mongodb-org-shell

We can also clean the files of the previous installation using the commands shown below.

# Remove residual files
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

 

Import Public Key

In this step, we will import the MongoDB public GPG Key using the command as shown below.

# Import GPG Key
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
# Result OK

The above command will show the OK message after adding the Key.

 

Create List File

Create the list file for Ubuntu 20.04 LTS using the command as shown below.

# Create list file using nano editor
sudo nano /etc/apt/sources.list.d/mongodb-org-5.0.list
# Copy and paste the content to the list file deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse

You can also create the file directly as shown below.

# Direct method
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Now refresh the packages index using the command as shown below.

# Install MongoDB
sudo apt-get install -y mongodb-org
# Installtion last few messages Adding user `mongodb' to group `mongodb' ... Adding user mongodb to group mongodb Done. Setting up mongodb-org-shell (5.0.3) ... Setting up mongodb-database-tools (100.5.0) ... Setting up mongodb-org-mongos (5.0.3) ... Setting up mongodb-org-database-tools-extra (5.0.3) ... Setting up mongodb-org-database (5.0.3) ... Setting up mongodb-org-tools (5.0.3) ... Setting up mongodb-org (5.0.3) ... Processing triggers for man-db (2.9.1-1) ...

We can see that the above command installed the packages mongodb-org, mongodb-org-database, mongodb-org-mongos, mongodb-org-shell, and mongodb-org-tools. The command also starts the MongoDB database service. We can optionally avoid the auto-upgrade of the currently installed MongoDB packages using the commands as shown below.

# Keep the current version installed on system upgrade
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

 

Verify Installation

In this step, we will verify the installation using the commands as shown below.

# Check version
mongod -version
# Output db version v5.0.3 Build Info: { "version": "5.0.3", "gitVersion": "657fea5a61a74d7a79df7aff8e4bcf0bc742b748", "openSSLVersion": "OpenSSL 1.1.1f 31 Mar 2020", "modules": [], "allocator": "tcmalloc", "environment": { "distmod": "ubuntu1804", "distarch": "x86_64", "target_arch": "x86_64" } }

This confirms that we have successfully installed MongoDB 5 on Ubuntu.

 

Server Commands

We can start and stop the server as a system service using the commands shown below.

# Start the Service
sudo service mongod start
# Restart the Service sudo service mongod restart
# Stop the Service sudo service mongod stop
# Check status sudo service mongod status

We can also enable or disable the MongoDB server to start automatically with the server. The default behavior is to start the server as part of the installation process.

# Disable auto-start
sudo systemctl disable mongod
# Enable auto-start sudo systemctl enable mongod

 

Getting Started With MongoDB

Start the MongoDB Shell as shown in Fig 1.

Install MongoDB 5 on Ubuntu 20.04 LTS - Mongo Shell

Fig 1

It shows the warning message indicating that the MongoDB Shell has been deprecated and will be removed in future versions of MongoDB. Instead of mongo shell, we can use mongosh using the mongosh command as shown in Fig 2.

Install MongoDB 5 on Ubuntu 20.04 LTS - mongosh

Fig 2

The mongosh outputs a message indicating that access control is not enabled for the installation. MongoDB provides options to create the Authentication Database to store user details and privileges across different databases. The same user having appropriate permissions can act on multiple databases using the Role-Based Access Control (RBAC). The RBAC provided by MongoDB governs access to a MongoDB system.

Now list the databases as shown below.

# List Databases
show databases
# Output admin 41 kB config 73.7 kB local 73.7 kB

We can see that MongoDB has already created three databases i.e. admin, config, and local. Now we will add the user admin with root role to the admin database using the commands as shown below.

# Switch to admin database
use admin
# Create the admin user with root role db.createUser( { user: "admin", pwd: "<password>", roles: [ "root" ] } )
# Output
{ ok: 1 }
# Exit the Shell exit

This is how we can add admin users to the admin database and assign a role to the user. We can start creating the databases and add data either using the shell or programmatically using the preferred language.

 

Secure The Server

We can further tighten the security by enabling the MongoDB authentication using the commands shown below.

# Open the config
sudo nano /lib/systemd/system/mongod.service
# Update the ExecStart value #ExecStart=/usr/bin/mongod --config /etc/mongod.conf ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf

Also, enable the authorization as shown below.

# Enable Authorization
sudo nano /etc/mongod.conf
# Update .... .... # security: security: authorization: enabled .... ....
# Save and close the Editor - Ctrl + O + Enter and Ctrl + X

Now save the file and reload the settings using the command shown below.

# Reload system service
sudo systemctl daemon-reload
# Restart the server sudo service mongod restart

Now we can connect with MongoDB using the shell as shown below.

# Connect securely using mongo shell
mongo -u admin -p <password> --authenticationDatabase admin
# OR
mongo -u admin --authenticationDatabase admin
# Connect securely using mongosh mongosh --username admin --password <password> --authenticationDatabase admin # OR mongosh -u admin -p <password> --authenticationDatabase admin # OR mongosh -u admin --authenticationDatabase admin
# Connect securely using mongosh mongosh "mongodb://<ip or hostname>:<port>" -u admin -p <password> --authenticationDatabase admin

It won't show the warning message about Access Control as we saw in Fig 2.

Install MongoDB 5 on Ubuntu 20.04 LTS - Secure mongosh

Fig 3

Now we have secured the Shell and only the authenticated users with valid roles and permissions can access the database.

 

Summary

This tutorial provided all the steps required to install MongoDB Server 5.0 on Ubuntu 20.04 LTS. It also provided the steps to connect the server using shell and added an admin user with the root role. Finally, we have enabled authentication to allow authenticated users using mongo shell and mongosh.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan