翻译或纠错本页面

Recover a Standalone after an Unexpected Shutdown

为了防止数据的不一致和损坏,请正常关闭数据库并且使用 durability journaling。默认情况下数据写入日志的周期为100ms,以此保证MongoDB在任何状态下可以恢复至一致状态,甚至是是由于电源切断或其他系统故障。

Detected unclean shutdown - mongod.lock is not empty.

如果你*并未*运行 replica set 并且*并未*打开日志功能,使用以下的步骤恢复不一致状态的数据。如果你已经运行复制集,你需要*经常*从备份恢复数据或者在空的 dbPath 下启动 mongod instance 实例,并让MongoDB执行初始同步来恢复数据。

警告

Do not use this tutorial to recover a member of a replica set. Instead, you should either restore from a backup or resync from another member of the set, as described in 复制集成员的重新同步.

[1]By default, MongoDB runs with journaling enabled to prevent data inconsistency in the event of an unclean shutdown. To shut down cleanly, see 停止 mongod 进程.

Procedure

重要

在未开启日志的情况下,如果发现 mongod 意外停止,并且 你也未启用复制集,在MongoDB重新运行之前需要启动修复程序。如果你已经启用复制集,你需要从备份中恢复数据并且让复制集进入 sync 状态以恢复数据。

1

Create a backup of the data files.

Create a backup copy of the data files in the --dbpath.

2

Start mongod with --repair.

To repair the data files, start the mongod instance with the --repair option. By default, during the repair operation, MongoDB uses a _tmp directory in the --dbpath.

If the mongod instance uses MMAPv1 Storage Engine, you can include the --repairpath option to specify an alternate temporary directory. For details, see --repairpath.

Issue a command similar to the following:

mongod --dbpath /data/db --repair

Upon completion, the dbpath should contain the repaired data files and an empty mongod.lock file. [2]

[2](1, 2)

在默认情况下 dbPath 的默认路径为``/data/db`` , mongod.lock 文件将在此,并且为非空文件,之后 mongod 将会拒绝启动,你将会在MongoDB记录中看到如下信息的输出: