How to Install MongoDB on Ubuntu 22.04: Step-by-Step Guide
MongoDB is a leading NoSQL database system renowned for its performance, scalability, and flexibility. This guide walks you through the installation process of MongoDB on Ubuntu 22.04, enabling you to harness its robust capabilities for your projects.
If you're looking to set up MongoDB on your Ubuntu system, having a basic grasp of essential Linux terminal commands and familiarity with Ubuntu as your OS is key. Ensure that you're using a 64-bit architecture OS since MongoDB installation is tailored for this platform.
Prerequisites
Before proceeding, ensure you have:
-
A cloud server running Ubuntu 22.04.
-
A root user or a user with
sudo
privileges. -
Access to the server via SSH.
Step 1: Preparing the Ubuntu System and Authorizing via SSH
- Connect to your cloud server via SSH:
ssh username@your_server_ip
Replace username
with your actual username and your_server_ip
with your server's IP address. You'll be prompted to enter your user's password.
- Once logged in, update the package index and upgrade installed packages with the commands:
sudo apt update && sudo apt upgrade -y
Step 2: Adding MongoDB Repository
- Import the MongoDB GPG key for package verification.
To import the MongoDB GPG open key for version 6.0, begin by opening your terminal. Next, input the command below to fetch and add the key:
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
Executing this command will return an 'OK
' status, confirming that the key has been added to the MongoDB instance. You will also see a warning that apt-key
is deprecated, however, the installation process will still work.
Once you've done so, the key import process should proceed smoothly. However, if your system doesn't have GNU Privacy Guard (gnupg
) installed, you may encounter an error. To resolve this, use the following terminal command to install the program:
sudo apt install -y gnupg2
Then run the import command again. It should be successful.
To verify that the key has been added successfully, you can run the command:
apt-key list
Executing this command will display a list of keys, and you should find the MongoDB key among them in the output.
- Add the MongoDB repository to your system:
You’re at a point where your APT setup isn’t yet aware of where to locate the mongodb-org
package for installing MongoDB's latest version.
APT, when fetching and installing packages, scans two specific locations on your server for online sources: the sources.list
file and the sources.list.d
directory. The sources.list
file itemizes active APT data sources, each on a separate line, prioritizing the most preferred ones. Meanwhile, the sources.list.d
directory is where you can incorporate additional sources as separate files.
Execute this command to generate a new file named mongodb-org-7.0.list
within the sources.list.d
directory.
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
- Update the package index once more.
Now that you’ve executed the command, proceed by refreshing your server’s local package index. This step is crucial for ensuring APT can locate the mongodb-org
package:
sudo apt-get update
Step 3: Installing MongoDB
With the package index updated, you’re ready to install MongoDB:
- Install MongoDB using the command:
sudo apt-get install -y mongodb-org
When prompted, press Y and then ENTER to confirm your intention to install the package.
Once the installation process is complete, MongoDB will be successfully installed on your system. However, it’s not yet operational. Your next step involves starting MongoDB and verifying its functionality.
- Start the MongoDB service and enable it to start automatically on boot.
You'll need to manually start the MongoDB service using the systemctl command. Here's the command you should run:
sudo systemctl start mongod
Once you’ve verified that the MongoDB service is running as expected, you should enable it to start automatically at boot. Here’s the command to achieve that:
sudo systemctl enable mongod
Executing this command will configure the MongoDB service to start up automatically every time the system boots up or is restarted.
Step 4: Verifying MongoDB Installation
Check the MongoDB service status to ensure it's running.
To check the status of the MongoDB service, you can use the systemctl
command without specifying the .service
suffix. Here's how you do it:
sudo systemctl status mongod
This command will return an output similar to the following, confirming that the MongoDB service is operational and running smoothly:
An active (running)
status indicates MongoDB is functioning properly.
Step 5: Managing MongoDB Service
To stop MongoDB, use the command:
sudo systemctl stop mongod
To restart MongoDB, use the command:
sudo systemctl restart mongod
To disable MongoDB, use the command:
sudo systemctl disable mongod
Conclusion
Congratulations on successfully completing the MongoDB installation on Ubuntu 22.04! You've taken a significant step towards leveraging MongoDB's capabilities to efficiently store, manage, and retrieve your data. MongoDB's flexible and scalable nature makes it an ideal choice for a wide range of applications, from small-scale projects to enterprise-level solutions.
Furthermore, remember to prioritize security by following best practices and regularly updating your MongoDB instance. Securing your database environment helps safeguard sensitive data and ensures compliance with industry regulations.