Kernel Parameter Tuning for MongoDB
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.
Edit a new file named tuned.conf in the directory we just created with the following content:
summary=Kernel specific configurations for running mongodb nodes.
Create /etc/tuned/mongodb/set_kernel_params.sh with the following contents:
echo never > /sys/kernel/mm/transparent_hugepage/defrag
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