Docs HomeMongoDB Manual

Manage Journaling管理日记

On this page本页内容

MongoDB uses write ahead logging to an on-disk journal to guarantee write operation durability.MongoDB使用写前日志记录到磁盘上的日志,以保证写操作的持久性。

The WiredTiger storage engine does not require journaling to guarantee a consistent state after a crash. WiredTiger存储引擎不需要日志记录来保证崩溃后的一致状态。The database will be restored to the last consistent checkpoint during recovery. 数据库将在恢复过程中恢复到最后一个一致性检查点However, if MongoDB exits unexpectedly in between checkpoints, journaling is required to recover writes that occurred after the last checkpoint.但是,如果MongoDB在检查点之间意外退出,则需要日志记录来恢复上次检查点之后发生的写入。

If mongod stops unexpectedly, the program can recover everything written to the journal. 如果mongod意外停止,程序可以恢复写入日志的所有内容。MongoDB will re-apply the write operations on restart and maintain a consistent state. MongoDB将在重新启动时重新应用写入操作,并保持一致的状态。By default, the greatest extent of lost writes, i.e., those not made to the journal, are those made in the last 100 milliseconds, plus the time it takes to perform the actual journal writes. 默认情况下,最大程度的丢失写入,即未写入日志的写入,是在最近100毫秒内完成的写入,加上执行实际日志写入所需的时间。See commitIntervalMs for more information on the default.有关默认值的详细信息,请参阅commitIntervalMs

Procedures过程

Get Commit Acknowledgement获取提交确认

You can get commit acknowledgement with the Write Concern and the j option. 您可以通过写入关注j选项获得提交确认。For details, see Write Concern.有关详细信息,请参阅写入关注

Monitor Journal Status监视日志状态

The serverStatus command/db.serverStatus() method returns wiredTiger.log, which contains statistics on the journal.serverStatus命令/db.serverStatus()方法返回wiredTigerlog,其中包含日志的统计信息。

Recover Data After Unexpected Shutdown意外关闭后恢复数据

On a restart after a crash, MongoDB replays all journal files in the journal directory before the server becomes available. If MongoDB must replay journal files, mongod notes these events in the log output.在崩溃后重新启动时,MongoDB会在服务器可用之前重播日志目录中的所有日志文件。如果MongoDB必须重放日志文件,mongod会在日志输出中记录这些事件。

There is no reason to run --repair.没有理由运行--repair

Change WiredTiger Journal Compressor更换WiredTiger日志压缩器

With the WiredTiger storage engine, MongoDB, by default, uses the snappy compressor for the journal. 在WiredTiger存储引擎中,MongoDB默认情况下会为日志使用snappy压缩器。To specify a different compressions algorithm or no compression for a mongod instance:要为mongod实例指定不同的压缩算法或不指定压缩,请执行以下操作:

Tip

If you encounter an unclean shutdown for a mongod during this procedure, you must use the old compressor settings to recover using the journal files. Once recovered, you can retry the procedure.如果在此过程中遇到mongod的不干净关闭,则必须使用旧的压缩器设置使用日志文件进行恢复。恢复后,您可以重试该过程。

Use the following procedure to change the journal compressor for a standalone mongod instance:使用以下过程更改独立mongod实例的日志压缩器:

  1. Update storage.wiredTiger.engineConfig.journalCompressor to the new value.storage.wiredTiger.engineConfig.journalCompressor更新为新值。

    If you use command-line options instead of a configuration file, you must update the --wiredTigerJournalCompressor command-line option during the restart below.如果使用命令行选项而不是配置文件,则必须在下面的重新启动过程中更新--wiredTigerJournalCompressor命令行选项。

  2. Perform a clean shutdown of the mongod instance. 执行mongod实例的干净关闭。For example, connect mongosh to the instance and issue db.shutdownServer():例如,将mongosh连接到实例并发出db.shutdownServer()

    db.getSiblingDB('admin').shutdownServer()
  3. Once you have confirmed that the process is no longer running, restart the mongod instance:一旦确认进程不再运行,请重新启动mongod实例:

    • If you are using a configuration file:如果您正在使用配置文件:

      mongod -f <path/to/myconfig.conf>
    • If you are using command-line options instead of a configuration file, update --wiredTigerJournalCompressor to the new value.如果使用命令行选项而不是配置文件,请将--wiredTigerJournalCompressor更新为新值。

      mongod --wiredTigerJournalCompressor <differentCompressor|none>  ...

Use the following procedure to change the journal compressor for a member of a replica set:使用以下过程可以更改复制集成员的日志压缩器:

  1. Perform a clean shutdown of the mongod instance. 执行mongod实例的干净关闭。For example, connect mongosh to the instance and issue db.shutdownServer():例如,将mongosh连接到实例并发出db.shutdownServer()

    db.getSiblingDB('admin').shutdownServer()
  2. Update storage.wiredTiger.engineConfig.journalCompressor to the new value.storage.wiredTiger.engineConfig.journalCompressor更新为新值。

    If you use command-line options instead of a configuration file, you must update the command-line options during the restart below.如果使用命令行选项而不是配置文件,则必须在下面的重新启动过程中更新命令行选项。

  3. Restart the mongod instance:重新启动mongod实例:

    • If you are using a configuration file:如果您正在使用配置文件:

      mongod -f <path/to/myconfig.conf>
    • If you are using command-line options instead of a configuration file, update --wiredTigerJournalCompressor to the new value.如果使用命令行选项而不是配置文件,请将--wiredTigerJournalCompressor更新为新值。

      mongod --wiredTigerJournalCompressor <differentCompressor|none> ...

Use the following procedure to change the journal compressor for a member of a shard replica set or config server replica set:使用以下过程更改分片副本集或配置服务器副本集成员的日志压缩器:

  1. Perform a clean shutdown of the mongod instance. For example, connect mongosh to the instance and issue db.shutdownServer():执行mongod实例的干净关闭。例如,将mongosh连接到实例并发出db.shutdownServer()

    db.getSiblingDB('admin').shutdownServer()
  2. Update storage.wiredTiger.engineConfig.journalCompressor to the new value.storage.wiredTiger.engineConfig.journalCompressor更新为新值。

    If you use command-line options instead of a configuration file, you must update the command-line options during the restart below.如果使用命令行选项而不是配置文件,则必须在下面的重新启动过程中更新命令行选项。

  3. Restart the mongod instance:重新启动mongod实例:

    • If you are using a configuration file:如果您正在使用配置文件:

      mongod -f <path/to/myconfig.conf>
    • If you are using command-line options instead of a configuration file, update --wiredTigerJournalCompressor to the new value.如果使用命令行选项而不是配置文件,请将--wiredTigerJournalCompressor更新为新值。

      mongod --shardsvr --wiredTigerJournalCompressor <differentCompressor|none> --replSet ...