Introduction
This article describes how to configure MongoDB log rotation to avoid any high disk space consumption by mongo logs.
Description
According to official MongoDB documentation, the MongoDB log rotation is not automatic and must be triggered using one of this ways:
Mongo admin command trigger
- Launch a mongo shell on admin database
- Enter the following command
db.adminCommand( { logRotate : 1 } )
Mongo process trigger
- Send the SIGUSR1 signal to the mongod process
kill -SIGUSR1 `cat /var/lib/mongo/mongod.lock`
Implementation
The second option will be used as we would manage the MongoDB log rotation using embedded logrotate utility.
Start by creating following file:
vi /etc/logrotate.d/mongodb
Then adding this content
/var/log/mongodb/*.log {
daily
rotate 5
compress
dateext
missingok
notifempty
sharedscripts
copytruncate
postrotate
/bin/kill -SIGUSR1 `cat /var/lib/mongo/mongod.lock 2> /dev/null` 2> /dev/null || true
endscript
}
Note : The rotate value defines the number of rotations where the compressed logs will be kept on disk before deletion.
The log rotation mechanism can be checked with followed trigger
logrotate -f -v /etc/logrotate.d/mongodb
Automation
The attached ses8-mongodb-logrotate.sh is available to perform the above actions automatically.