search

Home  >  Q&A  >  body text

mongodb

大家好,为什么mongodb存入的数据,第二天启动mongo的时候,删掉mongod.lock然后数据全没了,是什么原因?

高洛峰高洛峰2863 days ago641

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-27 09:04:59

    Solution

    First of all, mongod.lockmongo服务端启动后在硬盘中创建的一个锁文件,如果你正常退出mongod服务,该文件即使还存在,也不会影响下一次启动mongodservice. At the same time, this file will also record some status of mongod during operation, so that exception information prompts can be obtained when the service is restarted normally.

    Note: Generally, files can only be exited mongod服务时,才需要删除mongod.lock abnormally. Exit normally, no need to delete.

    Now the data is lost, it should be an abnormal exit, that is, the database crashes. If there is no 开启journal, then there is no way to ensure the integrity of the data.

    So it usually happens in a production environment开启journal.

    If the data is not backed up, you can only try to use mongod's built-in repair, run mongod加上--repair option:

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

    Do you want to delete the mongod.lock file?

    When mongod is shut down normally, the mongod.lock file will be cleared. The next time you start it, you will know that it was completely shut down last time. On the contrary, if the lock file is not cleared, mongod does not shut down gracefully.

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

    reply
    0
  • Cancelreply