Kernel Parameter Tuning for MongoDB

  • skeeved's picture
  • Posted on: 18 September 2017
  • By: skeeved

MongoDB, like most database systems, prefers to handle memory allocation itself rather than relying on the underlying kernel's memory handling processes. To run MongoDB efficiently, the kernel parameter transparent_hugepages and transparent_hugepages/defrag should be set to 'never'.

To complicate matters slightly, since CentOS 6, a service named tuned runs by default and sets various kernel parameters periodically. To keep our custom configurations, we'll create a new tuned profile and have it override the OS supplied defaults.

Setting tuned Parameters

Create a new directory to store our new tuned profile.

mkdir /etc/tuned/mongodb

Edit a new file named tuned.conf in the directory we just created with the following content:

[main]
include=virtual-guest
summary=Kernel specific configurations for running mongodb nodes.
[disk]
readahead=>32
[vm]
transparent_hugepages=never
[script]
script=set_kernel_params.sh

Create /etc/tuned/mongodb/set_kernel_params.sh with the following contents:

#!/bin/bash
. /usr/lib/tuned/functions
start() {
echo never > /sys/kernel/mm/transparent_hugepage/defrag
return 0
}
stop() {
return 0
}
process $@

Set mode 0700 on /etc/tuned/mongodb/set_kernel_params.sh.

chmod 0700 /etc/tuned/mongodb/set_kernel_params.sh

Enable the new tuned profile.

tuned-adm profile mongodb

Setting ulimit Maximum File Descriptors
We're also going to adjust the ulimit maximum number of file descriptors for the mongod user.

Create /etc/security/limits.d/99-mongod.conf with the following contents:

# Set soft proc limit to 1/2 of max file descriptor
mongod soft nproc 32000