How to get MongoDB up and running on Ubuntu 24.04

Having structured data simplifies lookup and maintenance compared to unstructured data. MongoDB is a NoSQL document database platform designed to overcome the limitations of traditional relational databases and other NoSQL solutions.

It employs a flexible schema for data storage, allowing updates and alterations at any time, unlike relational databases. MongoDB supports multiple operating systems, including the latest Ubuntu 24.04. This guide details the installation process.

How to Install MongoDB on Ubuntu 24.04

MongoDB excels with unstructured data and handles large volumes efficiently, making it suitable for Big Data systems, Map Reduce applications, news site forums, and social networking applications.

On Ubuntu 24.04, the most reliable method to install MongoDB involves using its official repository.

Step 1: Add the MongoDB repository to your system’s sources list. This command configures the path for the GPG key and then saves the repository link to the sources list 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

Step 2: Import the MongoDB public GPG key to verify the authenticity of the MongoDB packages.

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

Step 3: Update the apt packages list to include the MongoDB repository.

sudo apt update

Step 4: Install MongoDB using the apt package manager. This command installs the mongodb-org package, which includes the MongoDB server, shell, and related tools.

sudo apt install -y mongodb-org

To confirm the MongoDB installation, check the installed version by running the following command.

Step 5: Verify the MongoDB installation.

mongod --version

Alternative Installation Method

On the official MongoDB website, you can find .deb and .tar files available for download. These files can be downloaded by selecting the appropriate platform and package, or by copying the download link and using wget in the command line.

wget https://repo.mongodb.org/apt/ubuntu/dists/jammy/mongodb-org/7.0/multiverse/binary-arm64/mongodb-org-server_7.0.7_arm64.deb

Trying to install it this way may result in broken packages and dependency issues. Fixing these issues can be time-consuming, as the necessary dependencies might not have compatible release files.

echo "deb http://security.ubuntu.com/ubuntu impish-security main" | sudo tee /etc/apt/sources.list.d/impish-security.list

How to Configure MongoDB on Ubuntu 24.04

After installing MongoDB, configuration is necessary for smooth operation. MongoDB is often inactive after installation, so verify its service status.

Step 1: Check the status of the MongoDB service.

sudo systemctl status mongod

Step 2: Enable and start the MongoDB service. If the service is inactive, enable it to start on boot and then start it manually.

sudo systemctl enable mongod

sudo systemctl start mongod

Step 3: Launch the MongoDB shell. This command opens the mongosh interface, allowing you to interact with the MongoDB database.

mongosh

By default, no user is added to the database, and no authentication is required. You can create an admin user to enhance security.

Step 4: Switch to the admin database.

use admin

Step 5: Create a new user with the necessary roles. Replace <desired-user-name> and <desired-password> with your desired credentials.

db.createUser(
{
user: "<desired-user-name>",
pwd: "<desired-password>",
roles: [ { role: "root", db: "admin" } ]
}
)

Step 6: Verify the user creation by listing MongoDB users.

show users

To secure the database, enable authentication and configure access for specified systems by setting their IP addresses and the default port (27017) in the MongoDB configuration file.

security:
authorization: enabled

net:
Port: 27017
bindIp: <give-IPaddress>

To allow communication between the MongoDB server and other systems, configure the firewall to allow traffic on port 27017.

Step 7: Allow port 27017 through the firewall.

sudo ufw allow 27017

Step 8: Restart the MongoDB service to apply the changes, and verify that the port is communicating successfully.

sudo systemctl restart mongod

sudo lsof -i | grep mongo

How to Remove MongoDB on Ubuntu 24.04

To completely remove MongoDB from Ubuntu 24.04, follow these steps.

Step 1: Uninstall MongoDB using the apt package manager. The --autoremove option removes any dependencies that are no longer needed.

sudo apt remove --autoremove mongodb-org

Step 2: Remove the MongoDB repository directory from the sources.list.d folder.

sudo rm /etc/apt/sources.list.d/mongodb-org-7.0.list

Step 3: Remove the GPG key added to the Ubuntu keyring directory.

sudo rm /usr/share/keyrings/mongodb-server-7.0.gpg

MongoDB is a NoSQL document database used for applications needing flexible schemas and large data volumes; installing via the official repository is the recommended method. After installation, configuring authentication and firewall settings enhances security.