MongoDB is a free and open-source document database. It is classified as a NoSQL database which is different than traditional table-based SQL databases like MySQL and PostgreSQL.

In this tutorial, we will guide you how to install and configure MongoDB Community Edition on a CentOS 7 server.


Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges .

Installing MongoDB

Follow the steps below to install the latest stable version of MongoDB on your CentOS server :

  1. Enabling MongoDB repository

    To add the MongoDB repository to your system, open your text editor and create a new YUM repository configuration file named mongodb-org.repo inside the /etc/yum.repos.d/ directory:

    nano /etc/yum.repos.d/mongodb-org.repo
    name=MongoDB Repository


  2. Installing MongoDB

    Now that the repository is enabled you can install the mongodb-org meta-package using the yum utility:

    sudo yum install mongodb-org

    During the installation yum will prompt you to import the MongoDB GPG key. Type y and hit Enter.

  3. Starting MongoDB

    Once the installation is completed, start the MongoDB daemon and enable it to start on boot by typing:

    sudo systemctl start mongodsudo systemctl enable mongod
  4. Verifying MongoDB Installation

    To verify the installation we will connect to the MongoDB database server using the mongo tool and print the server version:


    Once you are inside the MongoDB shell type the following command which will display the MongoDB version:


    The output will look like the following:


Configuring MongoDB

You can configure your MongoDB instance by editing the /etc/mongod.conf configuration file which is written in YAML .

  authorization: enabled

If this option is disabled each user will have access to any database and will be able to execute any action.

After making changes to the MongoDB configuration file, restart the mongod service:

sudo systemctl restart mongod

To find more information about the configuration options available in MongoDB 4.0 visit the Configuration File Options documentation page.

Creating Administrative MongoDB User

If you enabled the MongoDB authentication, create one administrative MongoDB user that you will use to access and manage your MongoDB instance.

First access the mongo shell with:


Once you are inside the MongoDB shell type the following command to connect to the admin database:

use admin

switched to db admin

Create a new user named mongoAdmin with the userAdminAnyDatabase role:

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

Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
You can name the administrative MongoDB user as you want.

Exit the mongo shell with:


To test the changes, access the mongo shell using the administrative user you have previously created:

mongo -u mongoAdmin -p --authenticationDatabase admin
use admin

switched to db admin

Now, print the users with:

show users

	"_id" : "admin.mongoAdmin",
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
	"mechanisms" : [

You can also try to access the mongo shell without any arguments ( just type mongo) and see if you can list the users using the same commands as above.

You have learned how to install and configure MongoDB 4.0 on your CentOS 7 server