- Reference >
- Database Commands >
- repairDatabase
repairDatabase¶
-
repairDatabase¶ Warning
In general, if you have an intact copy of your data, such as would exist on a very recent backup or an intact member of a replica set, do not use
repairDatabaseor related options likedb.repairDatabase()in themongoshell ormongod --repair. Restore from an intact copy of your data.Note
When using journaling, there is almost never any need to run
repairDatabase. In the event of an unclean shutdown, the server will be able restore the data files to a pristine state automatically.The
repairDatabasecommand checks and repairs errors and inconsistencies with the data storage. The command is analogous to afsckcommand for file systems.If your
mongodinstance is not running with journaling the system experiences an unexpected system restart or crash, and you have no other intact replica set members with this data, you should run therepairDatabasecommand to ensure that there are no errors in the data storage.As a side effect, the
repairDatabasecommand will compact the database, as thecompactcommand, and also reduces the total size of the data files on disk. TherepairDatabasecommand will also recreate all indexes in the database.Use the following syntax:
Be aware that this command can take a long time to run if your database is large. In addition, it requires a quantity of free disk space equal to the size of your database. If you lack sufficient free space on the same volume, you can mount a separate volume and use that for the repair. In this case, you must run the command line and use the
--repairpathswitch to specify the folder in which to store the temporary repair files.Warning
This command obtains a global write lock and will block other operations until it has completed.
This command is accessible via a number of different avenues. You may:
Use the shell to run the above command, as above.
Use the
db.repairDatabase()in themongoshell.Run
mongoddirectly from your system’s shell. Make sure thatmongodisn’t already running, and that you issue this command as a user that has access to MongoDB’s data files. Run as:$ mongod --repair
To add a repair path:
$ mongod --repair --repairpath /opt/vol2/data
Note
This command will fail if your database is not a master or primary. In most cases, you should recover a corrupt secondary using the data from an existing intact node. If you must repair a secondary or slave node, first restart the node as a standalone mongod by omitting the
--replSetor--slaveoptions, as necessary.