Logrotate with MongoDB

MongoDB packages are shipped with logging enabled in configuration but without a script to rotate the logfile. There are two build-in ways to let MongoDB rotate its logfile. You can execute db.runCommand("logRotate"); from the mongo shell or kill -SIGUSR1 $(cat /var/lib/mongodb/mongod.lock) from unix shell.

The best way to automate this is to use logrotate. Copy the following code into/etc/logrotate.d/mongodb and make sure that pathes and filenames in the script correspond with those in your system.

01 /var/log/mongodb/mongodb.log {
02     daily
03     rotate 7
04     compress
05     missingok
06     notifempty
07     sharedscripts
08     postrotate
09         /bin/kill -SIGUSR1 `cat /var/lib/mongodb/mongod.lock` && \
10         rm -f /var/log/mongodb/mongodb.log.????-??-??T??-??-??
11     endscript
12 }

It rotates the logfile /var/log/mongodb/mongodb.log on a daily basis and keeps them for 7 days compressed. Since MongoDB’s build-in logRotate conflicts with logrotate, it also cleans up the empty, log-rotated files created by MongoDB.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值