Setting Up MongoDB on Ubuntu

Learn how to install and configure MongoDB on Ubuntu step-by-step to power your application’s database. This comprehensive guide covers everything from installation and service setup to basic configuration and optional security measures. By following these instructions, you’ll have MongoDB up and running smoothly on your Ubuntu machine, ready to support your application’s data needs efficiently and securely.

Setting up MongoDB on Ubuntu involves several steps including installation, configuration, and basic setup. Below are step-by-step instructions for setting up MongoDB on Ubuntu. This installation was carried out on Ubuntu 22.04.4 LTS(should work for the newer versions as well).

Install MongoDB Community Edition

1. Import the public key used by the package management system

From a terminal, install gnupg and curl if they are not already available:

sudo apt-get install gnupg curl

To import the MongoDB public GPG key, run the following command:

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 /etc/apt/sources.list.d/mongodb-org-7.0.list file 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

3. Reload local package database

sudo apt-get update

4. Install the MongoDB packages

sudo apt-get install -y mongodb-org

4. Install the MongoDB Compass (GUI Optional)

Easily explore and manipulate your database with Compass, the GUI for MongoDB. Intuitive and flexible, Compass provides detailed schema visualizations, real-time performance metrics, sophisticated querying abilities, and much more.

sudo apt-get install -y mongodb-org

Run MongoDB Community Edition

1. Start MongoDB.

sudo systemctl start mongod

2. Verify that MongoDB has started successfully.

sudo systemctl status mongod

This should display similar output as shown below

$ sudo systemctl status mongod
[sudo] password for putta: 
 mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor pres>
     Active: active (running) since Wed 2024-05-08 12:54:07 IST; 1h 56min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 275211 (mongod)
     Memory: 78.6M
        CPU: 31.213s
     CGroup: /system.slice/mongod.service
             └─275211 /usr/bin/mongod --config /etc/mongod.conf

May 08 12:54:07 putta-PowerEdge-T30 systemd[1]: Started MongoDB Database Serv>
May 08 12:54:07 putta-PowerEdge-T30 mongod[275211]: {"t":{"$date":"2024-05-08>
lines 1-12/12 (END)

3. Stop MongoDB. (Optional)

sudo systemctl stop mongod

4. Restart MongoDB. (Optional)

sudo systemctl restart mongod

5. Begin using MongoDB.

mongosh

Now you will see a MongoDB shell, where you can issue the queries. If you have reached here, it means that you have successfully installed MongoDB on your system.

Prabodh C P is a faculty in the Dept of CSE SIT, Tumkur and also currently a Research Scholar pursuing PhD in IIT Hyderabad. He conducts online classes for C, C++, Python. For more info call +919392302100

Leave a Reply

Your email address will not be published. Required fields are marked *