惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - abce

Redis 如何通过槽优化分布式系统中的键值存储 Redis 集群架构 Redis 的演进之路:从缓存到 AI 数据库(V1.0至8.4) Redis 缓存过期和删除策略 Redis 的 aof-use-rdb-preamble 及其工作原理 MySQL主从之间具有不同数据类型的列的复制 MySQL 如何存储临时文件 理解mysql中的 local_infile 变量 pbm 还原物理备份提示executable file not found in $PATH. stderr MySQL 日志报错:Got packets out of order MySQL 8.0 已废弃的功能 sqlserver 删除job失败 查看 percona server for mongodb 的版本 MySQL 8 的哈希连接 MySQL 8.0 克隆插件及其原理 MySQL 保留字需要了解的内容 MongoDB 数据碎片处理 MongoDB 8.0 的复制 min_examined_row_limit 对慢查询日志的影响
有效管理 MongoDB 日志和系统资源
abce · 2025-04-24 · via 博客园 - abce

2025-04-24 13:28  abce  阅读(152)  评论()    收藏  举报

可以采用日志自动rotation、老日志压缩、只保留必要的日志等管理方式来管理mongodb的日志。

选择合适的日志rotation方式

要想做日志的rotation,必须执行以下步骤之一:

·向 mongod 或 mongos 进程发送 SIGUSR1 信号。

·运行 MongoDB logRotate 命令。

·运行 Linux/Unix logrotate 实用程序。

当使用了--logpath选项,或者使用systemLog.path设置,mongod和mongos实例会向日志文件报告所有活动和操作的实时记录。在向日志文件报告活动数据时,默认情况下,MongoDB 只在响应 logRotate 命令或当 mongod 或 mongos 进程从操作系统接收到 SIGUSR1 信号时才会 rotate 日志。server 日志和审计日志可通过 logRotate 命令一起或单独 rotate。

MongoDB 的标准日志rotate方法是将当前日志文件存档,并启动新的日志文件。为此,mongod 或 mongos 实例会重命名当前日志文件,在文件名中添加一个 ISODate 格式的 UTC 时间戳。然后,它会打开一个新日志文件,关闭旧日志文件,并将所有新日志条目发送到新日志文件。

可以通过将 systemLog.logRotate 或启动参数 --logRotate 设置为 reopen,配置 MongoDB 以支持 Linux/Unix 系统的 logrotate 日志轮换工具。启用 reopen 后,mongod 或 mongos 会关闭当前日志文件,然后重新打开一个同名的新日志文件。此操作需另外的进程配合在rotate前先重命名原日志文件。

最后,你可以使用 --syslog 选项配置 mongod 将日志数据发送到 syslog。在这种情况下,你可以利用其他日志rotate工具。

logRotate 命令不会复制到副本,只能登录到副本集的每个实例单独执行。

Log Rotation的默认行为(rename)

默认情况下,MongoDB 使用 --logRotate 的 rename 行为。使用重命名,mongod 或 mongos 会通过在文件名中附加 UTC 时间戳来重命名当前日志文件,打开一个新日志文件,关闭旧日志文件,并将所有新日志条目发送到新日志文件。

1.启动mongodb实例

mongod -v --logpath /var/log/mongodb/server1.log

也可以显式的指定 --logRotate rename 选项。

2.列出日志文件

在另外的终端,列出匹配的文件:

ls /var/log/mongodb/server1.log*

3.rotate日志

db.adminCommand( { logRotate : "server" } )

Log Rotation的reopen行为

当 systemLog.logRotate 或 --logRotate 被设置为 reopen 时,logRotate 会遵循典型的 Linux/Unix 行为,简单地关闭日志文件然后重新打开具有相同名称的日志文件。使用 reopen,mongod 会希望另一个进程在Rotate之前重命名文件,并且重新打开一个新创建的文件。

手动执行日志的rotation

同时对server log 和 aduit log 做rotation:

use admin
db.adminCommand({ logRotate: 1 })

只对 audit log 做rotation

use admin
db.adminCommand({ logRotate: "audit", comment: "Rotating audit log" })

只对server log 做rotation:

use admin
db.adminCommand( { logRotate : "server" } )

脚本自动做日志的rotation

对 server log 做rotation:

· 对当前的mongodb server log 做 rotation

·压缩之前的日志

vi /etc/logrotate.d/mongo

/var/log/mongodb/auditLog*.json {
    daily           # Rotate logs daily
    rotate 10       # Keep last 10 logs
    missingok       # Ignore if log file is missing
    compress        # Compress old logs
    delaycompress # Delay compression by one cycle to avoid issues
    notifempty     # Skip rotation if log file is empty
    create 644 mongodb mongodb  # Ensure correct ownership and permissions
    sharedscripts  # Ensures postrotate script runs only once
    postrotate
if pgrep mongod >/dev/null; then
    systemctl kill --signal=SIGUSR1 mongod 2>/dev/null || \
    /bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod.pid 2>/dev/null` >/dev/null 2>&1
fi
    endscript
}

对 audit log 做rotation:

vi  /etc/logrotate.d/mongoAudit 

/var/log/mongodb/auditLog*.json
{
  daily
  missingok
  compress
  delaycompress
  notifempty
  sharedscripts
  create 644 mongod mongod
  rotate 10
  postrotate
   if pgrep mongod >/dev/null; then
    systemctl kill --signal=SIGUSR1 mongod 2>/dev/null || \
    /bin/kill -SIGUSR1 `cat /var/run/mongodb/mongod.pid 2>/dev/null` >/dev/null 2>&1
fi
  endscript
  }

mongod.conf推荐的设置

vi  /etc/mongod.conf 

systemLog:
  quiet: false
  destination: file
  logAppend: true # Ensures logs are appended instead of being overwritten
  logRotate: reopen # Allows external logrotate to manage MongoDB logs
  path: /log/mongod.log

日志管理的最佳实践

1.单独的日志目录

将日志放在的磁盘分区上,避免对数据分区的影响

2.开启日志rotation

在mongod.conf配置文件中配置systemLog.logAppend、systemLog.logRotates变量,实现日志的无缝rotation

3.自动日志管理

编写脚本来压缩和清空旧的日志,比如只保留15天内的日志

4.日志所在磁盘使用的监控

定期检查日志所在磁盘的使用情况