RHEL7: How to install a MongoDB service.

Share this link

Presentation of MongoDB

MongoDB Server is an open-source, document database designed for ease of development and scaling.


As the default MongoDB installation uses the /var/lib/mongo directory to store your databases, keep in mind that the partition or logical volume associated with /var needs adequate space.

Installation Procedure

To install, apply the following steps:

Create a file called /etc/yum.repos.d/mongodb-org-3.2.repo and paste the lines below into:

name=MongoDB Repository

Then, install the MongoDB packages:

# yum install -y mongodb-org

Note: To prevent unintended upgrades, the official MongoDB documentation advises to pin the various packages by adding the following directive in the /etc/yum.conf file:


Configuration Procedure

Limit Change

The number of processes available for MongoDB in the system is not enough, you need to change the default settings by editing the /etc/security/limits.d/20-nproc.conf file and paste the following lines at the end (source: MongoDB website):

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

Transparent Hugepages Deactivation

To get better performances, it is also recommended to disable transparent hugepages by creating a tuned profile (it is perfectly possible to run MongoDB without the tuned daemon but, in this case, to get good performances will require much more efforts from you) (source: MongoDB website) (if you use an other method, be careful about this bug).

Create the /etc/tuned/no-thp directory:

# mkdir /etc/tuned/no-thp

Create the /etc/tuned/no-thp/tuned.conf file and paste the following lines into:



Note: virtual-guest is supposed to be your current active profile, change it if needed.

Enable the new profile:

# tuned-adm profile no-thp

Check the configuration:

# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]

Start and enable at boot time the MongoDB service:

# systemctl start mongod && systemctl enable mongod

Check that MongoDB is correctly working:

# tail /var/log/mongodb/mongod.log | grep "waiting for connections"
2016-12-06T23:02:59.853+0100 I NETWORK  [initandlisten] waiting for connections on port 27017

Access From Outside

Note: By default, the MongoDB service only listens to the local network interface ( To be able to interact with any other server, comment the BindIP directive in the /etc/mongod.conf configuration file and update the Firewalld configuration with:

# firewall-cmd --permanent --add-port=27017/tcp
# firewall-cmd --reload

Authentication Mode

Then, you can create the root user (you could call it differently) for the mydb database:

# mongo
MongoDB shell version: 3.2.11
connecting to:
> db.createUser({ user: "root", pwd: "myPassword", roles: [{role: "root", db: "admin"}] })
Successfully added user: {
        "user" : "root",
        "roles" : [
                        "role" : "root",
                        "db" : "admin"
> exit

To enable the authentication mode, paste the following lines into the /etc/mongod.conf file:

  authorization: enabled

Restart the MongoDB service:

# systemctl restart mongod

Test the new authentication mechanism:

# mongo mydb --username root --password myPassword --authenticationDatabase mydb
MongoDB shell version: 3.2.11
connecting to: mydb
> exit

Source: MongoDB website.

Advanced Optimizations

Besides the standard configuration previously described, they are additional steps to follow to get the best performances.

Kernel Configuration

Edit the /etc/tuned/no-thp/tuned.conf file and paste the following lines:

vm.swappiness = 1             # almost no swap to disk (0=never swap, 100=always swap)
zone_reclaim_mode = 0         # no memory reclaimed (normally the default value)

Note: According to Percona, assigning 0 to the wm.swappiness kernel parameter doesn’t bring the expected behavior.

In case you are dealing with large-memory (64GB+) database servers, you can also add these recommended settings:

vm.dirty_ratio = 15           # percentage of total system memory that can hold dirty pages
vm.dirty_background_ratio = 5 # threshold to start flushing dirty pages to disk in the background

Concerning the network side, Percona (see links below) advises to add the following configuration:

net.core.somaxconn = 4096
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_max_syn_backlog = 4096

IO Scheduler and Read-Ahead Configuration

The IO Scheduler defines how disk Input/Output are managed (see tutorial).

The deadline IO scheduler should be chosen in all the cases except when dealing with virtual machines where the noop scheduler is the best: it is useless to ask a virtual machine to order its IO when all the work is done at the hypervisor level.

The Read-Ahead setting defines how much data is stored in the cache for each read. This is a way to increase efficiency in case of sequential reads.

As MongoDB reads are not sequential, this ends up wasting cache space.

Percona (see links below) provides a nice way to specify both configurations at the same time.

In the case of a physical server, create the /etc/udev/rules.d/60-sda.rules file and paste the following lines:

# set deadline scheduler and 16kb read-ahead for /dev/sda
ACTION=="add|change", KERNEL=="sda", ATTR{queue/scheduler}="deadline", ATTR{bdi/read_ahead_kb}="16"

Note: You can test the IO scheduler in place with # cat /sys/block/sda/queue/scheduler and the Read-Ahead parameter with # blockdev –getra /dev/sda

Filesystem Configuration

XFS is the best available option when choosing a filesystem, EXT4 being a second-best option.

Also, to avoid access-time metadata updates by default, add the flag noatime to the filesystem mount options in the /etc/fstab file. After a reboot or umount/mount operations with MongoDB stopped, this change will reduce the amount of useless disk IO activity.

Additional Resources

Several websites provide tutorials about installing MongoDB 3.2 on CentOS 7:

The Percona blog provides several very interesting posts about MongoDB performances:

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

RHCSA7: Task of the day

Allowed time: 5 minutes.
Create two users "tom" and "engine". "tom" has the UID/GID 3000 and "engine" the UID/GID 4000. "engine" doesn't have an interactive shell.

RHCE7: Task of the day

Allowed time: 3 minutes.
Configure your machine to be a router.

Poll for favorite RHEL 7 book

What is your favorite RHEL 7 book to prepare RHCSA & RHCE exams?

View Results

Loading ... Loading ...

Poll for most difficult RHCSA 7 topic

What do you think is the most difficult RHCSA 7 topic?

View Results

Loading ... Loading ...

Poll for most difficult RHCE 7 topic

What do you think is the most difficult RHCE 7 topic?

View Results

Loading ... Loading ...