MongoDB Log switch (Rotate log Files) combat

Source: Internet
Author: User
Tags mongo shell

MongoDB Log switch (Rotate log Files) combat

1. Under MONGO Shell, execute the logrotate command:

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


Need to run on Mongos,mongod,config server.


Variants of the method:

A) run under the Unix shell:

MONGO Localhost/admin–eval "Dbo.runcommand ({logrotate:1})"

b) run under the Unix shell:

MONGO Localhost/admin–eval "Dbo.runcommand ({logrotate:1})"

c) Bash script:

#!/bin/sh # # # Log rotate Mongo Localhost/admin–evel "Db.runcommand ({logrotate:1})" # # Compress newly rotated For f In/var/log/mongodb/mongod.log.???? -?? -?? T?? -??    -??; Do 7za a "$f. Z" "$f" rm–f "$f" done

D) Save the following script to the Logrotate.js file:

Db.getmongo (). Getdb ("admin"). RunCommand ({logrotate:1})


Create Script logrotate.sh:

#!/bin/sh # Clear old logs rm/var/log/mongodb/mongod.log.* # Rotate logs MONGO Logrotate.js

e) logrotate.sh//write to Scheduled task Crontab (requires expect package)

#!/usr/bin/expect–f Spawn/usr/local/mongodb/bin/mongo admin-udev-ptest–quiet expect ">" Send Db.runcomman D ("logRotate") send "\ r \ n" expect ">" Send "Exit"

2. Use the SIGUSR1 signal:

KILL–SIGUSR1 <mongod process id> find/var/log/mongodb/mongodb.log.*-mtime +7–delete


Variants of the method:

A) A timed script written in Python that generates a new log every day and logs for more than 7 days are deleted by itself.

#!/bin/env pythonimport sysimport osimport commandsimport datetime,time#get  Mongo pidmongo_pid = commands.getoutput ("/sbin/pidof mongod") print mongo_pid#send  Sig to mongoif mongo_pid !=  ":cmd = "/bin/kill -usr1 %s "  % (mongo_pid) print cmdmongo_rotate = commands.getoutput (cmd) else:print  "mongod  Is not running "#clean  log which > 7 daysstr_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:continuews1 = time.strptime (Ws[0], "%Y-%m-%d ") Ws2 = datetime.datetime (ws1[0],wS1[1],WS1[2]) if  (ARRAY_DAT_NOW&NBSP;-&NBSP;WS2) .days > 7:v_del =  Commands.getoutput ("/bin/rm -rf /var/log/mongodb//%s"  %  (Ws[1]))

Crontab–e Editing a timed task under root

0 2 * * */root/mongo_log_rotate.py >/root/null 2>&1

3. Log Management tool Logrotate

The best way to automate is to use Logrotate, where copytruncate parameters work better.

Copy the following code into the/etc/logrotate.d/mongodb file to ensure that the path and file name are correct in the script.

# vi/etc/logrotate.d/mongodb/var/log/mongodb/*.log {dailyrotate 7COMPRESSDATEEXTMISSINGOKNOTIFEMPTYSHAREDSCRIPTSCOPYTRUNCATEPOSTROTATE/BIN/KILL-SIGUSR1 ' cat/var/lib/mongo/ Mongod.lock 2>/dev/null ' 2>/dev/null | | trueendscript}# Logrotate–f/etc/logrotate.d/mongodb

4. Mongodb Bug
MongoDB stability is passable. The MongoDB process will also be terminated during the switchover process.
Specific content can be viewed under MongoDB bug system: SERVER-4739, SERVER-3339.

This article is from the SQL Server deep dives blog, so be sure to keep this source http://ultrasql.blog.51cto.com/9591438/1620675

MongoDB Log switch (Rotate log Files) combat

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.