1 MongoDB 日志概述
在之前的博客我们介绍MongoDB的几种日志文件,如下:
Mongodb 目录结构 和 数据文件 说明
https://www.cndba.cn/dave/article/107955
MongoDB 4.4 中 journal 和 oplog 日志 说明
https://www.cndba.cn/dave/article/107979
在MongoDB 4.4 中,有三种类型的日志:
- Journal log: 类似Oracle 的Redo log。
- Oplog: 副本集的同步日志。
- DB log: MongoDB 的运行日志。
Mongodb log 是在配置文件systemLog中配置的:
systemLog:
path: /data/mongodb/logs/shard1.log
logAppend: true
logRotate: rename
destination: file
timeStampFormat: iso8601-local
官网说明如下:
Rotate Log Files
https://www.mongodb.com/docs/manual/tutorial/rotate-log-files/
为了防止MongoDB 的log文件无限增大,占用太多磁盘空间。建议启用log rotation并及时清理历史日志文件。
启用server log 之后,mongod和mongos 实例会在log 文件中记录所有用户的活动和操作。 默认情况下,只有在执行logRotate 命令后才会切换日志,当然, mongod 和mongos 进程接收到操作系统发出的 SIGUSR1 信号也会触发日志切换。logRotate 命令可以对server logs 和 audit logs 进行切换,也可以分开切换。
在执行切换时,mongod和mongos 实例会先对当前的log file 追加UTC 时间(ISODate)进行重命名,然后打开一个新文件,关闭重命名后的旧log 文件。
2 操作示例
2.1 启用Log Rotation 功能
mongod -v —logpath /var/log/mongodb/server1.log
也可以在配置文件中添加logRotate: rename 来实现:
systemLog:
path: /data/mongodb/logs/shard1.log
logAppend: true
logRotate: rename
destination: file
timeStampFormat: iso8601-local
2.2 查看日志文件
[dave@www.cndba.cn_1 ~]# cd /data/mongodb/logs/
[dave@www.cndba.cn_1 logs]# ll
total 478920
-rw------- 1 root root 186041932 May 16 16:34 configdb.log
-rw------- 1 root root 84077170 May 15 13:37 mongodb.log
drwx------ 2 root root 4096 May 16 16:34 mongos.diagnostic.data
-rw------- 1 root root 37890518 May 16 16:34 mongos.log
-rw-r--r-- 1 root root 133746 May 5 23:46 pbm_agent_configdb.log
-rw-r--r-- 1 root root 29411 May 5 11:07 pbm_agent_shard1.log
-rw-r--r-- 1 root root 75206 May 5 23:46 pbm_agent_shard2.log
-rw-r--r-- 1 root root 96529 May 5 23:46 pbm_agent_shard3.log
-rw------- 1 root root 58503071 May 16 16:34 shard1.log
-rw------- 1 root root 45324478 May 16 16:34 shard2.log
-rw------- 1 root root 45694230 May 16 16:33 shard3.log
[dave@www.cndba.cn_1 logs]#
2.3 切换日志
2.3.1 操作系统发送SIGUSR1命令
[dave@www.cndba.cn_1 logs]# ps -ef|grep shard1
root 7345 6680 0 16:35 pts/0 00:00:00 grep --color=auto shard1
root 10287 863 1 May11 ? 01:39:24 /usr/local/mongodb/bin/mongod -f /data/mongodb/etc/shard1.conf
[dave@www.cndba.cn_1 logs]# kill -SIGUSR1 10287
[dave@www.cndba.cn_1 logs]#
[dave@www.cndba.cn_1 logs]# pwd
/data/mongodb/logs
[dave@www.cndba.cn_1 logs]# ll shard1*
-rw------- 1 root root 2993 May 16 16:35 shard1.log
-rw------- 1 root root 58508124 May 16 16:35 shard1.log.2022-05-16T08-35-39
[dave@www.cndba.cn_1 logs]#
这里已经切换成功,旧文件格式:server1.log.timestamp。
2.3.2 执行logRotate命令
https://www.mongodb.com/docs/manual/reference/command/logRotate/
该命令有一个选项:
db.adminCommand({logRotate:
})
可以设置如下3个值,当设置为1时表示同时切换serer和audit:
db.adminCommand({logRotate: 1})
db.adminCommand({logRotate:server})
db.adminCommand({logRotate:audit})
shard1:PRIMARY> db.adminCommand({logRotate: 1})
{
"ok" : 1,
"$gleStats" : {
"lastOpTime" : Timestamp(0, 0),
"electionId" : ObjectId("7fffffff000000000000000c")
},
"lastCommittedOpTime" : Timestamp(1652690436, 1),
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(1652690437, 2),
"t" : NumberLong(-1)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(1652690437, 2),
"signature" : {
"hash" : BinData(0,"WV+oRE0inYDholcjn98d2Xb387k="),
"keyId" : NumberLong("7093529851058978835")
}
},
"operationTime" : Timestamp(1652690436, 1)
}
shard1:PRIMARY>
虽然官方讲有如下语法,但我这执行却报错了:
shard1:PRIMARY> db.adminCommand({logRotate: server})
uncaught exception: ReferenceError: server is not defined :
@(shell):1:18
shard1:PRIMARY> db.adminCommand({logRotate: audit})
uncaught exception: ReferenceError: audit is not defined :
@(shell):1:18
扫了一眼官网,有如下2个限制:
- Your mongod instance needs to be running with the —logpath [file] option in order to use logRotate
- Auditing must be enabled in order to rotate the audit log.
可能和我这里使用配置文件的方式启动Mongod 实例有关系,这里不在进行更多的测试。db.adminCommand({logRotate: 1})能执行也可以了。
版权声明:本文为博主原创文章,未经博主允许不得转载。