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 -yStep 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.listStep 4: Update Package List
After adding the repository, update the package list to include the new MongoDB repository.
$ sudo apt updateStep 5: Install MongoDB Packages
Install MongoDB Community Edition packages using the following command:
$ sudo apt install -y mongodb-orgStep 6: Start and Enable MongoDB Service
Start the MongoDB service and enable it to start on boot.
$ sudo systemctl start mongod
$ sudo systemctl enable mongodStep 7: Verify MongoDB Installation
To verify that MongoDB is installed and running correctly, check the status of the MongoDB service.
$ sudo systemctl status mongodYou 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:
$ mongoThis 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.confFind the security section and add the following line:
security:
  authorization: enabledSave the file and exit the text editor. Restart the MongoDB service to apply the changes.
$ sudo systemctl restart mongodCreate an Administrative User
Access the MongoDB shell and create an administrative user.
$ mongoSwitch to the admin database.
use adminCreate a new user with the userAdminAnyDatabase role.
db.createUser(
  {
    user: "admin",
    pwd: "your_password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)Exit the MongoDB shell.
exitAuthenticate with the Administrative User
Access the MongoDB shell using the administrative user credentials.
$ mongo -u admin -p --authenticationDatabase adminConclusion
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!
