Tuesday, July 30, 2019

Enable automatic logrotation MOngodb

HI There,

this document will help to enable automatic logrotate for mongod instance.


Manually you can rotate the logfile as below

    > use admin
    > db.runCommand( { logRotate : 1 } )
For automating it, follow below


Go to directory  /etc/logrotate.d

vi mongodb

--add below to mongodb file
++++++++++++++++++++++++++++++++++++++++++++++++
/var/log/mongodb/mongod.log
{
   rotate 7
   daily
   size 1M
   missingok
   create 0600 mongod mongod
   delaycompress
   compress
   sharedscripts
   postrotate
     /bin/kill -SIGUSR1 $(cat /var/run/mongodb/mongod.pid)
   endscript
}

+++++++++++++++++++++++++++++++++++++++++++++++++++



'Daily' means that logs are rotated every day, 
'rotate 30' - logs will be kept for 30 days before being sent via email or deleted, 
'compress' - old logs are compressed with gzip, 
'missingok' - skip any errors in any log file is missing, 
'sharedscript's - scripts that are run script once, 
'postrotate' and 'endscript' - execute script after log rotation.

Note: In above script

"/var/log/mongodb/mongod.log" --- Should be replaced by your logfile 


To test if logrotate is working use below

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


Thanks

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home