On this page
Hundreds of drawers symbolizing a large amount of occupied space

Flush (Delete or Empty) MongoDB log file

Find out how to flush (delete, empty, clear) the MongoDB log file to improve performance and get more space on the disk.

Sometimes the database might work slowly and one of the reasons is a large log file. Tracking database activity is crucial for performance and issues monitoring. However, the log file may grow and therefore may cause the performance issue because of more and more writing and reading on a larger and larger log file. We may avoid such problems by scheduling regular log rotation and keeping the log file size below a defined threshold. Let’s get started on figuring out how to address this issue.

Locating Your MongoDB Logs

Use following command to locate the MongoDB logs: $ find / -name "mongod.log"

Clearing the Current MongoDB Logs

To clear the MongoDB log file, you can follow these steps:

  1. Open the terminal and navigate to the directory where the log file is located. The default location for MongoDB log files is /var/log/mongodb/, but you can find it using $ find / -name "mongod.log".
  2. Run the following command to clear the log file:

    $ cp /dev/null mongod.log

    This command will copy an empty file to mongod.log, effectively clearing its contents.

  3. Verify that the log file has been cleared by running:

    cat mongod.log

    This command should return an empty output.

Deleting the Old MongoDB Logs

To remove old logs make sure you are still in /var/log/mongodb directory (default MongoDB logs path) and list all files:

$ ls -al

There should be listed two files: 1. mongod.log and 2. mongod.log.date (e.g. mongod.log.2023-02-06T19-35-28). The old logs are in file number 2. Now we can remove this file:

$ rm mongod.log.2023-02-06T19-35-28

Determining the Size of the MongoDB Log Directory

All below actions refer to Ubuntu 22.04.

We need to check how large is the log file first. We’ll use the following command:

du -h --max-depth=1 /var/log

The command should display a list of directories along with their size calculation.

List of directories under var/log on Ubuntu 22.04

Now let’s list the content of the /var/log/mongodb:

$ cd /var/log/mongodb
$ ls -al

Result of /var/log/mongodb:

The list with the content of the /var/log/mongodb

Rotating MongoDB Log Files: A Comprehensive Guide

MongoDB is a great option for NoSQL, but sometimes we may forget to set some basic maintenance tasks, like managing log files. This leads to the fact that MongoDB logs can grow to tens of gigabytes. To avoid that log rotation should be set. Note that MongoDb does not automatically rotate log files as for official documentation.

What MongoDB says about log rotation:

MongoDB’s standard log rotation approach archives the current log file and starts a new one. To do this, the mongod or mongos instance renames the current log file by appending a UTC timestamp to the filename, in ISODate format. It then opens a new log file, closes the old log file, and sends all new log entries to the new log file.

Let’s then set the log to rotate for MongoDB. This will force a file rotation, mongod process will create another file, and you are able to delete the old one. The first step is to open Mongo Shell:

$ mongosh

Once the MongoDB shell opens use the following commands:

> use admin
> db.adminCommand({ logRotate: 1 })

Output:

{ ok: 1 }

Starting the Rotation Process: Tips and Tricks

First, we need to find the PID (process identification file) of mongod:

$ pidof mongod

or

$ pgrep mongod

Second, you can now send a signal (kill -SIGUSR1 <mongod pid>) to the mongod process. This won’t kill the process and it will just signal it to perform the rotation.

And that’s it. Now you should get not only more space on the disk but also speed up your MongoDB instance.

Related posts

Comments

Leave a Reply

Real-user monitoring for Accessibility, Performance, Security, SEO & Errors (SiteLint)