Practice of MongoDB Log switching (Rotate Log Files)
Practice of MongoDB Log switching (Rotate Log Files)
1. Under mongo shell, execute the logRotate command:
Use admin db.runCommand ({logRotate:1})
Need to run on mongos,mongod,config server.
Variants of this method:
A) run under 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 to scheduled task crontab (expect package 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"
two。 Use SIGUSR1 signals:
Kill-SIGUSR1 find / var/log/mongodb/mongodb.log.*-mtime + 7-delete
Variants of this method:
A) A timed script written in python generates a new log every day, and the log for more than 7 days is 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-ws2). Days > 7:v_del = commands.getoutput ("/ bin/rm-rf / var/log/mongodb//%s"% (ws [1]))
Edit scheduled tasks in crontab-e 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 the copytruncate parameter works better.
Copy the following code to the / etc/logrotate.d/mongodb file and make sure 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
The stability of mongodb is not satisfactory. It can also cause the mongodb process to terminate during the switching process.
For more information, please see the mongodb bug system: SERVER-4739, SERVER-3339.