How to Install MongoDB in Ubuntu 20.04 Linux.

Hi Techies! Will look at “How to install MongoDB on Ubuntu 20.04Linux. MongoDB is a No-SQL database.

Which is written in C++, It uses a JSON like structure. MongoDB is a cross-platform and document-oriented database.

The initial release of the MongoDB was on 11 February 2009, you can find the main website of the MongoDB as well the git repository

10gen software company began developing MongoDB in 2007 as a component of a planned platform as a service product.

But in 2009, the company shifted to an open-source development model, with the company offering commercial support and other services.

So finally in 2013, 10gen changed its name to MongoDB Inc. let’s install MongoDB on Ubuntu 20.04 Linux environment.


Also, find the cheat sheet for the Quick Installation, kindly come to read the whole post for the better knowledge of MongoDB installation.

Cheat Sheet of How to Install MongoDB on Ubuntu 20.04 Linux.

STEP 1: Install the GnuPG for and add the key to your Ubuntu Server.

Ubuntu 20:

$ sudo apt install gnupg

Add the Key to Ubuntu Server 20

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

STEP 2: Add the repository of MongoDB into your Ubuntu 20.04

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

This will add the repo into the file “/etc/apt/sources.list.d/mongodb-org-4.4.list”

STEP 3: Update the repository and Install MongoDB 4.4 on Ubuntu 20.04

For Ubuntu 20:

$ sudo apt update
$ sudo apt install -y mongodb-org

STEP 4: Start the Service of MongoDB Server on Ubuntu

$ sudo systemctl start mongod

Check the Status:

$ sudo systemctl status mongod

Restart the service:

$ sudo systemctl restart mongod

Stop the service:

$ sudo systemctl stop mongod

Start the service at the time of Booting

$ sudo systemctl enable mongod

STEP 5: Use the “mongo” command in the terminal or BASH to connect with MongoDB.

$ mongo


STEP 1: Install the GnuPG and add MongoDB key to your Ubuntu 20.04.

For Ubuntu 20.04:

$ sudo apt install gnupg

After the installation of the GnuPG package let’s add the key to our server.

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

After adding it will give the output as “OK” on your terminal or bash

For the different version, we have to just make the changes in the above command, let assume we want to install version 4 of the MongoDB.

So the command to install MongoDB 4 on Ubuntu 20.04 will be:

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

STEP 2: Add the repository of MongoDB into Ubuntu Server.

For Ubuntu 20:

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

So we have added the repository of MongoDB 4.4 into the file /etc/apt/sources.list.d/mongodb-org-4.4.list.

STEP 3: Update the repository and Install MongoDB 4.4 on Ubuntu 20.04

So first, we will update the repository of MongoDB by using the update command.

For Ubuntu 20:

$ sudo apt update

So After successfully Update of the repo we will install the MongoDB server.

For Ubuntu 20:

$ sudo apt install -y mongodb-org
How to install MongoDB on ubuntu 18.04, 20.04, 16.04.
Install MongoDB Server.

STEP 4: Start the Service in MongoDB

So after the successful install, we will start the services of the MongoDB server on Ubuntu 20.04 by “systemctl command”

$ sudo systemctl start mongod
Start the service for MongoDB.
$ sudo systemctl status mongod

So, here we can see that the service of MongoDB is active and running on the Ubuntu 20.04 server.

If we have to restart the service of MongoDB then use the restart option with systemctl.

$ sudo systemctl restart mongod

If we have to stop the service of MongoDB then use the stop option with the systemctl command.

$ sudo systemctl stop mongod

What if we want to start the service when my server got rebooted then use the enable option with systemctl.

$ sudo systemctl enable mongod

So we have successfully install the MongoDB server on Ubuntu 20.04 and use “mongo” in terminal or bash to check are you able to connect.

mongo command
$ mongo

Allow MongoDB to connect from Remote IP

If you have to connect your MongoDB server from a Remote IP, then you have to make some configuration changes and restart the service of MongoDB.

So first we will look into the configuration file of the MongoDB server “/etc/mongod.conf”

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1

So you will find that the bind IP is 127.0.0.1 which will allow to local only, and we want to allow to any remote IP then change the bind IP to “0.0.0.0”

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

After that, we will require a restart to MongoDB service, so that we can access the port 27017 to any remote IP.

$ sudo systemctl restart mongod

If still not working then check your firewall, which will be active, now you have two option can inactive the firewall or you can allow the 27107 port number.

To inactive the firewall use “sudo ufw disable”, but it can be dangerous if your server has another application or you are in the production environment.

So it’s better to allow the port 27017 for remote IP’s only.

$ sudo ufw allow 27017

So by allowing this anyone can access the 27017 port to your server then check the status of ufw.

$ sudo ufw status

Output:
Status: active
To Action From
-- ------ ----
27017 ALLOW Anywhere
27017 (v6) ALLOW Anywhere (v6)

If you are facing any issue while installing MongoDB let us know, we will help you out.

Thanks to reading the Article, learn Linux basic command, and how to Install MySQL. Want to Install MongoDB in CentOS. Follow the link Install MongoDB in Amazon Linux 2 ec2.


install MongoDB on Ubuntu, install MongoDB, install MongoDB on Ubuntu 18.04, install MongoDB on Ubuntu 20.04, install MongoDB on linux, MongoDB install Ubuntu

Give your valuable time