MongoDB has log rotation functionality built in, but since I run CentOS I like to have everything managed through logrotate
. The 10gen RPM I installed doesn’t have a logrotate script, so I wrote one. Create file /etc/logrotate.d/mongod
:
/var/log/mongo/mongod.log { daily rotate 30 compress dateext missingok notifempty sharedscripts postrotate /bin/kill -SIGUSR1 `cat /var/lib/mongo/mongod.lock 2> /dev/null` 2> /dev/null || true endscript }
logrotate
runs at 4 AM daily by default (via the script in /etc/cron.daily/logrotate
). The file above rotates the mongod.log file daily, retaining 30 days of files, appending the date to each one after rotation (rather than the “.1” or “.2” suffix) and then gzipping it.