在Wernfried-Domscheit的帮助下,经过大量研究、数周的试错和挫折,我终于在Windows上找到了一个可行的解决方案。
前提条件
1. 调整MongoDB服务的配置文件
- 停止服务
- 打开
mongod.cfg
并找到配置systemLog
的代码行:
systemLog:
destination: file
logAppend: true
path: E:\MongoDB\Server\4.4\log\mongod.log
logRotate: reopen
请确保覆盖您的 mongod.log
路径。同时,请确保设置了 logAppend: true
和 logRotate: reopen
。
- 删除当前的
mongod.log
文件
- 重新启动服务
2. 配置日志轮换配置
这是我的配置。您可以根据自己的需求进行自定义。但不要使用 copy
、copytruncate
和 create
,并且不要删除 postrotate
命令!
E:\MongoDB\Server\4.4\log\mongod.log {
nocompress
daily
size 100m
missingok
rotate 50
postrotate
E:\logrotate\notify_mongodb_service.bat
endscript
}
此配置每天或当日志大小超过100兆字节时,以未压缩的形式进行日志轮转。最多存储50个日志文件,旧文件将被删除。当轮转成功时,将执行postrotate脚本。
3. 创建notify_mongodb_service.bat
文件
此文件向MongoDB服务发送一个命令,表示将使用一个新文件。如果启用了授权,您还可以添加-u username -p password
。
E:\MongoDB\Server\4.4\bin\mongo.exe --eval "db.adminCommand({ logRotate: 1 })"
- 将路径更改为系统上的
mongo.exe
位置。
- 保存文件,并确保
logrotate
配置中的路径相同!(在postrotate
和endscript
之间的行)
该命令存储在额外的文件中,因为LogRotateWin解释mongo命令的括号({}
)并抛出异常。
4. 检查所有是否正常工作
打开日志文件夹。您应该看到一个单独的文件mongod.log
。
打开终端并检查日志轮换是否正在工作(-f
即使没有触发器被触发也会强制进行日志轮换):
logrotate logrotate.conf -f
应该出现一个新的日志文件。(对我来说是mongod.log.1
)
mongod.log
应该为空。
触发将被记录的事件。例如,使用monogdbCompass连接到您的MongoDB。
检查 mongod.log
。连接应该在其中记录。
5. 创建一个任务计划程序作业以定期运行日志轮换
我不会谈论任务计划程序的创建,但这是我的配置示例。您可以导入该文件并根据需要进行修改。
还要更改 logrotate.exe
的路径为您系统的位置。
此作业每小时运行一次,以检查是否触发了一个或多个日志轮换触发器:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http:
<RegistrationInfo>
<Date>2021-12-02T17:57:55.9541897</Date>
<Author>Grayknife</Author>
<Description>Execute Logrotate Hourly</Description>
<URI>\docker\LogRotate</URI>
</RegistrationInfo>
<Triggers>
<CalendarTrigger>
<Repetition>
<Interval>PT1H</Interval>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<StartBoundary>2021-12-02T18:00:00</StartBoundary>
<ExecutionTimeLimit>PT30M</ExecutionTimeLimit>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>1234</UserId>
<LogonType>Password</LogonType>
<RunLevel>LeastPrivilege</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>E:\logrotate\logrotate.exe</Command>
<Arguments>logrotate.conf</Arguments>
<WorkingDirectory>E:\logrotate</WorkingDirectory>
</Exec>
</Actions>
</Task>
我希望这个指南能够帮助到某些人。
编辑于2022年5月1日
我遇到了一个问题,当文件数量超过9个时,logrotate
会抛出异常:
E:\logrotate>E:\logrotate\logrotate.exe logrotate.conf -f
logrotate: Force option set to true
logrotate: Exception: Cannot create a file when that file already exists.
logrotate: StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.File.InternalMove(String sourceFileName, String destFileName, Boolean checkHost)
at logrotate.Program.AgeOutRotatedFiles(logrotateconf lrc, FileInfo fi, String rotate_path)
at logrotate.Program.RotateFile(logrotateconf lrc, FileInfo fi)
at logrotate.Program.Main(String[] args)
E:\logrotate>E:\logrotate\logrotate.exe logrotate.conf -f
logrotate: Force option set to true
logrotate: Exception: Access to the path 'E:\MongoDB\Server\4.4\log\mongod.log.10' is denied.
logrotate: StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at System.IO.File.Delete(String path)
at logrotate.Program.DeleteRotateFile(String m_filepath, logrotateconf lrc)
at logrotate.Program.AgeOutRotatedFiles(logrotateconf lrc, FileInfo fi, String rotate_path)
at logrotate.Program.RotateFile(logrotateconf lrc, FileInfo fi)
at logrotate.Program.Main(String[] args)
E:\logrotate>
暂时的解决方案是将文件最大数设置为9。