MongoDB: Recover Data after an Unexpected Shutdown in Ubuntu

Note: MongoDBs data files are usually located at /data/db but in my Ubuntu installation they where in /var/lib/mongodb.

Repair preserving data:

sudo mkdir /var/lib/mongodb0
sudo mongod --dbpath /var/lib/mongodb --repair --repairpath /var/lib/mongodb0
sudo mongod --dbpath /var/lib/mongodb0

Now that the server is up with a repaired version (/var/lib/mongodb0) of your original data (from /var/lib/mongodb) use mongo client to verify that everything is OK. In that case replace /var/lib/mongodb with /var/lib/mongodb0.

sudo rm -rf /var/lib/mongodb
sudo mv /var/lib/mongodb0 /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb

Repair without preserving data:

sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --dbpath /var/lib/mongodb --repair

When this completes, the repaired data files will replace the original data files in the /var/lib/mongodb directory.


Leave a comment ?


Leave a Comment