Home  »  ArticlesGuidesHow ToSoftwareTechnologyTools   »   How to Install MongoDB Community Edition on Debian 12 Linux Systems

How to Install MongoDB Community Edition on Debian 12 Linux Systems

MongoDB is a powerful, open-source NoSQL database that provides high performance, high availability, and easy scalability. This guide will walk you through the steps to install MongoDB Community Edition on a Debian 12 system.

Prerequisites

Before starting, ensure you have:

  • A running Debian 12 system
  • A user account with sudo privileges
  • Internet connectivity to download necessary packages

Step 1: Update Your System

First, update your system to ensure all existing packages are up to date.

$ sudo apt update
$ sudo apt upgrade -y

Step 2: Import the MongoDB Public GPG Key

MongoDB provides a GPG key to verify the authenticity of the packages. Import the MongoDB public GPG key using the following command:

$ wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -

Step 3: Create a MongoDB List File

Create a list file for MongoDB. This file contains the repository details from which MongoDB packages will be downloaded.

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/debian buster/mongodb-org/6.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

Step 4: Update Package List

After adding the repository, update the package list to include the new MongoDB repository.

$ sudo apt update

Step 5: Install MongoDB Packages

Install MongoDB Community Edition packages using the following command:

$ sudo apt install -y mongodb-org

Step 6: Start and Enable MongoDB Service

Start the MongoDB service and enable it to start on boot.

$ sudo systemctl start mongod
$ sudo systemctl enable mongod

Step 7: Verify MongoDB Installation

To verify that MongoDB is installed and running correctly, check the status of the MongoDB service.

$ sudo systemctl status mongod

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

Step 8: Configure MongoDB

MongoDB configuration files are located in the /etc/mongod.conf directory. The default configuration is typically sufficient for many users, but you can customize it to suit your specific requirements.

Access MongoDB Shell

To access the MongoDB shell, use the following command:

$ mongo

This command will connect to the MongoDB instance and drop you into the MongoDB shell.

Step 9: Secure MongoDB

It’s important to secure your MongoDB installation by enabling authentication and setting up a user with the appropriate roles.

Enable Authentication

Edit the MongoDB configuration file to enable authentication.

$ sudo nano /etc/mongod.conf

Find the security section and add the following line:

security:
  authorization: enabled

Save the file and exit the text editor. Restart the MongoDB service to apply the changes.

$ sudo systemctl restart mongod

Create an Administrative User

Access the MongoDB shell and create an administrative user.

$ mongo

Switch to the admin database.

use admin

Create a new user with the userAdminAnyDatabase role.

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

Exit the MongoDB shell.

exit

Authenticate with the Administrative User

Access the MongoDB shell using the administrative user credentials.

$ mongo -u admin -p --authenticationDatabase admin

Conclusion

You have successfully installed and configured MongoDB Community Edition on your Debian 12 system. MongoDB is now ready for you to start developing applications with a robust and scalable NoSQL database. For more detailed configurations and advanced usage, refer to the official MongoDB documentation.

Happy databasing!

References

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.