MongoDB, a powerful and versatile NoSQL database, is widely used for its ability to handle large volumes of diverse data types with ease. Whether you are developing a web application, managing big data, or leveraging real-time analytics, MongoDB offers the flexibility and scalability required for modern data-driven applications. However, to fully utilize MongoDB’s capabilities, it is crucial to install it correctly and ensure that it is securely configured.

This guide will walk you through the process of installing and securing MongoDB on Ubuntu 22.04.

Step 1 – Update System Packages

First, update your system packages to ensure you have the latest updates and security patches.

apt update && apt upgrade -y

This command will update the package list and upgrade all the installed packages to their latest versions.

Next, you will also need to install libssl to your server. You can download and install it using the following commands:

wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.0g-2ubuntu4_amd64.deb
dpkg -i libssl1.1_1.1.0g-2ubuntu4_amd64.deb

Step 2 – Import the MongoDB GPG Key

MongoDB provides a GPG key to ensure the integrity of the packages. Import this key using the following command:

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

You should see an output saying OK, indicating the key has been successfully added.

Step 3 – Add MongoDB Repository

Next, add the MongoDB repository to your system’s sources list.

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-4.4.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.0.list

This command will add the MongoDB repository to your sources list, making MongoDB packages available for installation.

Step 4 – Install MongoDB

Now, install MongoDB using the following command:

apt update
apt install mongodb-org

This will install the latest version of MongoDB along with all necessary dependencies.

Now, verify the MongoDB installation using the following command:

mongo --version

Output:

MongoDB shell version v4.4.29
Build Info: {
    "version": "4.4.29",
    "gitVersion": "f4dda329a99811c707eb06d05ad023599f9be263",
    "openSSLVersion": "OpenSSL 1.1.0g  2 Nov 2017",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "ubuntu2004",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Step 5 – Start and Enable MongoDB Service

After the installation, start the MongoDB service and enable it to start on boot.

systemctl start mongod
systemctl enable mongod

To verify that MongoDB is running, use the command:

systemctl status mongod

You should see output indicating that the MongoDB service is active and running.

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Mon 2024-07-15 11:33:18 UTC; 5s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 7581 (mongod)
     Memory: 64.7M
        CPU: 788ms
     CGroup: /system.slice/mongod.service
             └─7581 /usr/bin/mongod --config /etc/mongod.conf

Step 6 – Secure MongoDB

By default, MongoDB does not enforce authentication. To secure your database, you need to enable authentication.

Open the MongoDB configuration file:

nano /etc/mongod.conf

Find the security section and add the following lines:

security:
  authorization: "enabled"

Save and close the file, then restart MongoDB:

systemctl restart mongod

Access the MongoDB shell:

mongo

Switch to the admin database:

use admin

Create the administrative user:

db.createUser({
  user: "admin",
  pwd: "strongpassword",
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})

Replace "strongpassword" with a strong password of your choice. After creating the user, exit the MongoDB shell:
exit

Step 7 – Verify MongoDB Connectivity

After configuring SSL/TLS for MongoDB, it’s important to verify that the authentication is working as expected. We will do this by connecting to the MongoDB instance using the mongosh shell.
If you haven’t already installed the MongoDB shell, you can do so with the following command:

apt install mongodb-mongosh

Use the mongosh shell to connect to your MongoDB instance with authentication.

mongosh --host localhost --port 27017 -u admin -p --authenticationDatabase admin

Replace your_mongodb_server_ip with the IP address of your MongoDB server. You will be prompted to enter the password for the admin user. If the connection is successful, you should see the MongoDB shell prompt:

Enter password: **************
Current Mongosh Log ID:	6695102f9cd95f2e6b482f8a
Connecting to:		mongodb://@localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+2.2.12
Using MongoDB:		4.4.29
Using Mongosh:		2.2.12

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

Warning: Found ~/.mongorc.js, but not ~/.mongoshrc.js. ~/.mongorc.js will not be loaded.
  You may want to copy or rename ~/.mongorc.js to ~/.mongoshrc.js.
test> 

Conclusion

In this article, we covered the installation and security setup for MongoDB on Ubuntu 22.04. You learned how to install MongoDB, enable authentication, create an administrative user, and verify the secure connection. By following these steps, you have ensured that your MongoDB instance is both operational and secure. You can deploy the MongoDB database server on dedicated server hosting from Atlantic.Net!