Installation of MongoDB on Ubuntu

Installation of MongoDB on Ubuntu

Overview

This tutorial is a guide to installing MongoDB Community Edition on LTS (long-term support) releases of Ubuntu Linux using the apt package manager.

Install MongoDB Community Edition Follow these steps to install MongoDB Community Edition using the apt package manager. Import the public key used by the package management system.

Open up the terminal and input the following command.

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

The operation should respond with an OK.

If you get any error indicating that GnuPG is not installed, you can:

Install GnuPG and its required libraries using the following command:

sudo apt-get install gnupg

When you are done with that, try again importing the key:

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

Adding repo / Create a list file for MongoDB.

There are three versions of Ubuntu.

Ubuntu 20.04 (Focal) Ubuntu 18.04 (Bionic) Ubuntu 16.04 (Xenial)

This instruction is for the 1st one Ubuntu 20.04 (Focal).

Create the /etc/apt/sources.list.d/mongodb-org-4.4.list file for Ubuntu 20.04 (Focal):

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

Update the local package database.

sudo apt-get update

Install the latest version of MongoDB.

sudo apt-get install -y mongodb-org

Run MongoDB

Directories setting.

If you installed via the package manager, the data directory /var/lib/mongodb and the log directory /var/log/mongodb are created during the installation.

Start MongoDB

You can start the mongod process with the following command:

sudo systemctl start mongod

If you receive an error similar to the following when starting mongod: Failed to start mongod.service: Unit mongod.service not found. Run the following command first:

sudo systemctl daemon-reload

Then run the above starting command again.

Checks whether MongoDB has started successfully.

sudo systemctl status mongod

You can optionally set when your system reboot or restarts MongoDB should automatically run.

sudo systemctl enable mongod

Stop MongoDB

You can stop the mongod with the following command:

sudo systemctl stop mongod

Restart MongoDB

You can restart the MongoDB with the following command:

sudo systemctl restart mongod

Start using MongoDB

Open another window of the terminal and run the following command to start a mongo shell on the same host machine as the mongod.

mongo

Removing / Uninstalling MongoDB

To completely remove the mongodb from your system first you have to stop it.

Stopping MongoDB

Stop the mongod process with the following command:

sudo service mongod stop

Remove Packages

Remove all the installed MongoDB packages on your system with the following command.

sudo apt-get purge mongodb-org*

Removing Data Directories

Remove MongoDB databases and their log files from your system with these commands.

sudo rm -r /var/log/mongodb sudo rm -r /var/lib/mongodb

Author Kashif Sanjrani

Thanks!