MongoDB Log Rotation

Source: Internet
Author: User
Tags mongo shell

Overview

MongoDB can use the standard method to implement log rotation, that is, to get to the current log and open a new one. To implement it, the Mongod instance renames the current log file by adding a UTC (GMT) timestamp at the end of the file name, then opens the new log file, closes the old log file, and sends all new log information to the new logfile.


Rotation Setps

MongoDB Standard log Rotation method is through the log rotation command, or let the mongod process to receive a SIGUSER1 signal, the following specific implementation methods


1 open Mongod process, ensure appending enabled

Mongod-v--logpath/var/log/mongodb/server1.log--logappend

2 See if there are MONGO log files in the terminal

ls/var/log/mongodb/server1.log*

3 If it is a Windows system, open the MONGO shell, go to the Admin database and enter the rotation command, i.e.

Use admin
Db.runcommand ({logrotate:1})

If it is a Linux system, run KILL-SIGUSR1 <mongod process at the terminal id>

4 Review the results again for a new MONGO log file generation

ls/var/log/mongodb/server1.log*


My Auto Script and Crontab

Here is the timed script I wrote in Python, which generates a new log every day, and logs for more than 10 days are deleted by itself.

#!/bin/env python
Import sys
import OS
Import commands
Import datetime, Time

#get MONGO pid
Mongo_pid = Commands.getoutput ("/sbin/pidof mongod")
Print mongo_pid

#send Sig to MONGO
if mongo_pid! = ':
    cmd = '/bin/kill-usr1%s '% (mongo_pid)
    Print CMD
    mongo_rotate = commands.getoutput (cmd)
Else:
    print "Mongod is not running ..."

#clean log which > ten days
Str_now = Time.strftime ("%y-%m-%d")
Dat_now = Time.strptime (Str_now, "%y-%m-%d")
Array_dat_now = Datetime.datetime (dat_now[0],dat_now[1],dat_now[2])
LNS = Commands.getoutput ("/bin/ls--full-time/var/log/mongodb/|awk ' {print $6, $9} ')
For ln in lns.split (' \ n '):
WS = Ln.split ()
If Len (ws)! = 2:
Continue
WS1 = Time.strptime (Ws[0], "%y-%m-%d")
WS2 = Datetime.datetime (ws1[0],ws1[1],ws1[2])
if (array_dat_now-ws2). Days > 10:
V_del = Commands.getoutput ("/bin/rm-rf/var/log/mongodb//%s"% (Ws[1]))


CRONTAB-E view scheduled Tasks under root

0 2 * * */home/jiangjianjian/mongo_log_rotate.py >/home/jiangjianjian/null 2>&1

MongoDB Log Rotation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.