Skip to content

Instantly share code, notes, and snippets.

@pballester
Forked from pagebrooks/MongoDB-LogRotate.md
Last active September 18, 2023 15:01
Show Gist options
  • Save pballester/1b1d0da497cde0031d4584ca7868451c to your computer and use it in GitHub Desktop.
Save pballester/1b1d0da497cde0031d4584ca7868451c to your computer and use it in GitHub Desktop.

Procedure for Mongodb Logrotate

The default install for Mongodb does not configure log rotation.

In /etc/logrotate.d/mongod

/var/log/mongodb/*.log {
    daily
    rotate 30
    compress
    dateext
    missingok
    notifempty
    sharedscripts
    postrotate
        /bin/kill -SIGUSR1 `cat /var/lib/mongodb/mongod.lock 2> /dev/null` 2> /dev/null || true
    endscript
}
  • Rotate logs daily
  • Delete log files older than 30 days
  • Compress the log files
  • Use YYYYMMDD for the archive extension
  • Don't fail if there is no log file
  • Don't archive empty log files
  • Only run postrotate script once, even if multiple files are present
  • Signal to Mongo that we are writing to a new log file. Apparently, mongo will continue to write to the old log file if you do not signal. The cat command is used to obtain the PID for the mongod process.

Resources

http://stackoverflow.com/questions/5004626/mongodb-log-file-growth http://docs.mongodb.org/manual/tutorial/rotate-log-files/

@ekmobile
Copy link

Doesn't the service need a restart? If I run the script with logrotate -f -v /etc/logrotate.d/mongod the service will fail and I need to restart it manually afterwards.

@ekmobile
Copy link

That's the only thing that is working for me:

/var/log/mongodb/*.log {
    daily
    rotate 30
    compress
    dateext
    missingok
    notifempty
    sharedscripts
    postrotate
        /bin/kill -SIGUSR1 `cat /var/lib/mongodb/mongod.lock 2> /dev/null` 2> /dev/null || true
	systemctl restart mongod
    endscript
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment