DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for How to Install MongoDB on Ubuntu 20.04
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

How to Install MongoDB on Ubuntu 20.04

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 -
Enter fullscreen mode Exit fullscreen mode

To verify the added GPG key, type:

sudo apt-key list
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Update the packages again:

sudo apt-get update
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Step 4: Run MongoDB

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

sudo systemctl start mongod
Enter fullscreen mode Exit fullscreen mode

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

sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

sudo systemctl enable mongod
Enter fullscreen mode Exit fullscreen mode

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

mongod

//or

mongosh
Enter fullscreen mode Exit fullscreen mode

Thank you for reading this blog.

Top comments (0)

πŸ‘‹ New to DEV?

Head over to our Welcome Thread and tell us what you're working on!