Introduction

MongoDB is an open-source and cross-platform document-oriented NoSQL database that is popular in building fast and scalable applications that handle massive amounts of data. Unlike traditional relational databases where data is stored in tables, MongoDB uses JSON format to store data in documents. In JSON format, data is formatted in key-value pairs where field names and values are separated by a colon and encapsulated in curly braces.

In this guide you are going to learn how to install and setup MongoDB on your Ubuntu 22.04 server. You will also learn to configure remote connection to your Mongo database.

Steps to Install MongoDB on Ubuntu 22.04

Before doing anything, update your system packages with the help of the following command:

sudo apt update 

Install the required utility packages by entering the following command:

sudo apt install wget curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release 

First, you need to include the MongoDB proprietary package repository in APT resources. To do this, enter the public key of GPG MongoDB with reference to the latest release version of its stable version running the following command:

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

MongoDB’s APT queries in the sources.list.d directory to download and install the sources of the target packages, and now you need to configure it. Before doing so, create a file called mongodb-org-5.0.list:

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, you need to update your Ubuntu system again. This allows the APT package manager to know where to fetch the MongoDB package for download and installation. To do this run the following command:

sudo apt update 

Now it’s time to install MongoDB on your system. You can do this by entering the following command:

sudo apt install mongodb-org -y 

You can install a specific version of MongoDB using the following command:

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

Once the download is complete, start the MongoDB service using the following command:

sudo systemctl start mongod 

You can check MongoDB status with the following command:

sudo systemctl status mongod 

Now enable the MongoDB database with the following command. This will keep the MongoDB database running successfully even after the system reboots:

sudo systemctl enable mongod 

You can test the connection to the MongoDB database by entering the following command:

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

Enter the following command to access the MongoDB shell:

mongo 

Finally, you can access any database-related objective.

Conclusion

MongoDB is a powerful and flexible NoSQL database whose popularity is on a steady rise. It’s a popular database of choice for building mission-critical applications that handle vast amounts of unstructured data.In this guide, you learned how to install and start using MongoDB on Ubuntu 20.04.