When running a MongoDB instance, it’s always important and even recommended to disable Transparent Huge Pages. Database workload can be poor when THP is enabled.
This service will allow you to disable THP during startup.
Create the service file we’ll use.
sudo touch /etc/systemd/system/mongodb-thp-never.service
Edit the file and insert the text below, your service name may be different, check /etc/systemd/system to be sure.
[Unit] Description="Disable THP before MongoDB boots" Before=mongod.service [Service] Type=oneshot ExecStart=/bin/bash -c 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' ExecStart=/bin/bash -c 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' [Install] WantedBy=mongod.service
Now that we have the service file created we can enable the service and start it.
Reload the daemon
sudo systemctl daemon-reload
Activate the file
sudo systemctl enable mongodb-thp-never.service
Start the service
sudo systemctl start mongodb-thp-never.service
Check your defrag file to see if the changes took effect.
That’s it! You will need to restart your MongoDB service for this to take effect.