How to Install MongoDB On CentOS 8

Most of you must be aware of MongoDB. Mongo DB is one of the most used NOSQL database currently in market . It uses a JSON formatted data structure as compared to relational databases .

MongoDB is a NoSQL Database released in 2009 that supports a flexible schema approach. It helps developers and companies build applications quickly without spending much time configuring a Database. One can store both structured and unstructured data using MongoDB. When installed on Debian, one can utilize easy and powerful Linux features as well.

Now a days most of the applications uses mongo db as a database . its light weight and highly scalable and doesn’t involve any complex relationships. This tutorial guides you through How to Install MongoDB 4.0  on a CentOS 7 server.

Recently one of my friends had issues Configuring MongoDB 4.0 on CentOS 8. I thought to help him and write down the steps.

Let me also list down the problems We faced while Installing MongoDB 4.0 on CentOS 7.

  1. Installing On CentOS requires the addition of repository.
  2. Enable MongoDB authentication and admin user.
  3. Enable MongoDB access outside of your VM using the IP address and Port.

How to Add the MongoDB Repository in CentOS

You must log in using the root user or create a root user if you want. As you will be having a fresh Cent OS , you need to add the Mongo repo in your system.

Install MongoDB 4.0 On CentOS 7

We now need to configure Yum, Navigate to /etc/yum.repos.d/ directory. Create a repository file as below.

[root@frugalisminds]# cd /etc/yum.repos.d/
[root@frugalisminds]# vi mongodb-org-4.0.repo

Copy the configuration below save and exit.

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

Installing MongoDB 4.0 using yum

[root@frugalisminds ~]# yum -y install mongodb-org

It will ask you to download MongoDB packages, please press Y and Continue.

Once the installation is completed, start your MongoDB service using the following command.

[root@frugalisminds ~]# systemctl start mongod

Restart the MongoDB service using the following command.

[root@frugalisminds ~]# systemctl restart mongod

Create MongoDB admin User

We are creating one admin user with credential admin/admin123 .Lets login to mongo using mongo shell.

[root@frugalisminds ~]# mongo

Switch to the admin database

 use admin

Create an admin user below in mongo.

db.createUser(
  {
    user: "admin",
    pwd: "admin@123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

Enable Authentication in Mongo DB

Enable MongoDB access using Machine Ip address

Let’s try to access MongoDB using machine IP.

 mongo --host <ip address>:<port> --verbose
# mongo --host 192.168.1.19:27017 --verbose

You will get a connection error. Now we need to enable access using an IP address. Navigate to /etc/mongo.conf  find out the line where bindIp is present.

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1,192.168.1.19  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.


Update your IP address, as you can see I have entered by address as 192.168.1.19 separated by a comma.

rerun the command using the IP address, you will be logged in inside Mongo shell.

Install MongoDB 4.0 On CentOS 7

References :- Big Thanks to all the links and Videos that helped me doing this on a Fresh CentOs .

  1. No Internet Connection On CentOS 7 – Youtube.
  2. Lan Configuration CentOS – Youtube.
  3. Install MongoDB – Mongodb.com.
  4. MongoDb Security CheckList .