对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

logRotate(数据库命令)

logRotate

The logRotate command is an administrative command that allows you to rotate the MongoDB server log and/or audit log to prevent a single logfile from consuming too much disk space.

You must issue the logRotate command against the admin database.

此命令可用于以下环境中托管的部署:

重要

This command is not supported in MongoDB Atlas clusters. For information on Atlas support for all commands, see Unsupported Commands.

该命令具有以下语法:

db.adminCommand(
{
logRotate: <integer or string>,
comment: <string>
}
)

该命令接受以下字段:

字段
类型
说明

logRotate

整数或字符串

要轮换的一个或多个日志,按以下方式进行:

  • 1 -- 切换服务器和审核日志

  • "server" -- 仅轮换服务器日志

  • "audit" -- 仅切换审核日志

comment

字符串

可选。日志轮换时服务器记录到日志文件和审核文件的消息。

您也可以通过向 mongod 进程发送 SIGUSR1 信号来轮换日志。

例如,如果正在运行的 mongod 实例的进程 ID (PID) 为 2200,则以下命令在 Linux 上为该实例轮转日志文件:

kill -SIGUSR1 2200

The systemLog.logRotate setting or --logRotate option specify logRotate's behavior.

When systemLog.logRotate or --logRotate are set to rename, logRotate renames the existing log file by appending the current timestamp to the filename. The appended timestamp has the following form:

<YYYY>-<mm>-<DD>T<HH>-<MM>-<SS>

Then logRotate creates a new log file with the same name as originally specified by the systemLog.path setting to mongod or mongos.

When systemLog.logRotate or --logRotate are set to reopen, logRotate follows the typical Linux/Unix behavior, and simply closes the log file then reopens a log file with the same name. With reopen, mongod expects that another process renames the file prior to the rotation, and that the reopen results in the creation of a new file.

下面的示例同时轮换服务器日志和审核日志:

db.adminCommand( { logRotate: 1 } )

以下示例仅轮换审核日志,并在轮换时向日志文件提供自定义消息:

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