Installing MongoDB on ubuntu 22.04
author image

Author:

Naman AroraFri, 9 Feb, 2024

Introduction:

Welcome to our comprehensive guide on installing MongoDB 7.0 on Ubuntu Linux LTS. MongoDB is a popular NoSQL database that offers scalability and flexibility for modern applications. In this tutorial, we'll walk you through the steps to install MongoDB Community Edition on your Ubuntu LTS system.

MongoDB Version:

This tutorial covers the installation of MongoDB 7.0 Community Edition. Please note that MongoDB frequently releases updates, so ensure you're using the latest stable version available at the time of installation.

Considerations:

  • Platform Support: MongoDB 7.0 Community Edition supports 64-bit Ubuntu LTS releases, including 22.04 LTS ("Jammy") and 20.04 LTS ("Focal").
  • To determine your Ubuntu release, run the following command in your terminal:
cat /etc/lsb-release

Install MongoDB Community Edition:

1. Import the public key:

First, ensure gnupg and curl are installed:

sudo apt-get install gnupg curl

Then, import the MongoDB public GPG key:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
   --dearmor

2. Create a list file for MongoDB:

Create the list file /etc/apt/sources.list.d/mongodb-org-7.0.list for your version of Ubuntu.

  • For Ubuntu 22.04 (Jammy):
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
  • For Ubuntu 22.04 (Focal):
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

3. Reload local package database:

Issue the following command to reload the local package database:

sudo apt-get update

4. Install the MongoDB packages:

You can install either the latest stable version of MongoDB or a specific version of MongoDB.

  • For the latest version run:
sudo apt-get install -y mongodb-org
  • For any specific version run:
sudo apt-get install -y mongodb-org=7.0.5 mongodb-org-database=7.0.5 mongodb-org-server=7.0.5 mongodb-mongosh=7.0.5 mongodb-org-mongos=7.0.5 mongodb-org-tools=7.0.5

If you only install mongodb-org=7.0.5 and do not include the component packages, the latest version of each MongoDB package will be installed regardless of what version you specified.

To prevent unintended upgrades, you can pin the package at the currently installed version:

echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-database hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-mongosh hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections

Run MongoDB Community Edition

Starting in MongoDB 4.4, a startup error is generated if the ulimit value for number of open files is under 64000.

  • Directories

By default, MongoDB creates data directory /var/lib/mongodb and log directory /var/log/mongodb. Ensure appropriate permissions for these directories.

  • Configuration File

The official MongoDB package includes a configuration file (/etc/mongod.conf). These settings (such as the data directory and log directory specifications) take effect upon startup. That is, if you change the configuration file while the MongoDB instance is running, you must restart the instance for the changes to take effect.

  • Init System

To run and manage your mongod process, you will be using your operating system's built-in init system. Recent versions of Linux tend to use systemd (which uses the systemctl command), while older versions of Linux tend to use System V init (which uses the service command).

If you are unsure which init system your platform uses, run the following command:

ps --no-headers -o comm 1
  1. systemd - go for the systemd (systemctl) code below.
  2. init - go for the System V Init (service) code below.

  • Systemd (systemctl)

1. Start MongoDB

You can start the mongod process by issuing 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 start command above again.

2. Verify that MongoDB has started successfully

sudo systemctl status mongod

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:

sudo systemctl enable mongod

3. Stop MongoDB

As needed, you can stop the mongod process by issuing the following command:

sudo systemctl stop mongod

4. Restart MongoDB

sudo systemctl restart mongod

5. Begin using MongoDB

Start a mongosh session on the same host machine as the mongod. You can run mongosh without any command-line options to connect to a mongod that is running on your localhost with default port 27017.

mongosh

For more information on connecting using mongosh, such as to connect to a mongod instance running on a different host and/or port, see the mongosh documentation.

  • System V Init (Service)

1. Start MongoDB

You can start the mongod process by issuing the following command:

sudo service mongod start

2. Verify that MongoDB has started successfully

sudo service mongod status

3. Stop MongoDB

As needed, you can stop the mongod process by issuing the following command:

sudo service mongod stop

4. Restart MongoDB

sudo service mongod restart

5. Begin using MongoDB

Start a mongosh session on the same host machine as the mongod. You can run mongosh without any command-line options to connect to a mongod that is running on your localhost with default port 27017.

mongosh

For more information on connecting using mongosh, such as to connect to a mongod instance running on a different host and/or port, see the mongosh documentation.

Uninstall MongoDB Community Edition

To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs. The following section guides you through the necessary steps.

  • Stop MongoDB

Stop the mongod process by issuing the following command:

sudo service mongod stop
  • Remove Packages

Remove any MongoDB packages that you had previously installed.

sudo apt-get purge "mongodb-org*"
  • Remove Data Directories

Remove MongoDB databases and log files.

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

Conclusion

Congratulations, you've successfully installed MongoDB 7.0 Community Edition on your Ubuntu Linux LTS system, empowering your projects with a robust NoSQL database solution. Stay updated with MongoDB's latest releases and best practices for optimal performance, and feel free to explore further documentation and community resources for continued support and learning.

Happy Coding !

author image

Naman Arora

Technical Author with a passion for translating the complexities of software, computers, and emerging technologies into accessible and engaging content. Armed with a background in computer science, I blend technical expertise with a flair for effective communication in my writing. Join me on this tech-savvy journey as we explore coding languages, unravel the nuances of software architecture, and stay informed about the latest tech trends. Let's navigate the digital frontier together!

Portfolio designed and developed by Naman Arora