There is a new version of this tutorial available for CentOS 8.

How to Install and Configure MongoDB on CentOS 7

MongoDB is a NoSQL database that provides high performance, high availability, and automatic scaling. NoSQL database means that, unlike MySQL or PostgreSQL, it does not support SQL (Structured Query Language) to retrieve or manipulate the stored data. MongoDB does not store data in tables, instead, it stores data in a "document" structure similar to JSON (in MongoDB called BSON). MongoDB was first introduced in 2009, six years ago. Currently developed by the company MongoDB MongoDB Inc.

In this tutorial, I will guide you to install and configure MongoDB 3.2 (stable) on a CentOS 7 server. We will add and configure the administrator user for MongoDB and configure the authentication for MongoDB services.


  • CentOS 7
  • Root privileges

What we will do in this tutorial :

  1. Add the MongoDB repository.
  2. Installing MongoDB.
  3. Fix some MongoDB errors.
  4. Create an administrator user.
  5. Enable MongoDB authentication and Testing.

Step 1 - Add the MongoDB Repository in CentOS

Connect to your CentOS 7 server with the ssh root account:

ssh [email protected]

We need to to go to the 'yum.repos.d' directory in order to add the new CentOS repository. Go to that directory and create new repository file 'mongodb-org-3.2.repo' with vim:

cd /etc/yum.repos.d/
vim mongodb-org-3.2.repo

Paste MongoDB repository configuration that is shown below:

name=MongoDB Repository

Save the file and exit the editor.

Step 2 - Installing MongoDB

We've added MongoDB repository, now check the repository list on the server to make sure that the MongoDB repository is available in the list.

Run the command below as root to do so:

yum repolist

You sould see results below. The MongoDB repository is on the list.

The MongoDB repository is installed on CentOS 7

Next, install MongoDB with the yum command.

yum -y install mongodb-org

When the installation is finished, start MongoDB with this systemctl command:

systemctl start mongod

Check that MongoDB is running by checking that the port '27017' is open.

netstat -plntu

And make sure the mongodb service is active.

systemctl status mongod

Start MongoDB on CentOS 7

Step 3 - Fix a MongoDB Error

MongoDB is installed. Now we can access the mongodb shell by using the command below:


You will probably see this error about ulimit configuration on the server.

** WARNING: soft rlimits too low. rlimits set to 4096 processes, 64000 files. Number of processes should be at least 32000...

MongoDB Ulimit Error

Ulimits or User limits define how much of a system-wide resource a user may use.

In order to resolve this problem, we need to increase the ulimit configuration of the user mongod.

On CentOS 7 server, the MongoDB database is running under the user 'mongod'. Go to the 'security' directory and edit the 'limits.conf' configuration file.

cd /etc/security/
vim limits.conf

I will increase the limits of the mongod user to '64000' - number of processes or nproc and the number of open files or nofile to 64000.

Paste new configuration below to the end of the file:

mongod soft nproc 64000
mongod hard nproc 64000
mongod soft nofile 64000
mongod hard nofile 64000

Save the limits.conf file.

Run the sysctl command below to apply the changed limits to the system:

sysctl -p

Then restart the MongoDB service and try again to access the mongo shell, the error is gone now.

systemctl restart mongod

Set Ulimits for MongoDB

Step 4 - Create a MongoDB Administrator User

In this step, we will create a new user "admin" for MongoDB with the role 'UserAdminAnyDatabase' from the mongo shell.

Open the mongodb shell:


I will create new user administrator named 'admin' with password 'admin123'. Please use a secure password on your server. Then we will configure the role of the user to be 'UserAdminAnyDatabase'.

Switch to the database 'admin'.

use admin

Type in the MongoDB query below to create the new administrator user:

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

Next, ensure that the user has been created with the MongoDB query below.

show users

Create MongoDB user

The MongoDB administrator user has been created.

Step 5 - Enable User Authentication in MongoDB

In this step, we will enable authentication for users to prevent that another user without sufficient privileges is able to see the data on the database.

On our CentOS 7 server, MongoDB is running under systemd with an init script in the '/etc/init.d/' dirctory. We will edit that script to force the mongodb service to run with the '--auth' option.

Go to the '/etc/init.d/' directory and edit the "mongod" file:

cd /etc/init.d/
vim mongod

On line 15 you will find the "OPTION" variable, there we will add the "mongod" option.


Save the file.

Reload the systemd service and restart MongoDB.

systemctl daemon-reload
systemctl restart mongod

Next, we have to test the configuration by logging into the mongo shell and switch to the admin database, then try to see the admin users.


use admin
show users

You will see an error about the unauthorized execution of the command in the database admin. Now we need to use the command 'db.auth()' for the authentication.

db.auth('admin', 'admin123')

Now you can see the users with their roles and the privileges.

Configure MongoDB authentication

MongoDB 3.2 has been installed and configured on a CentOS 7 Server.

Share this page:

9 Comment(s)

Add comment

Please register in our forum first to comment.


By: Tom

Thanks, whoever you are!  Everything worked exactly as indicated on this web page.  This is probably the first time I have run through CentOS 7 product install instructions where every single command worked exactly as shown, with no trouble at all.

By: aorosar

Changing the limit values in limits.conf didn't work for me, I had to change it in the init script:

I had to add this line and then restart the service:



By: Longuè

very nice thanks !! It's working as expected

By: suresh kumar

Superb article. thanks a lot

By: suyash

A document should have commands that can give more exact output. Like

netstat -plntu could have changed by netstat -plntu | grep 27017 for better output.

By: sanjeev patro

Thanks for sharing this valueble information. I want you to add one more step after before dbcreateusers

1> service stop mongodb

2> mongod --config /etc/mongod.conf# mongod --config /etc/mongod.confabout to fork child process, waiting until server is ready for connections.forked process: 31134child process started successfully, parent exiting


3> then execute mongo

#mongoMongoDB shell version: 3.2.22connecting to: testServer has startup warnings:2019-11-27T04:23:52.747+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2019-11-27T04:23:52.747+0000 I CONTROL  [initandlisten]2019-11-27T04:23:52.748+0000 I CONTROL  [initandlisten]2019-11-27T04:23:52.748+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.2019-11-27T04:23:52.748+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'2019-11-27T04:23:52.748+0000 I CONTROL  [initandlisten]2019-11-27T04:23:52.748+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2019-11-27T04:23:52.748+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'2019-11-27T04:23:52.748+0000 I CONTROL  [initandlisten]> db.createUser(...   {...     user: "admin",...     pwd: "admin123",...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]...   }... )Successfully added user: {        "user" : "admin",        "roles" : [                {                        "role" : "userAdminAnyDatabase",                        "db" : "admin"                }        ]}> show users{        "_id" : "test.admin",        "user" : "admin",        "db" : "test",        "roles" : [                {                        "role" : "userAdminAnyDatabase",                        "db" : "admin"                }        ]}

The reason why i recommend this tep is if we try to execute "db.createUser" immdiately after installing mongodb and starting the service I faced this issue

2019-11-27T02:05:09.927+0000 E QUERY    [thread1] Error: couldn't add user: not authorized on admin to execute command { createUser: "admin", pwd: "xxx", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ], digestPassword: false, writeConcern: { w: "majority", wtimeout: 60000.0 } } :[email protected]/mongo/shell/utils.js:25:[email protected]/mongo/shell/db.js:1267:[email protected](shell):1:1


By: kunal

i have installed mongodb on server,it was running successfully,but form last few days after typing mongo on server,not able to connect and error is

connecting to: mongodb://

2020-03-11T10:10:41.529+0330 E  QUERY    [js] Error: couldn't connect to server, connection attempt failed: SocketException: Error connecting to :: caused by :: Connection refused :

[email protected]/mongo/shell/mongo.js:341:17

By: igpf

yeah... force once a how-to that works line for line.  thank you. very nice.

By: Oguzhan

Hi man,

Thanks for great tutorial!