Database Manual / Self-Managed Deployments / Administration / Backup Methods

Recover a Self-Managed Standalone after an Unexpected Shutdown在意外关机后恢复自我管理的独立设备

Warning

The following procedure applies to standalone mongod instance version 8.3. For other MongoDB versions, refer to the corresponding version of the manual.以下过程适用于独立mongod实例版本8.3。对于其他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 Resync a Member of a Self-Managed Replica Set.相反,您应该从备份还原或从该集的另一个成员重新同步,如“重新同步自我管理副本集的成员”中所述。

Tip

If you are running with journaling enabled, there is almost never any need to run repair since the server can use the journal files to restore the data files to a clean state automatically. However, you may need to run repair in cases where you need to recover from a disk-level data corruption.如果您在启用日志记录的情况下运行,则几乎不需要运行修复,因为服务器可以使用日志文件自动将数据文件还原到干净状态。但是,在需要从磁盘级数据损坏中恢复的情况下,您可能需要运行修复。

Disk-level data corruption or missing data files can prevent mongod instance from starting, and journal files may be insufficient to recover automatically:磁盘级数据损坏或丢失数据文件会阻止mongod实例启动,日志文件可能不足以自动恢复:

2018-10-24T18:05:18.248-04:00 W STORAGE  [initandlisten] Detected unclean shutdown - mongod.lock is not empty.

...

2018-10-24T17:24:53.122-04:00 E STORAGE [initandlisten] Failed to get the cursor for uri: table:collection-2-6854866147293273505
2018-10-24T17:24:53.122-04:00 E STORAGE [initandlisten] This may be due to missing data files. ...

...

***aborting after fassert() failure

In such cases, your dbPath contains a non-empty mongod.lock file.在这种情况下,dbPath包含一个非空的mongod.lock文件。

The following procedure uses mongod --repair to recover from these cases:以下过程使用mongod --repair从这些情况中恢复:

Warning

Only use mongod --repair if you have no other options. The operation removes and does not save any corrupt data during the repair process.只有在没有其他选择的情况下才使用mongod --repair。该操作在修复过程中删除且不保存任何损坏的数据。

For the WiredTiger storage engine, mongod --repair:对于WiredTiger存储引擎,mongod --repair

Procedure过程

Important

Run the repair operation as the same user that normally runs the mongod process to avoid changing the permissions of the MongoDB data files.以正常运行mongod进程的用户身份运行修复操作,以避免更改MongoDB数据文件的权限。

1

Create a backup of the data files.创建数据文件的备份。

Create a backup copy of the data files in the --dbpath.--dbpath中创建数据文件的备份副本。

2

Start mongod with --repair.--repair启动mongod

To repair the data files, start the mongod instance with the --repair option.要修复数据文件,请使用--repair选项启动mongod实例。

Issue a command similar to the following for your standalone:为单机发出类似于以下命令:

mongod --dbpath /data/db --repair

Upon completion, the dbpath should contain the repaired data files and an empty mongod.lock file. 完成后,dbpath应包含修复的数据文件和一个空的mongod.lock文件。[1]

Note

If the repair fails to complete for any reason, you must restart the instance with the --repair option to complete the repair.如果修复因任何原因未能完成,则必须使用--repair选项重新启动实例以完成修复。

[1] Generally, you should not manually remove the mongod.lock file. 通常,您不应该手动删除mongod.lock文件。Instead, use the above procedure to recover the database. In dire situations, you can remove the file, start the database using the possibly corrupt files, and attempt to recover data from the database. 相反,请使用上述过程恢复数据库。在严重的情况下,您可以删除该文件,使用可能损坏的文件启动数据库,并尝试从数据库中恢复数据。However, it is impossible to predict the state of the database in these situations.然而,在这些情况下,不可能预测数据库的状态。