Log rotation

Log rotation in DMK

When creating a MongoDB instance using the DMK (dmk_dbcreate.py), log rotation should be setup to ensure logs are managed effectively.

DMK Log Directory Structure

As part of the instance creation process, DMK generates the log directory:

/u04/mongodb/logs/<instance_name>

A symbolic link is also created from:

/u01/app/mongodb/admin/<instance_name>/logs -> /u04/mongodb/logs/<instance_name>

This keeps logs in a centralized volume and separates them from binary, data, and backup paths.

Log Configuration in the MongoDB Template

The log rotation behavior is controlled via the MongoDB configuration file, which is generated from the template provided in the DMK YAML:

systemLog:
  destination: file
  path: ${log_path}/${instance_name}.log
  logAppend: true

To support external rotation using tools like logrotate, the following line should be present:

  logRotate: reopen

This instructs MongoDB to reopen its log file when receiving a SIGUSR1 signal, making external rotation seamless.

Starting with release 2.3.0 of the DMK, this is the default configuration when creating a MongoDB instance with DMK.

System Log Rotation with logrotate

DMK recommends a logrotate configuration, typically found in:

/etc/logrotate.d/mongodb

The template logrotate configuration look like this :

Variable
Path
Purpose

$MONGO_BASE

/u01/app/mongodb

Binary files, instances configuration.

$MONGO_DATA_ROOT

/u02/mongodb/data/

MongoDB data files.

$MONGO_JOURNAL_ROOT

/u03/mongodb/journal/

MongoDB journal files. If you don't want to separate MongoDB journal and data files, set MONGO_JOURNAL_ROOT to the same value as MONGO_DATA_ROOT.

$MONGO_LOG_ROOT

/u04/mongodb/log/

MongoDB instances log files. Default changed in release 2.3.0 from /u02/mongodblog to /u04/mongodb/log, to separate data and log files.

$MONGO_BACKUP_ROOT

/u90/mongodb/backup

MongoDB backups location.

This configuration ensures:

  • Daily rotation

  • 7 archived logs kept

  • Compression of older logs

  • No error if the log file is missing

  • No rotation of empty files

Manual Log Rotation

You can also trigger log rotation manually, from the shell:

kill -SIGUSR1 <mongod_pid>

Further Reading

Last updated