How To Install MongoDB On CentOS 7

In this blog post , We will learn how to Install MongoDB on Centos 7.

Also explained how to manage MongoDB and configure remote access.

What Is MongoDB?

MongoDB is an object-oriented, simple, dynamic, and scalable NoSQL database.

It is based on the NoSQL document store model. The data objects are stored as separate documents inside a collection — instead of storing the data into the columns and rows of a traditional relational database.

Installing MongoDB On Centos 7

Before proceeding further , Make sure you have root or sudo privileges to perform mongoDB installation on the server.

Follow the below steps to install MongoDB community Edition using yum package manager.

First we need to configure yum for mongodb.

Create a file named mongodb.repo under /etc/yum.repos.d folder.

cd /etc/yum.repos.d/
vi mongodb.repo

And paste the below contents to the file.

name=MongoDB Repository

Save and close the file.

Now we can install mongodb from the repository we have added using yum command.

sudo yum install mongodb-org -y

While installing MongoDB using the above command , The following dependencies such as mongodb-org-mongos , mongodb-org-server , mongodb-org-shell , mongodb-org-tools will also be installed on the system.

To check the version of MongoDB installed on the system , use the below command.

mongo --version

The systemd service file will be created by default while installing MongoDB , Using which we can easily manage the mongodb service.

The mongoDB service file mongod.service is created under /usr/lib/systemd/system folder.

cat /usr/lib/systemd/system/mongod.service

Using the below command , Start the MongoDB service.

systemctl start mongod

To check the status of the mongoDB service,

systemctl status mongod

To automatically start the mongodb service on system reboot,

systemctl enable mongod

By default , MongoDB process listens on the port 27017.To check,

sudo netstat -nltp |grep mongo

To stop the mongoDB service,

sudo systemctl stop mongodb

To restart the mongoDB service,

sudo systemctl restart mongodb

To disable mongoDB service on system startup,

sudo systemctl disable mongodb

Configuring Remote Access

By default , MongoDB will accept connections only from localhost and listens on the port 27017.

To accept connections from the remote servers , We need to configure mongoDB and allow port 27017 port on the firewall.

To allow remote access from anywhere,

sudo ufw allow 27107

To allow the remote connections from anywhere or for the particular IP address , We need to allow the IP address in the /etc/mongod.conf configuration file.

Open the configuration file,

sudo vi /etc/mongod.conf

You can find a config named , bind_ip , Allow required IP address here as shown below.

Install MongoDB

The above config will allow connections from the localhost and the IP address we have allowed.Any incoming requests other than this will be rejected.

To allow connections from any IP address,

Install MongoDB

Save and Close the file.

We need to restart the mongoDB service for the changes to take effect.

sudo systemctl restart mongod

Now If you do netstat -nltp and check , The mongoDB will be listening on port 27017 (as usual) but will accept connections from anywhere.

sudo netstat -nltp |grep mongo

Connecting MongoDB

By default , MongoDB can be accessed without any authentication.

To launch the mongo shell , run the below command.


From the mongo shell, To list all the available database,

show dbs

To manage users , roles and database access such as granting or revoking database access to the users , We need to create a administrator (root user) in the admin database.

To connect to admin database,

use admin

Run the below command to create root user with admin privileges in the admin database.

db.createUser({user:"root", pwd:"sha((^%*&i(Qu", roles:[{role:"root", db:"admin"}]})

We have successfully created a root user who can manage other users and databases.

But To be able to login as admin user with authentication , We need to enable authentication on the mongoDB service which is not activated by default.

We know that the mongoDB service post installation will create systemd service file , but without –auth parameter.

Lets go ahead and configure –auth parameter on the /lib/systemd/system/mongod.service file.

sudo vi /lib/systemd/system/mongod.service

Under the [Service] section , You can find a parameter ExecStart

We need to change this line


ExecStart=/usr/bin/mongod $OPTIONS


ExecStart=/usr/bin/mongod $OPTIONS --auth

Once added , Save and Close the file.

We need to run the following commands for the changes to take effect.

systemctl daemon-reload
systemctl restart mongod

If you check the status , You can see that the –auth is enabled.

systemctl status mongod

Lets connect to mongoDB admin database as the root user

sudo mongo -uroot -p --authenticationDatabase "admin"

You will prompted for the password , Enter the password and hit Enter.

You should be successfully logged in as root user with authentication.

We have learnt to install mongoDB on Centos 7.

Also we know how to allow or restrict remote access for the mongoDB and enabled authentication.

Hope you find it helpful.

Please do check out my other articles.