Installing MongoDB on CentOS

 

Installing MongoDB on CentOS: A Step-by-Step Guide

MongoDB is a popular NoSQL database known for its flexibility, scalability, and ease of use. In this guide, we will walk you through the process of installing MongoDB on a CentOS operating system.

Prerequisites

Before we begin, ensure that you have the following:

  • A CentOS server with root or sudo privileges
  • An internet connection

Step 1: Update the System

First, update your system packages to the latest versions.


sudo yum update -y



Step 2: Add MongoDB Repository

To install MongoDB, we need to add its repository to the system.

  1. Create a /etc/yum.repos.d/mongodb-org-6.0.repo file using a text editor like nano or vi.
      sudo nano /etc/yum.repos.d/mongodb-org-6.0.repo

    2.Add the following content to the file:

[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc

Save and close the file.

Step 3: Install MongoDB

Install MongoDB packages using the following command:

sudo yum install -y mongodb-org

This command installs the following packages:

  • mongodb-org-server: The mongod daemon and associated configuration files
  • mongodb-org-mongos: The mongos daemon
  • mongodb-org-shell: The mongo shell
  • mongodb-org-tools: MongoDB tools (e.g., mongodump, mongorestore)

Step 4: Start and Enable MongoDB

Start the MongoDB service and enable it to start on boot

sudo systemctl start mongod

sudo systemctl enable mongod

Step 5: Verify MongoDB Installation

Verify that MongoDB is running by checking its status.

sudo systemctl status mongod

You should see an output indicating that MongoDB is active and running.

Step 6: Configure MongoDB (Optional)

By default, MongoDB listens on localhost (127.0.0.1). If you want MongoDB to be accessible from a different IP address, you need to modify the MongoDB configuration file.

  1. Open the MongoDB configuration file.
sudo nano /etc/mongod.conf

Find the net section and update the bindIp value to your desired IP address.

net:
  port: 27017
  bindIp: 127.0.0.1,192.168.1.100

Replace 192.168.1.100 with your server's IP address.

  1. Save and close the file.

  2. Restart MongoDB to apply the changes.

sudo systemctl restart mongod


Step 7: Secure MongoDB

It's important to secure your MongoDB installation, especially if it's exposed to the internet.

  1. Create an administrative user.
mongo

use admin
db.createUser({
  user: "admin",
  pwd: "yourpassword",
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})
exit


Replace yourpassword with a strong password.

  1. Enable authentication by editing the MongoDB configuration file.
sudo nano /etc/mongod.conf

Add the following line under the security section.

security:
  authorization: enabled

  • Save and close the file.

  • Restart MongoDB to apply the changes.

  • sudo systemctl restart mongod

  • Step 8: Connect to MongoDB

    Connect to MongoDB using the mongo shell and the administrative user you created.

  • mongo -u admin -p --authenticationDatabase admin

  • Enter your password when prompted.

    Conclusion

    Congratulations! You have successfully installed MongoDB on CentOS. You can now start using MongoDB to build powerful and scalable applications. Remember to regularly update your MongoDB installation and secure it against unauthorized access.


  • Comments

    Popular posts from this blog