How To Install MongoDB on Ubuntu 22.04 LTS

Install MongoDB on Ubuntu 22.04

In this tutorial, we will show you how to install MongoDB on Ubuntu 22.04 LTS. For those of you who didn’t know, 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. MongoDB is available in two editions: Community and Enterprise.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the MongoDB NoSQL database server on Ubuntu 22.04 (Jammy Jellyfish). You can follow the same instructions for Ubuntu 22.04 and any other Debian-based distribution like Linux Mint, Elementary OS, Pop!_OS, and more as well.

Prerequisites

  • A server running one of the following operating systems: Ubuntu 22.04, 20.04, and any other Debian-based distribution like Linux Mint.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo useror access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install MongoDB on Ubuntu 22.04 LTS Jammy Jellyfish

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

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

Step 2. Installing MongoDB on Ubuntu 22.04.

By default, MongoDB is available on Ubuntu 22.04 base repository. Now run the following command below to add the MongoDB repository to your Ubuntu system:

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Next, add the MongoDB GPG key:

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

After the repository was added, then install the MongoDB server packages using the following command below:

sudo apt update 
sudo apt install mongodb-org

Once successfully installed, enable MongoDB (to start automatically upon system boot), start, and verify the status using the commands below:

sudo systemctl enable mongod
sudo systemctl start mongod
sudo systemctl status mongod

Step 3. Test MongoDB.

To test the connection to the MongoDB database, we will run the following command:

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

Output:

MongoDB shell version v5.0.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1022b711-gdt-e46-aec6-f6172bmw666") }
MongoDB server version: 5.0.6
{
        "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
        },
        "ok" : 1
}

To connect to the MongoDB shell simply type:

mongo

Step 4. Create Admin User on MongoDB.

First, access the MongoDB shell:

mongo

Next, connect to the admin database:

use admin

Then, run the following to create a new Mongo user account. For the tutorial, a user idroot will be made:

db.createUser(
{
user: "idroot",
pwd: "your-password-idroot-user",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

Exit the mongo shell:

quit()

Finally, test MongoDB with the new account you created connection status:

mongo --port 27017 --authenticationDatabase "admin" -u "idroot" -p

Output:

mongo -u mongoAdmin -p --authenticationDatabase admin
MongoDB shell version v5.0.9
Enter password:

Congratulations! You have successfully installed MongoDB. Thanks for using this tutorial for installing the MongoDB 5 NoSQL database on Ubuntu 22.04 LTS Jammy Jellyfish system. For additional help or useful information, we recommend you check the official MongoDB website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!