How to Install MongoDB on CentOS 7

Reading Time: 3 minutes

MongoDB is a NoSQL database intended for storing large amounts of data in document-oriented storage with dynamic schemas. NoSQL refers to a database with a data model other than the tabular format used in relational databases such as MySQL, PostgreSQL, and Microsoft SQL. MongoDB features include: full index support, replication, high availability, and auto-sharding.

Preflight Check

  • A Liquid Web Core Managed CentOS 7 node.
  • We are logged in as the root user.

Step #1: Add the MongoDB Repository

First, we will use the vim text editor to create a new repo file for MongoDB. For a refresher on editing files with vim see: New User Tutorial: Overview of the Vim Text Editor

root@host:~# vim /etc/yum.repos.d/mongodb.repo

Next, add the following information to the file you’ve created, using i to insert:

[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

Then save and exit vim using the command :wq.

Step #2: Install MongoDB

As a matter of best practice we will update our packages.

root@host:~# yum -y update

At this point, installing MongoDB is as simple as running this command.

root@host:~# yum install -y mongodb-org

Step #3: Configure MongoDB

Prerequisites

Many Linux OSes confine the amount of system resources which a process can use. These limits sometimes can negatively effect the operation of MongoDB, and should be adjusted accordingly. The ulimit setting for CentOS 7 can be modified using the following command.

root@host:~# ulimit -n <value>

Additionally, CentOS 7 enforces a distinct max process limitation using, nproc, which overrides the ulimit settings. This nproc value is defined in this configuration file

/etc/security/limits.d/20-nproc.conf

To configure a nproc value, use vim to create a file called 99-mongodb-nproc.conf.

root@host:~# touch /etc/security/limits.d/99-mongodb-nproc.conf

Now, add the new soft nproc and hard nproc values to increase the process limit.

-f (file size): unlimited
-t (cpu time): unlimited
-v (virtual memory): unlimited [1]
-l (locked-in-memory size): unlimited
-n (open files): 64000
-m (memory size): unlimited [1] [2]
-u (processes/threads): 64000

Step #4: Start MongoDB

Note:
In MongoDB 4.4 and above, a startup error is seen if the ulimit value for number of open files is under 64000.

To run and manage the mongod process, we will use our OSes default init system. Newer versions of Linux typically use systemd (systemctl), and older versions use System V init (service). To determine which init system the platform uses, run the following command.

root@host:~# ps --no-headers -o comm 1

 Init Systems

Start Mongodb Using Systemd (Systemctl)Start Mongodb Using System V Init (service)
1. Start the mongod process.
root@host:~# systemctl start mongod
If you receive an error like:
Failed to start mongod.service: Unit mongod.service not found.
Run the following command.
root@host:~# systemctl daemon-reload
Then, run the start command above again.

2. Verify MongoDB has started.
To verify the mongod process has started successfully, issue the following command.
root@host:~# systemctl status mongod
To ensure MongoDB starts after a system reboot, issuing the following command.
root@host:~# systemctl enable mongod
1. Start Mongodb
Start mongod using the following command.
root@host:~# service mongod start

2. Verify MongoDB started successfully
To verify the mongod process started successfully, check the contents of the logfile in /var/log/mongodb/mongod.log for a line reading [initandlisten] waiting for connections on port port#
where is the port configured in /etc/mongod.conf, 27017 by default.
To ensure MongoDB starts after a system reboot, use the following command.
root@host:~# chkconfig mongod on

Step #5: Check MongoDB Status & Info

Check MongoDB Service Status

systemctl status mongod

Summary List of Status Statistics (Continuous)

mongostat

Summary List of Status Statistics (5 Rows, Summarized Every 2 Seconds)

mongostat --rowcount 5 2

Enter the MongoDB Command Line

mongo

By default, running this command will look for a MongoDB server listening on port 27017 on the localhost interface.

If you’d like to connect to a MongoDB server running on a different port, then use the –port option. For example, if you wanted to connect to a local MongoDB server listening on port 22222, then you’d issue the following command:

mongo --port 22222

Shutdown MongoDB

systemctl stop mongod

Conclusion

MongoDB is an excellent NoSQL solution for many types of projects. It is resilient, robust, and durable document database that is designed for ease of development and scaling. 

Our talented Support Teams are full of experienced technicians and administrators who have intimate knowledge of multiple web hosting technologies, especially those discussed in this article. We are always available to assist with any issues related to this article, 24 hours a day, 7 days a week 365 days a year.

If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server or a Dedicated server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via phone @800.580.4985, a chat or support ticket to assisting you with this process.

Author Bio

About the Author: David Singer

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Refer a friend and get hosting credit!