[2019]How to Install MongoDB 4.0 On CentOS 7

How to Install MongoDB 4.0 On CentOS 7:-

Introduction :-

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 . 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 7. I thought to help him and write down the steps.

Let me also list down the problems he 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.

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.

name=MongoDB Repository

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

How to 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.

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

Follow my previous Post for rest of the Steps .

Enable MongoDB access using Machine Ip address:-

Let’s try to access MongoDB using machine IP.

 mongo --host <ip address>:<port> --verbose
# mongo --host --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
  port: 27017
  bindIp:,  # Enter,:: 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 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 .

Enable Authentication in MongoDB

Enable Authentication in MongoDB:-

Most RDBMS like MySQL, Oracle, SQLServer comes with inbuilt authentication setup with simple authentication. MongoDB has a different feature, it doesn’t comes with inbuilt basic authentication setup.There is no restriction to the MongoDB database inbuilt, any user can access any database.In this blog post, we are going to see how we can setup user authentication to our mongo database server and schema level.

Our Requirement:-

  1. We are going to authenticate database connection with an Admin User.
  2. We are going to create a User which as Owner permission to the specific database in MongoDb

Step 1: Connect MongoDb Without Authentication :-

We don’t have any authentication set in our mongo server so we can login by typing following command

$ mongo mongodb://<host>:<port>

Our host is in case we are in local machine and port by default is 27017 .Now lets enable authentication in mongodb .

Step 2: Create a User:-

Open your Mongo shell and enter the command below to Switch to admin database .

First of all, you need to create an administrator user. I prefer creating super user.

> use admin

Now We need to create a user which has root access to the database . Provides access to the operations and all the  resources ofreadWriteAnyDatabase,dbAdminAnyDatabaseuserAdminAnyDatabase clusterAdminroles, restoreand rolesbackup combined.

This user has all access to do any operation on any other databases including admin database.

> db.createUser({user:"admin",
                        }] });

Step 3: Configuring Mongod authentication configuration:-

Open a file mongod.conf located under etc using any editor of your choice.

$ sudo vi /etc/mongod.conf

You will see something like this:-


    authorization: "enabled"

Locate #security and add the following line just below of that, save and exit from your editor . Your updated file should look like the image below.


Step 4 : Restart MongoDB:

$ sudo service mongod restart

Step 5 : Connect to Mongo and Authenticate admin User:-

Let’s connect to mongo shell and execute following commands to test the user we have just created.

MongoDB shell version: 3.0.15
connecting to: test

> use admin


You will get some error saying:-

> show collections
2018-02-26T10:54:40.800-0500 E QUERY    Error: listCollections failed: {
        "ok" : 0,
        "errmsg" : "not authorized on admin to execute command { listCollections: 1.0 }",
        "code" : 13
    at Error (<anonymous>)
    at DB._getCollectionInfosCommand (src/mongo/shell/db.js:646:15)
    at DB.getCollectionInfos (src/mongo/shell/db.js:658:20)
    at DB.getCollectionNames (src/mongo/shell/db.js:669:17)
    at shellHelper.show (src/mongo/shell/utils.js:625:12)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/db.js:646

It says you are not authorized to view collections inside admin database hence you need to authenticate yourself. This shows that your database has enabled authentication in MongoDB.

Let’s authenticate with the user we created .

> db.auth("admin","admin")
> db.getUsers();
                "_id" : "admin.admin",
                "user" : "admin",
                "db" : "admin",
                "roles" : [
                                "role" : "root",
                                "db" : "admin"

We must get the output of db.auth() as 1.Now you have successfully enable authentication in MongoDB.

Let’s Create a database and create a Separate User for the database and try to enable authentication in MongoDB for that database.

Step 6 :Create Database, User and Enable Authentication for Specific Database:-

We are going to create a user with a role as  DbOwner for the database named as testMongo.

>use testMongo
switched to db testMongo

> db.createUser(
    user: "testMongoAdmin",
    pwd: "testMongoAdmin",
    roles: [ { role: "dbOwner", db: "testMongo" } ]

Now you don’t need to log in as admin in every database.We can use database specific users to access database and use admin user to manage all databases.