MongoDB日志轮换

环境和条件

我在CentOS7上执行了。

$ cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)

MongoDB的版本为3.4.2。

$ mongod --version
db version v3.4.2
git version: 3f76e40c105fc223b3e5aac3e20dcd026b83b38b
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
    distmod: rhel70
    distarch: x86_64
    target_arch: x86_64

步骤

编辑mongod.conf文件。
以下内容可供参考,涉及日志相关的配置。

系统日志选项

$ sudo /etc/mongod.conf

修改以下描述。

# logAppend: true #コメントアウトする
logRotate: rename

logAppend默认应该为false,但由于使用了MongoDB 2.6版本,它被设为了true,因此我将其注释掉了。

将logAppend设置为false时,日志仍未进行轮换?请注意。

systemLog.logAppend中包含以下描述:

When true, mongos or mongod appends new entries to the end of the existing log file when the mongos or mongod instance restarts. Without this option, mongod will back up the existing log and create a new file.

当为true时,而不是当为false时,由于没有这个选项,似乎最好的选择是把设置本身注释掉。

logRotate的默认选项是重命名,但也可以明确指定。

重新启动MongoDB。

$ sudo systemctl restart mongod

打开Mongo Shell,并按照以下命令发出指令。

$ mongo

> use admin;

> db.runCommand('logRotate', 1);

> exit

请确认日志文件的输出路径(可以在mongod.conf的systemLog:path中进行设置)。
如果日志文件已经被分割如下所示,则表示正常。

$ ls /var/log/mongodb -l
-rw-r--r-- 1 mongod mongod    7445330  4月 7 13:36 mongod.log
-rw-r--r-- 1 mongod mongod 2143691041  4月 7 12:05 mongod.log.2017-04-07T03-05-51
广告
将在 10 秒后关闭
bannerAds