How to Install MongoDB 4.4 on Ubuntu 18.04 & 16.04 via PPA

How to Install MongoDB 4.4 on Ubuntu 18.04 & 16.04 via PPA

 How to Install MongoDB 4.4 on Ubuntu 18.04 & 16.04 via PPA

To install MongoDB 4.4 on Ubuntu 18.04 or 16.04 using a PPA (Personal Package Archive), follow these steps. The installation involves adding the MongoDB 4.4 PPA, installing the MongoDB packages, and configuring the MongoDB service.

1. Import MongoDB 4.4 Public Key

MongoDB requires a public key to ensure the authenticity of the packages being installed.

  1. Import the MongoDB 4.4 public key:

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

    This will add the MongoDB public GPG key to your system.

2. Add the MongoDB 4.4 Repository

Once the key is added, you'll need to add the MongoDB 4.4 repository to your system.

  1. Add the MongoDB repository for Ubuntu 18.04 (Bionic):

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

    For Ubuntu 16.04 (Xenial), replace bionic with xenial:

    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

3. Update Package List

After adding the MongoDB repository, you need to update your system’s package index to include the MongoDB packages.

  1. Update package list:

    sudo apt-get update

4. Install MongoDB 4.4

Once the repository is added and the package list is updated, you can proceed to install MongoDB.

  1. Install MongoDB 4.4:

    sudo apt-get install -y mongodb-org

    This will install the MongoDB 4.4 package along with its dependencies (mongod, mongo, and other related tools).

5. Start MongoDB Service

After installation, you can start the MongoDB service.

  1. Start MongoDB:

    sudo systemctl start mongod
  2. Enable MongoDB to start on boot:

    sudo systemctl enable mongod
  3. Check the status of MongoDB:

    sudo systemctl status mongod

    This will show if MongoDB is running. You should see Active: active (running) in the output.

6. Verify MongoDB Installation

To verify that MongoDB is correctly installed and running, access the MongoDB shell:

  1. Access the MongoDB shell:

    mongo

    This will bring up the MongoDB shell where you can run commands.

  2. Check MongoDB version:

    Once inside the shell, check the version of MongoDB by running:

    db.version()

    This should return the version 4.4.x.

7. Firewall Configuration (Optional)

If your system has a firewall enabled (e.g., ufw), you will need to allow traffic on MongoDB's default port 27017 to access MongoDB remotely.

  1. Allow port 27017 through the firewall (for ufw):

    sudo ufw allow 27017

    For firewalld:

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

8. Enable Authentication (Optional)

For production systems, it's essential to enable authentication to secure MongoDB.

  1. Edit the MongoDB configuration file:

    sudo vi /etc/mongod.conf
  2. Enable authentication by adding or uncommenting the following:

    security: authorization: "enabled"
  3. Restart MongoDB to apply changes:

    sudo systemctl restart mongod
  4. Create an admin user (optional):

    Access the MongoDB shell and create an admin user for authentication:

    mongo

    Switch to the admin database and create a user:

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

    Exit the shell and reconnect using the new user credentials:

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

9. Uninstall MongoDB (If Necessary)

If you want to uninstall MongoDB, you can do so using the following commands:

  1. Stop MongoDB:

    sudo systemctl stop mongod
  2. Remove MongoDB packages:

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

    sudo rm -rf /var/lib/mongo

Summary

  • Added MongoDB 4.4 repository via PPA for Ubuntu 18.04/16.04.
  • Installed MongoDB 4.4 using apt-get.
  • Started MongoDB and enabled it to start on boot.
  • Configured firewall to allow MongoDB access (optional).
  • Enabled authentication for better security (optional).

MongoDB 4.4 should now be installed and running on your Ubuntu system. Let me know if you need further assistance or configuration! 🚀

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