How to Install MongoDB on CentOS 7

Filed Under: MongoDB

In this article, we dive in and see how you can install MongoDB on CentOS 7. MongoDB is a free, flexible and opensource NoSQL database, different from the regular SQL databases like PostgreSQL and MySQL. In MongoDB, data is stored in JSON format and does not require a schema.

Installing MongoDB

At the time of penning down this guide, the latest version of MongoDB was version 4.0. Before proceeding with the installation process, confirm the latest release here.

Step 1: Enabling MongoDB repository

Using your favorite text editor, create a new YUM repository called mongodb-org.repo

$ vim  /etc/yum.repos.d/mongodb-org.repo

Next, add the following content.

name=MongoDB Repository

Step 2: Installing MongoDB

With the MongoDB repository enabled, install MongoDB using the command below.

$ sudo yum install mongodb-org

Sample Output

Yum Install Mongodb CentOS 7

When prompted to import the MongoDB GPG key, type y and press ENTER.

Sample Output

Importing GPG Key For MongoDB

As part of the Mongo-org package, the following packages will also be installed.

  1. mongodb-org-server – This is the mongod daemon, plus corresponding init scripts and configurations.
  2. mongodb-org-mongos – This is the mongos daemon.
  3. mongodb-org-shell – This is the mongo shell, which is an interactive JavaScript interface to MongoDB, used to perform administrative tasks through the command line.
  4. mongodb-org-tools – This package contains several MongoDB tools used for importing and exporting data, statistics, as well as other utilities.

Step 3: Starting MongoDB

With MongoDB successfully installed, start the MongoDB daemon using the command as shown.

$ sudo systemctl start mongod

You can also enable it to start on boot by running the following command.

$ sudo systemctl enable mongod

To confirm that MongoDB daemon is running, execute:

$ sudo systemctl status mongod

Sample Output

Verify Status of Mongod daemon

Awesome, we have successfully installed MongoDB on the CentOS 7 server. Let’s now see how we can configure the database.

Step 4: Configuring MongoDB

For best practices, it’s recommended to have authentication to access the database server, because as it stands, any user can have access to it.

To achieve this, open the /etc/mongod.conf file and uncomment the following lines.

  authorization: enabled

To connect to the MongoDB database run the mongo command below.

$ mongo

logging in to MongoDB

To connect to the admin database, run:

use admin

Sample Output

Use Admin command MongoDB

To create a new user called mongoAdmin with the userAdminAnyDatabase role run:

    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

Sample Output

Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"

To exit from the Mongo shell run:


To login using the admin user account we just created, run:

$ mongo -u mongoAdmin -p --authenticationDatabase admin

Provide the password and later, you will drop to the Mongo shell.

To display users created in the system, run the command below to switch to the admin user.

use admin

Then run the following command.

show users

Sample Output

Show Users in MongoDB

Wonderful! We have successfully installed MongoDB on CentOS and created an Admin user for authentication.


  1. hamid says:

    i have problem to start this code systemctl start mngod
    it give error plz help

    1. Pankaj says:

      You have a typo, it’s “mongod” not “mngod”.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content