How to Install MongoDB 4.2 on Debian 10/9/8

How to Install MongoDB 4.2 on Debian 10/9/8

How to Install MongoDB 4.2 on Debian 10/9/8

To install MongoDB 4.2 on Debian 10/9/8, follow these steps. This guide involves adding the MongoDB 4.2 repository, installing the MongoDB packages, and starting the MongoDB service.

1. Add MongoDB 4.2 Repository

MongoDB provides official repositories for Debian-based distributions. Since MongoDB 4.2 is an older version, you need to explicitly use the MongoDB 4.2 repository.

  1. Import the MongoDB public key to your system so the package manager can verify the authenticity of the packages:

    wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
  2. Create a MongoDB 4.2 repository file under /etc/apt/sources.list.d/:

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

    For Debian 9 (Stretch), replace buster with stretch in the URL:

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

    For Debian 8 (Jessie), use jessie instead of buster or stretch:

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

2. Update System Packages

  1. Update the package index on your system:

    sudo apt-get update

3. Install MongoDB 4.2

Now that the repository has been added, you can install MongoDB 4.2.

  1. Install MongoDB using apt:

    sudo apt-get install -y mongodb-org

    This will install the mongodb-org package, which includes MongoDB, mongod, and other required binaries.

4. Start MongoDB Service

  1. Start the MongoDB service:

    sudo systemctl start mongod
  2. Enable MongoDB to start automatically at boot:

    sudo systemctl enable mongod
  3. Verify that MongoDB is running:

    sudo systemctl status mongod

    You should see output indicating that MongoDB is active and running, like:

    Active: active (running)

5. Verify MongoDB Installation

  1. Access the MongoDB shell:

    mongo

    This will connect you to the MongoDB shell, and you should see a prompt similar to this:

    MongoDB shell version v4.2.x

    You can check the MongoDB version to confirm installation:

    db.version()

    This should output the version 4.2.x.

6. Configure Firewall (Optional)

If your system is running a firewall, you will need to open port 27017 (MongoDB’s default port) to allow remote connections.

  1. Allow port 27017 through the firewall:

    sudo ufw allow 27017
  2. If using firewalld, open the port:

    sudo firewall-cmd --permanent --zone=public --add-port=27017/tcp sudo firewall-cmd --reload

7. Enable Authentication (Optional)

For security purposes, it’s recommended to enable authentication to ensure that only authorized users can access MongoDB.

  1. Edit the MongoDB configuration file:

    sudo vi /etc/mongod.conf
  2. Under the security section, add the following:

    security: authorization: "enabled"
  3. Save the file and restart MongoDB:

    sudo systemctl restart mongod
  4. Create an admin user to manage authentication. Connect to MongoDB:

    mongo
  5. Switch to the admin database and create an admin user:

    use admin db.createUser({ user: "admin", pwd: "adminPassword123", // Replace with a secure password roles: [ { role: "root", db: "admin" } ] });
  6. Exit the shell:

    exit
  7. Reconnect to MongoDB with authentication:

    mongo --username admin --password --authenticationDatabase admin

8. Troubleshooting

  • MongoDB Not Starting: If MongoDB doesn't start, check the logs for errors:

    sudo journalctl -u mongod
  • Permissions Issues: Ensure that the MongoDB data directory (/var/lib/mongo) has proper ownership:

    sudo chown -R mongod:mongod /var/lib/mongo

9. Uninstall MongoDB (If Necessary)

If you want to uninstall MongoDB at any point, follow these steps:

  1. Stop the MongoDB service:

    sudo systemctl stop mongod
  2. Remove MongoDB packages:

    sudo apt-get purge -y mongodb-org*
  3. Optionally, remove MongoDB data directory:

    sudo rm -rf /var/lib/mongo

Summary

  • Added the MongoDB 4.2 repository for Debian 10/9/8.
  • Installed MongoDB using the apt package manager.
  • Started and enabled MongoDB to run at boot.
  • Secured MongoDB by enabling authentication and creating an admin user (optional).
  • Opened port 27017 to allow remote connections (optional).

MongoDB should now be installed and running on your Debian system! Let me know if you need help with configurations or further steps. 🚀

    Souy Soeng

    Souy Soeng

    Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

    Github

    Post a Comment

    CAN FEEDBACK
    close