Install Mongodb on Linux Ubuntu

admin11 February 2024Last Update :

Embarking on the MongoDB Installation Journey on Ubuntu Linux

Install Mongodb on Linux Ubuntu

MongoDB, a leading NoSQL database, is renowned for its scalability, flexibility, and performance. It’s a document-oriented database that stores data in JSON-like formats, making it an ideal choice for modern web applications. In this comprehensive guide, we’ll embark on a step-by-step journey to install MongoDB on a Linux Ubuntu system. Whether you’re a developer, database administrator, or just a tech enthusiast, this guide will equip you with the knowledge to set up MongoDB and get it running smoothly on your Ubuntu machine.

Prerequisites for MongoDB Installation

Before we dive into the installation process, it’s essential to ensure that your system meets the necessary requirements. Here’s what you’ll need:

  • An Ubuntu Linux system (preferably the latest LTS version)
  • Access to a terminal window/command line
  • sudo privileges or access to the root user
  • An internet connection to download the necessary packages

With these prerequisites in place, you’re ready to proceed with the installation.

Step-by-Step Guide to Installing MongoDB on Ubuntu

Installing MongoDB on Ubuntu involves a series of steps that we’ll go through in detail. Follow these steps carefully to ensure a successful installation.

Step 1: Importing the MongoDB Repository

Ubuntu’s default repositories don’t always contain the latest version of MongoDB. To ensure we get the most recent release, we’ll import the MongoDB repository directly from the source.

  1. Open your terminal window.
  2. Import the public key used by the package management system with the following command:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
  1. Create a list file for MongoDB by running:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

This command adds the repository to your system and ensures you’re downloading the official MongoDB package.

Step 2: Installing MongoDB Packages

With the repository in place, it’s time to install the MongoDB packages.

  1. Update the package database:
sudo apt-get update
  1. Install the MongoDB package:
sudo apt-get install -y mongodb-org

This command installs several packages, including the MongoDB server mongod, the MongoDB shell mongo, and other MongoDB tools.

Step 3: Starting MongoDB and Testing the Installation

After installation, you’ll need to start the MongoDB service and verify that it’s running correctly.

  1. Start the MongoDB service with:
sudo systemctl start mongod
  1. Check the service’s status to ensure it’s active:
sudo systemctl status mongod

If MongoDB has started successfully, you’ll see an output indicating that the service is active and running.

Step 4: Enabling MongoDB to Start on Boot

To ensure MongoDB starts automatically when your system boots, enable it with the following command:

sudo systemctl enable mongod

This step is optional but recommended for a production environment or if you want MongoDB to be always available.

Step 5: Verifying the Installation

To confirm that MongoDB is installed and functioning correctly, connect to the database using the MongoDB shell.

mongo

This command will open the MongoDB shell, and you should see the MongoDB version and a welcome message.

Securing MongoDB on Ubuntu

After installing MongoDB, it’s crucial to secure your database to prevent unauthorized access. Here are some steps to enhance the security of your MongoDB installation:

Creating an Administrative User

Start by creating an administrative user with the necessary privileges to manage the database.

  1. Access the MongoDB shell:
mongo
  1. Switch to the admin database:
use admin
  1. Create an administrative user:
db.createUser({
  user: "admin",
  pwd: "your_secure_password",
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})

Replace “your_secure_password” with a strong password of your choice.

Enabling Authentication

To enforce authentication, you’ll need to edit the MongoDB configuration file and enable authorization.

  1. Open the MongoDB configuration file in your preferred text editor:
sudo nano /etc/mongod.conf
  1. Locate the security section and add the following line:
security:
  authorization: "enabled"
  1. Save the file and exit the text editor.
  2. Restart the MongoDB service to apply the changes:
sudo systemctl restart mongod

With authentication enabled, you’ll need to use the administrative user credentials to access the MongoDB shell or perform administrative tasks.

Managing MongoDB Service on Ubuntu

Understanding how to manage the MongoDB service is essential for maintaining your database. Here are some common service management commands:

  • Starting the service: sudo systemctl start mongod
  • Stopping the service: sudo systemctl stop mongod
  • Restarting the service: sudo systemctl restart mongod
  • Checking the service status: sudo systemctl status mongod

These commands will help you control the MongoDB service as needed.

FAQ Section

What is MongoDB?

MongoDB is an open-source NoSQL database that uses a document-oriented data model. It’s designed for high availability, scalability, and performance with a flexible schema.

Why use MongoDB on Ubuntu?

Ubuntu is a popular Linux distribution known for its stability and security. Combining Ubuntu with MongoDB provides a robust platform for developing and deploying applications that require a scalable database.

How do I update MongoDB on Ubuntu?

To update MongoDB, you can use the following commands:

sudo apt-get update
sudo apt-get upgrade mongodb-org

These commands will update the MongoDB packages to the latest version available in the repository.

Can I install a specific version of MongoDB?

Yes, you can install a specific version by specifying the package version in the installation command. For example:

sudo apt-get install -y mongodb-org=4.4.0 mongodb-org-server=4.4.0 mongodb-org-shell=4.4.0 mongodb-org-mongos=4.4.0 mongodb-org-tools=4.4.0

Replace “4.4.0” with the desired version number.

How do I uninstall MongoDB from Ubuntu?

To uninstall MongoDB, use the following commands:

sudo systemctl stop mongod
sudo apt-get purge mongodb-org*
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

These commands will stop the service, remove MongoDB packages, and delete its data and log directories.

Conclusion

Installing MongoDB on Ubuntu Linux is a straightforward process that opens up a world of possibilities for developers and database administrators. By following this guide, you’ve learned how to import the MongoDB repository, install the necessary packages, secure your database, and manage the MongoDB service. With MongoDB now set up on your Ubuntu system, you’re ready to start developing powerful applications backed by a robust and scalable NoSQL database.

Remember to keep security in mind and regularly update your MongoDB installation to benefit from the latest features and security patches. Happy coding!

References

Leave a Comment

Your email address will not be published. Required fields are marked *


Comments Rules :