mongodb, question

Hello, everyone, why did mongodb delete mongoD. Lock when starting Mongo the next day and then all the data was lost? What is the reason?


First of all,mongod.lockYesmongoA lock file created in the hard disk after the server starts, if you exit normallymongodService, even if the file still exists, it will not affect the next startup.mongodServing. At the same time, this file will also record some states of mongod during the running process, so as to obtain abnormal information tips when restarting the service normally.

Note: generally only in abnormal exitmongodService, only need to deletemongod.lockDocuments. Exit normally without deletion.

Now the data is lost, it should be abnormal exit, i.e. the database crashes, if notOpen journal, then there is no way to ensure the integrity of the data.

Therefore, in a production environment, it is common toOpen journal.

If the data has not been backed up, you can only try mongod’s built-in repair and run it.mongodAdd--repairOptions:

mongod --dbpath /path/to/corrupt/data --repair

Do you want to delete itmongod.lockDocuments?

When mongod is normally closed, mongod.lock file will be cleared, and it will be known that it was completely closed last time when it is started next time. On the contrary, if the lock file is not cleared, mongod does not normally close.

If mongod detects that there is no normal shutdown, it will not let you start again. When starting, it will print the information of the last abnormal shutdown and require you to copy a copy of the data. However, some people have realized that this check can be bypassed by deleting this lock file. So there is a reason to delete this file. Deleting the lock file at startup means that you do not know or care if your data is corrupted. If Mongoose cannot be started, please repair your data instead of deleting the lock file.