MongoDB Log Rotation Automation

When you are using mongodb for large applications (heavy write and read intensive), then you probable noticed that mongod.log/mongos.log file is growing big in size with in a few time span. I saw 20GB mongos.log file size in a single day. So, your logfile should be in a limited size for diagnostics and debugging purpose. Other wise if some error happens and you want to take quick analysis of those logfile, then probably it will be very cumbersome job. To serve this purpose we need log file should be rotate with reference of either by time or by size.

Below are the few points you can do as per the mongodb documentation in your mongod.conf file.

  destination: file
  path: /var/mongodb/log/mongod.log
  logAppend: true
  logRotate: reopen

You have two choices logRotate: reopen or rename, but both the case you need to run below mention code to rotate the log.

db.adminCommand( { logRotate : 1 } )

Either you need to execute this code manually or you need to write a corn schedule job to do this periodically. Now I am showing you how do you automate mongodb log file rotation in your linux VM without writing any shell script and corn job scheduler.

MongoDB Automation Logrotate

For this log rotation automation, I used logrotate mechanism which are available in most of the linux distribution.

Mongdb configuration

In mongodb configuration file ( mongod.conf or mongos.conf ) at the systemLog section I provided below configuration. My log file path is /var/mongodb/log/mongod.log . After below changes, do not forget to restart the mongod or mongos process once again.

  destination: file
  path: /var/mongodb/log/mongod.log
  logAppend: true
  logRotate: reopen

Now create this file below, here mongodb is linux user name.

sudo nano /etc/logrotate.d/mongodb

In this file make this changes

   rotate 10
   dateformat %Y-%m-%d-%s
   size 10000M
   create 600 mongodb mongodb
     /bin/kill -SIGUSR1 $(cat /var/run/

pid file path for MongoDB

You will find this file to your data directory, in my case /data/db


Adding a simple file link will do the trick for me.

ln -s /data/db/mongod.lock /var/run/

Now, this is linked to $(cat /var/run/ of the logrotate setup file /etc/logrotate.d/mongodb which you are currently editing.

Explain /etc/ logrotate.d/mongodb Parameter

rotate 10I am keeping 10 log files
dailyrotate files at least once a day
size 10000Mrotate file when its size reaches 10GB
missingokdon’t show error, if there is no log file
create 0600 mongodb mongodbautomatically create new log file with permissions 0600 (only owner can read and write) under mondodb user and group
delaycompressPostpone compression of the previous log file to the next rotation cycle
compressPostpone compression of the previous log file to the next rotation cycle. This only has effect when used in combination with compress
sharedscriptsNormally, prerotate and postrotate scripts are run for each log which is rotated and the absolute path to the log file is passed as first argument to the script.
postrotateDefines beginning of the script which will run after log has been rotated
endscriptend of the script
dateextArchive old versions of log files adding a date extension like YYYYMMDD instead of simply adding a number
dateformatOnly %Y %m %d and %s specifiers are allowed. The default value is -%Y%m%d.
dateyesterdayUse yesterday’s instead of today’s date to create the dateext extension, so that the rotated log file has a date in its name that is the same as the timestamps within it.

Now test the logfile rotation, does it work properly or not?

 logrotate --force /etc/logrotate.d/mongodb

It should be look like this

-rw------- 1 mongodb mongodb 5.5M Sep 13 11:57 mongod.log
-rw------- 1 mongodb mongodb 1.9K Sep 13 12:19 mongod.log2020-09-11-1599913164.gz
-rw------- 1 mongodb mongodb 1.4K Sep 13 12:19 mongod.log2020-09-12-1599913195


You need to check with below command should rotate the log file.

sudo logrotate --force /etc/logrotate.d/mongodb

You can also troubleshoot by making debug flag on, and try to fix error for the logrotation by this command

sudo logrotate -d /etc/logrotate.d/mongodb

You can also face something like every thing you done it correctly and when you make the -f or –force command it will work, but it will not work at regular interval. Then you have just last resort measure to make this fired by cron job scheduler.

crontab -e // Open linux cron file
59 23 * * * sudo logrotate --force /etc/logrotate.d/mongodb //add this line if you need to rotate the log daily basis.
0 */1 * * * sudo logrotate --force /etc/logrotate.d/mongodb  // add this line if you want to rotate the log in hourly basis.

For cron scheduler timing you can check this site.


5 responses to “MongoDB Log Rotation Automation”

  1. papri laha Avatar
    papri laha


  2. Seems for reopen it works fine with /bin/kill -SIGUSR1. Without reopen in mingod.conf , it crashed mongod process.

    1. Yes reopen should be mention on configuration file, and the process id should link, in my case /data/db/mongod.lock I added this link ln -s /data/db/mongod.lock /var/run/, or you can directly link /data/db/mongod.lock.

  3. Alejandro Avatar

    Hi, could you tell me what version of mongo were you using and if that works for 4.4.2 please?

    1. I did it 4.2 and it worked. In 4.4 also –logRotate reopen option are there in MongoDB. logrotate unix tool will work independently, Hence it should work on 4.4 too.