21

Mongo docs say that I can:

  1. use -SIGUSR1 signal and get the old log renamed and current switched
  2. 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?

6 Answers6

21

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 {
    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
}
mmx73
  • 311
16

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
Bill Ryder
  • 159
  • 1
  • 5
15

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
}
5

My solution for MongoDB v4.4 & Ubuntu 18.04

Create logrotate config /etc/logrotate.d/mongodb

/var/log/mongodb/mongod.log {
    rotate 7
    size 100M
    missingok
    create 0600 mongodb mongodb
    delaycompress
    compress
    sharedscripts
    postrotate
            /bin/kill -SIGUSR1 $(cat /var/run/mongodb/mongod.pid)
    endscript
}

Modify /etc/mongod.conf for reopening log file at kill signal and select pid file

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
  logRotate: reopen
processManagement:
  pidFilePath: /var/run/mongodb/mongod.pid

Modify /lib/systemd/system/mongod.service. It will create directory /var/run/mongodb at service start

[Service]
RuntimeDirectory=mongodb

Apply & check changes

sudo systemctl daemon-reload
sudo systemctl restart mongod
sudo systemctl status mongod

ls -lah /var/log/mongodb/ logrotate -f /etc/logrotate.d/mongodb ls -lah /var/log/mongodb/

Warn!

/lib/systemd/system/mongod.service can be overwritten after updating mongo with apt upgrade

gr8
  • 51
0

The following worked for me:

/var/log/mongo/mongod.log {
    missingok
    rotate 3
    size 100M
    nodateext
    postrotate
            /usr/bin/kill -USR1 $(cat /var/run/mongod.pid)
            rm -f /var/log/mongo/mongod.log.[0-9][0-9][0-9][0-9]-*
    endscript
}

Notes:

  • Tested on RedHat 6.5
  • The only way I could get a working solution was to delete the empty log files that Mongo produces
  • The location of the lock file depends on how MongoDB was installed
  • kill is a Bash builtin but logrotate runs under /bin/sh - which doesn't recognise SIGUSR1 on RedHat 6.5
  • I haven't tested with compress but it should be a straighforward addition
0

Note that in version 3.0 and higher it is not required to kill your database daemon simply to rotate a log. Refer to documentation here:

https://docs.mongodb.com/manual/tutorial/rotate-log-files/