How to Install MongoDB on Ubuntu 20.04

Install MongoDB on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure Install MongoDB on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

MongoDB is one of the most popular free and open-source NoSQL databases. MongoDB has written in C++ for massive scalability, high performance, and availability. It stores data in JSON-like documents and supports Ad hoc queries, indexing, and real-time aggregation.

Install MongoDB on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

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

Step 2. Install MongoDB on Ubuntu system.

Import the repository’s GPG key and add the MongoDB repository with:

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse'

After that the repository is enabled, install the MongoDB using the apt command:

sudo apt install mongodb-org

Once the installation is completed, start the MongoDB daemon and enable it to start on boot by typing:

sudo systemctl start mongod
sudo systemctl enable mongod

Confirm the version of the MongoDB server:

mongod --version

Step 3. Configuring MongoDB.

The MongoDB configuration file is named mongod.conf.

sudo nano /etc/mongod.conf
security:
  authorization: enabled

The authorization the option enables Role-Based Access Control (RBAC) that regulates users access to database resources and operations. If this option is disabled each user will have access to all databases and perform any action.

After editing the MongoDB configuration file, restart the mongod service for changes to take effect:

sudo systemctl restart mongod

Step 4. Create Admin User

By default, MongoDB doesn’t authenticate users to read and modify data, and it is the biggest security concern. To enable authentication, first, create a database administrative user.

Step 5. Access MongoDB.

Connect to MongoDB shell by using the following command:

$ mongo
OR
$ mongo <DBHOST_IP_ADDRESS>

Congratulation, you have learned how to install and configure Install MongoDB on Ubuntu 20.04 LTS Focal Fossa. If you have any question, please leave a comment below.