MongoDB log switching (RotateLogFiles) Practice

Source: Internet
Author: User
Tags mongo shell
MongoDB log switching (RotateLogFiles) Practice 1. In mongoshell, run the logRotate command: useadmindb. runCommand ({logRotate: 1}) and run it on mongos, mongod, and configserver. A) Run: mongolocalhostadmin-eval "dbo. runComma" in unixshell.

MongoDB Log Files (Rotate Log Files) Practice 1. In mongo shell, run the logRotate command: useadmindb. runCommand ({logRotate: 1}) and run it on mongos, mongod, and config server. A) run the mongolocalhost/admin-eval "dbo. runComma" command in the unix shell.

MongoDB Log switching (Rotate Log Files) Practice

1. Run the logRotate command in mongo shell:

use admin    db.runCommand({logRotate:1})


It must be run on mongos, mongod, and config server.


Variants of this method:

A) run in unix shell:

mongo localhost/admin –eval “dbo.runCommand({logRotate:1})”


B) 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

C) Save the following script to the logRotate. js file:

db.getMongo().getDB(“admin”).runCommand({logRotate:1})


Create the script logRotate. sh:

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

D) logRotate. sh // write it to the crontab of the scheduled task (the audit CT package is required)

#!/usr/bin/expect –f    spawn /usr/local/mongodb/bin/mongo admin -udev -ptest –quiet    expect ">"    send db.runCommand("logRotate")    send "\r\n"    expect ">"    send "exit"

2. Use the SIGUSR1 signal:

kill –SIGUSR1 
 
      find /var/log/mongodb/mongodb.log.* -mtime +7 –delete
 


Variants of this method:

A) Use a scheduled script written in python to generate a new log every day and delete the logs that have been written for more than 7 days.

#!/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 - ws2).days > 7:v_del = commands.getoutput("/bin/rm -rf /var/log/mongodb//%s" % (ws[1]))

Edit the scheduled task in crontab-e under root.

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

3. logrotate

The best way to automate is to use logrotate, where the copytruncate parameter can work better.

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

# 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 unsatisfactory. The mongodb process will also be terminated during the switchover.
For details, see mongodb bug Systems: SERVER-4739 and SERVER-3339.

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.