MongoDB 日志切换(Rotate Log Files)实战
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,MongoDB 日志切换(Rotate Log Files)实战1. 在mongo shell下,执行logRotate命令:use admin db.runCommand({logRotate
千家信息网最后更新 2026年10月31日MongoDB 日志切换(Rotate Log Files)实战
MongoDB 日志切换(Rotate Log Files)实战
1. 在mongo shell下,执行logRotate命令:
use admin db.runCommand({logRotate:1})
需要在mongos,mongod,config server运行。
该方式的变种:
a) 在unix shell下运行:
mongo localhost/admin -eval "dbo.runCommand({logRotate:1})"#!/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" donec) 将如下脚本保存到logRotate.js文件:
db.getMongo().getDB("admin").runCommand({logRotate:1})
创建脚本logRotate.sh:
#!/bin/sh # Clear old logs rm /var/log/mongodb/mongod.log.* # Rotate logs mongo logRotate.js
d) logRotate.sh //写到计划任务crontab即可(需要expect软件包)
#!/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. 使用SIGUSR1信号:
kill -SIGUSR1find /var/log/mongodb/mongodb.log.* -mtime +7 -delete
该方法的变种:
a) 用python写的定时脚本,每天产生一个新的log,超过7天的log自行删除。
#!/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]))在root下crontab -e编辑定时任务
0 2 * * * /root/mongo_log_rotate.py >/root/null 2>&1
3. 日志管理工具logrotate
自动化的最好方式是使用logrotate,其中copytruncate参数能更好工作。
拷贝以下代码到/etc/logrotate.d/mongodb文件中,确保脚本中的路径和文件名正确。
# 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/mongodb4. Mongodb bug
mongodb稳定性差强人意。在切换过程中也会导致mongodb进程终止。
具体内容可以查看下mongodb bug系统:SERVER-4739、SERVER-3339。
脚本
文件
日志
切换
任务
变种
方式
运行
实战
差强人意
代码
信号
内容
参数
命令
工具
拷贝
文件名
方法
最好
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技网
互联网扶贫科技
互联网科技公司怎么样
美国互联网黑科技
电子科技大学互联网
齐家互联网科技有限公司
人人互联网科技是培训
知域互联网科技有限公司大骗子
互联网科技产业特征
互联网科技公司营业执照怎么办理
南通创西澳互联网科技有限公司
云南省自由互联网科技有限公司
产业互联网科技服务公司
四川互联网展示科技馆设计
上海乐五互联网科技有限公司
互联网高科技行业有哪些企业
美国五大互联网科技公司市值多少
数字互联网科技有限公司
互联网金融金融科技
华人运通互联网科技公司
瑰夏咖啡
天津市摩尔互联网科技
互联网核心科技是什么
互联网科技资迅
互联网科技金融
互联网科技英文
华中科技大学互联网
无线互联网科技
城市宝互联网科技有限公司
云南互联网科技学费