Mongodb - proper way to rotate logs
Solution 1
copytruncate works pretty well for logrotation.
a config similar to this should do the job for you:
/var/log/mongodb/*.log {
daily
missingok
rotate 5
compress
dateext
delaycompress
copytruncate
notifempty
}
Solution 2
Since mongodb 3.0 you can change the behavior of mongodb with the logRotate parameter, change in /etc/mongod.conf
systemLog:
logAppend: true
logRotate: reopen
See also Mongo Manuals.
Then you can use this logrotate configuration:
/var/log/mongodb/*.log {
daily
rotate 30
size 50M
compress
dateext
missingok
notifempty
sharedscripts
postrotate
/bin/kill -SIGUSR1 `cat /var/lib/mongodb/mongod.lock 2> /dev/null` 2> /dev/null || true
endscript
}
Solution 3
The server crashed for me if you send SIGUSR1 to mongod after you have moved the logfile out of the way with logrotate.
The following config is safe for the version I've tested - 2.6.6 on ubuntu 12.04 - the previous examples crashed the server. Put this into /etc/logrotate.d/mongod:
/var/log/mongodb/mongodb.log {
weekly
missingok
rotate 4
compress
notifempty
create
postrotate
/usr/bin/pkill -USR1 mongod
rm /var/log/mongodb/mongodb.log.????-??-??T??-??-??
endscript
}
See: https://jira.mongodb.org/browse/SERVER-11087 for more details and a suggestion from Akshay Kumar which I used in the above (use create instead of nocreate and cp /dev/null to the logfile).
In later releases there is supposed to be a logRotate option you can use to reopen the file - not rename it - which will work around the rename problem - but it didn't work in my version (it was unsupported).
See: https://github.com/mongodb/mongo/commit/616461d294bd9f5054ca38b302b6fc5d70fde20c
I've tested this with
logrotate -v -f /etc/logrotate.d/mongod
Related videos on Youtube
Andrey Regentov
Updated on September 18, 2022Comments
-
Andrey Regentov over 1 year
Mongo docs say that I can:
- use -SIGUSR1 signal and get the old log renamed and current switched
- use logrotate from OS
I want the OS's logrotate ability to zip old files and remove oldest, but see no way to tell mongod process to switch current log other than sending SIGUSR1.
So I wrote
/var/log/mongodb/*.log { daily rotate 5 compress dateext missingok notifempty sharedscripts postrotate /usr/bin/killall -SIGUSR1 mongod /usr/bin/killall -SIGUSR1 mongos endscript }
to /etc/logrotate.d/mongo.
And now get well-named logfiles from logrotate and empty logfiles like
mongodb.log.2013-09-18T23-49-44
as traces of SIGUSR1 switching. How to get rid of the latter? -
Thomas Bratt over 9 yearsThis didn't work for me on RedHat 6.5. Logs were rotated but the original .log file continued to grow without bound.
-
Mxx over 9 years@ThomasBratt this is correct because without restarting mongo processes file handler remain open. fwics this method will not work all that well.
-
Mxx over 9 years@ThomasBratt take a look at this answer stackoverflow.com/a/8396266/949859
-
Thomas Bratt over 9 years@Mxx Nice find - looks like copytruncate will work with the a postrotate step to signal to Mongo to truncate the logfile
-
Tim over 8 yearsWorks great for me in Mongo 2.6.11 on CentOS 7
-
Law29 almost 8 yearsIt seems Andrey already read that documentation; you're not really answering his question.
-
cwhisperer over 7 yearsI had to replace mongodb.log with mongod.log and it works
-
Mike about 7 yearsCan confirm that if
systemLog.logRotate: reopen
inmongod.conf
, then the pkill will work as intended and no deletion of the renamed log file is required as none is created. -
Gert van den Berg about 5 yearsA PID file created from the config file should probably be used.... See
processManagement.pidFilePath
in the config or the SystemD unit file's PIDFile setting (/var/run/mongodb/mongod.pid
for me) -
JeffRSon over 3 yearsBUT - keep in mind that nowaday /run is tmpfs, so it is cleared on reboot and the mongodb folder does not exist unless it is created vi tmpfs conf!