This post was originally written in September 2018 and was updated in March 2025.
MongoDB logs are essential for tracking operations and troubleshooting, but left unmanaged, they can quickly consume valuable disk space and even impact database performance. Proactively managing log file size through automated rotation is crucial for maintaining a healthy system. This blog post will guide you through the best—and simplest—methods for automating MongoDB log rotation, ensuring your logs remain useful and manageable without constant manual intervention.
In MongoDB, the log is not rotated automatically, so we need to rotate it manually. Usually, the log size of the MongoDB server depends on the level of information configured and the slow log configured. By default, commands taking more than 100ms, or whatever the value set for the slowOpThresholdMs parameter, are written into the MongoDB log file. Let’s see how to automate log rotation on Linux-based servers.
The following two methods could be used to rotate the log in MongoDB. The first uses the command shown below from within mongo shell:
|
1 |
> use admin<br>> db.adminCommand( { logRotate : 1 } ) |
The alternative is to use SIGUSR1 signal to rotate the logs for a single process in Linux/Unix-based systems:
|
1 |
# kill -SIGUSR1 $(pidof mongod) |
The behavior of log rotation in MongoDB differs according to the value of the parameter logRotate which was introduced in version 3.0 (note that this should not be confused with the logRotate command that we’ve seen above). The two values are:
In versions 2.6 and earlier, the default behavior when issuing the logRotate command is the same as when using rename, i.e. it renames the original log file from mongod.log to mongod.log.xxxx-xx-xxTxx-xx-xx format where x is filled with the rotation date time, and creates a new log file mongod.log to continue to write logs. You can see an example of this below:
|
1 |
mongodb-osx-x86_64-3.2.19/bin/mongod --fork <br> --dbpath=/usr/local/opt/mongo/data6 <br> --logpath=/usr/local/opt/mongo/data6/mongod.log <br> --logappend --logRotate rename<br>about to fork child process, waiting until server is ready for connections.<br>forked process: 59324<br>child process started successfully, parent exiting<br><br>ls -ltrh data6/mongod.log*<br>-rw-r--r-- 1 vinodhkrish admin 4.9K Sep 14 16:57 mongod.log<br> <br>mongo <br>MongoDB shell version: 3.2.19<br>connecting to: test<br>> <br>> db.adminCommand( { logRotate : 1 } )<br>{ "ok" : 1 }<br>> exit<br>bye<br><br>ls -ltrh data6/mongod.log*<br>-rw-r--r-- 1 vinodhkrish admin 4.9K Sep 14 16:57 mongod.log.2018-09-14T11-29-54<br>-rw-r--r-- 1 vinodhkrish admin 1.0K Sep 14 16:59 mongod.log |
Using the above method, we need to compress and move the rotated log file manually. You can of course write a script to automate this. But when using the logrotate=reopen option, the mongod.log is just closed and opened again. In this case, you need to use the command alongside with Linux’s logrotate utility to avoid the loss of log writing in the course of the log rotation operation. We will see more about this in the next section.
I wasn’t a fan of this second method for long time! But MongoDB log rotation seems to work well when using Linux/Unix’s logrotate tool. Now I prefer this approach, since it doesn’t need the complex script writing that’s needed for the first log rotation method described above. Let’s see in detail how to configure log rotation with Linux/Unix’s logrotate utility.
Start MongoDB with the following options:
|
1 |
systemLog:<br> destination: file<br> logAppend: true<br> path: /var/log/mongodb/mongod.log<br> logRotate: reopen<br>processManagement: <br> pidFilePath: /var/run/mongodb/mongod.pid |
As mentioned in the section about Linux’s logrotation utility, you need to create a separate config file /etc/logrotate.d/mongod for MongoDB’s log file rotation. Add the content shown below into that config file:
|
1 |
/var/log/mongodb/mongod.log {<br> daily<br> size 100M<br> rotate 10<br> missingok<br> compress<br> delaycompress<br> notifempty<br> create 640 mongod mongod<br> sharedscripts<br> postrotate<br> /bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod.pid 2>/dev/null` >/dev/null 2>&1<br> endscript<br>} |
In this config file, we assume that log path is set as /var/log/mongodb/mongod.log in /etc/mongod.conf file, and instruct Linux’s logrotation utility to do the following:
Please test the logrotate manually using the created /etc/logrotate.d/mongod file to make sure it is working as expected. Here’s how:
|
1 |
cd /var/log/mongodb/<br>ls -ltrh<br>total 4.0K<br>-rw-r-----. 1 mongod mongod 1.3K Sep 14 12:45 mongod.log<br><br>logrotate -f /etc/logrotate.d/mongod <br>ls -ltrh<br>total 8.0K<br>-rw-r-----. 1 mongod mongod 1.4K Sep 14 12:58 mongod.log.1<br>-rw-r-----. 1 mongod mongod 1.3K Sep 14 12:58 mongod.log<br><br>logrotate -f /etc/logrotate.d/mongod <br>ls -ltrh<br>total 12K<br>-rw-r-----. 1 mongod mongod 491 Sep 14 12:58 mongod.log.2.gz<br>-rw-r-----. 1 mongod mongod 1.4K Sep 14 12:58 mongod.log.1<br>-rw-r-----. 1 mongod mongod 1.3K Sep 14 12:58 mongod.log |
Since the introduction of logRotate parameter in MongoDB 3.0, the log rotate script needs an extra step when you are using logRotate=rename or when using <=2.x versions.
Start the MongoDB with the following options (for 2.4 and earlier, ) :
|
1 |
logAppend=true<br>logpath=/var/log/mongodb/mongod.log<br>pidfilePath=/var/run/mongodb/mongod.pid |
Start MongoDB with the following options YAML format (YAML config introduced from version 2.6) :
|
1 |
systemLog:<br> destination: file<br> logAppend: true<br> path: /var/log/mongodb/mongod.log<br>processManagement:<br> pidFilePath: /var/run/mongodb/mongod.pid |
The config file /etc/logrotate.d/mongod for MongoDB’s log file rotation should be set up like this:
|
1 |
/var/log/mongodb/mongod.log {<br> daily<br> size 100M<br> rotate 10<br> missingok<br> compress<br> delaycompress<br> notifempty<br> create 640 mongod mongod<br> sharedscripts<br> postrotate<br> /bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod.pid 2>/dev/null` >/dev/null 2>&1<br> find /var/log/mongodb -type f -size 0 -regextype posix-awk <br>-regex "^/var/log/mongodb/mongod.log.[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}-[0-9]{2}-[0-9]{2}$" <br>-execdir rm {} ; >/dev/null 2>&1<br> endscript<br>} |
In this case, the logrotate utility behaves as follows:
Q1: Why is MongoDB log rotation necessary?
A: MongoDB log rotation is necessary to prevent log files from growing indefinitely, which can consume excessive disk space, make log analysis difficult, and potentially impact database performance as it writes to very large files. Regularly rotating logs keeps file sizes manageable.
Q2: What are the main ways to rotate MongoDB logs?
A: There are two primary methods: 1) Using MongoDB’s internal logRotate administrative command (triggered via mongo shell or SIGUSR1 signal), whose behavior depends on the systemLog.logRotate parameter (rename or reopen). 2) Using an external system utility like the Linux logrotate tool, which typically works best with MongoDB’s systemLog.logRotate set to reopen.
Q3: What does the systemLog.logRotate: reopen option do and why use it with the logrotate utility?
A: When systemLog.logRotate is set to reopen (and logAppend is true), signaling mongod (usually with kill -SIGUSR1) causes it to close and reopen its log file handle. This is designed to work seamlessly with the Linux logrotate utility, which renames the current log file (mongod.log becomes mongod.log.1) and creates a new empty mongod.log. The SIGUSR1 signal in the postrotate script tells mongod to start writing to this newly created empty file.
Resources
RELATED POSTS