w3resource

Step-by-Step Installation Guide for MongoDB Community Edition on Debian


Install MongoDB Community Edition on Debian

MongoDB Community Edition is a powerful, open-source NoSQL database ideal for modern applications. Installing MongoDB on Debian involves adding the MongoDB repository, installing the necessary packages, and configuring the database for operation.

This guide provides a step-by-step tutorial to install MongoDB Community Edition on Debian systems.


Prerequisites

Before you start, ensure:

    1. Operating System: Debian 10 (Buster) or Debian 11 (Bullseye).

    2. Root Access: Administrative privileges with sudo.

    3. Internet Connection: Required for downloading MongoDB packages.


Step 1: Import MongoDB's Public GPG Key

Download and import the MongoDB GPG key to verify the authenticity of the packages:

# Import the MongoDB GPG key
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor

Step 2: Add MongoDB Repository

Configure the MongoDB repository in your system:

    For Debian 10 (Buster):

    # Add MongoDB repository for Debian 10
    echo "deb [signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg] https://repo.mongodb.org/apt/debian buster/mongodb-org/6.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
    

    For Debian 11 (Bullseye):

    # Add MongoDB repository for Debian 11
    echo "deb [signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg] https://repo.mongodb.org/apt/debian bullseye/mongodb-org/6.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
    

Step 3: Update the Package List and Install MongoDB

    1. Update the package list to include the MongoDB repository:

    # Update system package list
    sudo apt update
    

    2. Install MongoDB:

    # Install MongoDB Community Edition
    sudo apt install -y mongodb-org
    

Step 4: Start and Enable MongoDB Service

    1. Start the MongoDB service:

    # Start MongoDB service
    sudo systemctl start mongod
    

    2. Enable MongoDB to start at boot:

    # Enable MongoDB to start on boot
    sudo systemctl enable mongod
    

Step 5: Verify MongoDB Installation

To confirm that MongoDB is installed and running correctly:

1. Check the status of MongoDB:

# Check MongoDB service status
sudo systemctl status mongod

If the service is running, you should see active in the output.

2. Open the MongoDB shell:

# Access MongoDB shell
mongosh

Step 6: Configure Security (Optional)

Enable Authentication

    1. Open MongoDB's configuration file:

    sudo nano /etc/mongod.conf
    

    2. Add the following under the security section:

    security:
      authorization: "enabled"
    

    3. Restart the MongoDB service:

    sudo systemctl restart mongod
    

Step 7: Configure Firewall (Optional)

If you want to allow remote access to MongoDB, open port 27017:

# Allow MongoDB port in the firewall
sudo ufw allow 27017/tcp

Example Commands for MongoDB on Debian

Insert a Document

Code:


// Insert a sample document into the 'users' collection
db.users.insertOne({ name: "Debian User", age: 30, role: "Admin" });

Query a Collection

Code:


// Find all documents in the 'users' collection
db.users.find();

Troubleshooting Tips

Issue Solution
MongoDB service fails to start Check the logs at /var/log/mongodb/mongod.log for detailed error messages.
Package signing error Ensure the MongoDB GPG key is correctly imported and the repository URL matches your OS version.
Connection refused remotely Check firewall settings and ensure MongoDB is listening on the correct IP address.

Additional Notes:

  • Compatibility: Ensure your MongoDB version matches your Debian distribution.
  • Backup: Back up important data regularly, especially before making configuration changes.
  • Updates: Periodically check for MongoDB updates using sudo apt update and sudo apt upgrade.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/installation-mongodb-on-debian.php