How to Install MongoDB on Ubuntu 20.04

  Suresh Ramani   Ubuntu, Linux   1 month ago
How to Install MongoDB on Ubuntu 20.04 - TechvBlogs

What is MongoDB?

MongoDB is a free and open-source document database. It belongs to a family of databases called NoSQL, which is different from the traditional table-based SQL databases like MySQL and PostgreSQL.

In MongoDB, data is stored in flexible, JSON-like documents where fields can vary from document to document. It does not require a predefined schema, and the data structure can be changed over time.

Features and Advantages of MongoDB

  • Offers high scalability and flexibility; automatic failover and data redundancy
  • Offers an expressive query language that is simple to learn and use
  • Ad-hoc queries for real-time analytics
  • It supports arrays and nested objects as values and allows for flexible and dynamic schemas.
  • It is easy to compose queries that allow sorting and filtering, no matter how nested and supports aggregation, geo-location, time-series, graph search, and more.
  • Supports sharding which enables splitting of large datasets across multiple distributed collections which then eases querying.
  • Supports multiple storage engines

This tutorial describes how to install and configure MongoDB Community Edition on Ubuntu 20.04.

Step 1: Import MongoDB public key

MongoDB is available in the Ubuntu repository. But it's not maintained by MongoDB Inc. If you already installed the MongoDB package, uninstall it first. Then proceed with the following steps.

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

To verify the added GPG key, type:

sudo apt-key list

Step 2: Add MongoDB repository to the source list

Let's add the official MongoDB repository to the source list file - this will allow us to fetch the latest official mongodb-org package.

To create a source list file, type:

sudo touch /etc/apt/sources.list.d/mongodb-org-5.0.list

Now, add the repository source for Ubuntu 20.04:

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

Update the packages again:

sudo apt-get update

And now, you’re finally ready to install MongoDB.

Step 3: Install MongoDB on Ubuntu 20.04

Now that the MongoDB repository is enabled, you can install the latest stable version by running the following command.

sudo apt-get install -y mongodb-org

How to Install MongoDB on Ubuntu 20.04 - TechvBlogs

Step 4: Run MongoDB

To run MongoDB, start the mongod service (daemon for MongoDB) using the command below:

sudo systemctl start mongod

If the service does not start or you encounter an error like “service not found”, issue the command below:

sudo systemctl daemon-reload

After starting the mongod service, check its status to verify if it is running fine. Use the command below to do so:

sudo systemctl status mongod

How to Install MongoDB on Ubuntu 20.04 - TechvBlogs

To start the MongoDB automatically at each boot, the command is:

sudo systemctl enable mongod

Now to start the mongo shell from the same system running the mongod process, the command is as follows:

mongod

#or

mongosh

Thank you for reading this blog.

If you want to manage your VPS / VM Server without touching the command line go and  Checkout this linkServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a  matter of minutes.  You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

Read Also:  How to Install MySQL on Ubuntu 18.04

If you have any queries or doubts about this topic please feel free to contact us. We will try to reach you.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
Full Stack Developer