Deleting log file

Hello,
my mongod.log file has been increased a lot and due to that mongodb has been stopped and not able to serve any request. It has grown up to 90 GB and there is no space on server now. I want to clean the file to free up the space but couldn’t find the option to do so. I found a post saying roate the file, but my server doesn’t have any space. And rotating will create one more copy of 90 GB file. So, I have 2 questions

  1. How can I clean my mongod.log file to free up the space
  2. If I delete the file, will it get created again automatically?
    Please help

Hello,

Firstly, you may want to check why the log grew so big, suspecting you have high logging level, you could check the log level with the below:
db.getLogComponents()

rotating will not create a new copy, it will simply rename the existing one with a timestamp when the log rotation was initiated and start writing to a fresh mongo.log, you can then gzip the old log file or move the it to another disk if you have free space or you can delete it at this point if it’s no longer needed.

For your second question if the old log entries are not needed, I have also tried in my testing environment to empty the logfile entirely by issuing the below:

cat /dev/null > mongod.log

and then rotating the logfile as below:
mongo --eval ‘db.adminCommand( { logRotate : “server” } )’

and found that mongod process rotated the emptied file and created a fresh mongod.log and started writing to it.

4 Likes

I have the same issue, I had already check the server log level and it is in default level 0. I have also an hourly logrotation with logrotate.
Our /var/log/ partition could be full after 10 minutes with multi-process scripts using 64 workers.
Our MongoDB server shutdown when it couldn’t write in log partition.

Is there a way to handle this more than those two tips?

Thanks @Mohamed_Elshafey for your reply. I will check the things as you suggested.

@Mohamed_Elshafey this for mongodb atlas doesn’t help at all.
How could I delete the logs?
Do not delete the .log, delete the logs. That is, clean the document.

This db.getLogComponents() is just verbosity
Returns the current verbosity settings.

Either you put a button, a command or @Mohamed_Elshafey you are going to have to clean our logs one by one. I don’t care about the method.

I had the same problem, used Long Path Tool and the file got deleted without any issue.

If you run a self-managed MongoDB on Linux, then logrotate would be the standard solution.

A typical configuration looks like this:

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

and the logrotate config could be this one:

/var/log/mongodb/mongod.log{
  size 10M
  rotate 9
  sharedscripts
  postrotate
    if /usr/sbin/pidof -s mongod > /dev/null ; then kill -USR1 $( /usr/sbin/pidof mongod ) ; fi
  endscript
}

This sample will create logfile with a size of 10 MByte. After 10 MByte a new logfile is created and the old file is zipped. When you get 9 logfiles, then the oldest ones are deleted.
The kill -USR1 triggers mongod to create a new logfile.

Typically you run logrotate command once per day by cron job.

Note, to ensure that no log message gets lost, counter-intuitive you must first delete the old logfile and then run kill -USR1 or db.adminCommand( { logRotate : "server" } ) to rotate the log.