简体   繁体   English

服务器崩溃后MongoDB无法启动

[英]MongoDB won't start after server crash

My Ubuntu computer had crashed, and when I restarted it MongoDB wasn't working. 我的Ubuntu计算机崩溃了,当我重新启动它时,MongoDB无法运行。 I tried the following commands, and got the following output: 我尝试了以下命令,并得到以下输出:

$ mongo
Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed

$ service mongodb status
mongodb stop/waiting

$ service mongodb restart
stop: Unknown instance: 
start: Rejected send message, 1 matched rules; type="method_call",
       sender=":1.57" (uid=1000 pid=2227 comm="start mongodb ")
       interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)"
       requested_reply="0"
       destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init")

$ tail /var/log/mongodb/mongodb.log
[initandlisten] exception in initAndListen: 12596 old lock file, terminating
dbexit: 
[initandlisten] shutdown: going to close listening sockets...
[initandlisten] shutdown: going to flush diaglog...
[initandlisten] shutdown: going to close sockets...
[initandlisten] shutdown: waiting for fs preallocator...
[initandlisten] shutdown: closing all files...
[initandlisten] closeAllFiles() finished
dbexit: really exiting now

(Output reformatted to match website layout.) (输出重新格式化以匹配网站布局。)

What happened? 发生了什么? How can I fix it? 我该如何解决?

The log file is telling you that you have an "old lock file" . 日志文件告诉您有一个“旧锁文件” MongoDB keeps a lock file while it's running. MongoDB在运行时保留一个文件。 It creates this file when it is started, and deletes it when it's stopped. 它在启动时创建此文件,并在文件停止时删除它。 When the computer crashes (or MongoDB crashes, eg via kill ), this file is not deleted, and thus the database does not start. 当计算机崩溃(或MongoDB崩溃,例如通过kill )时,不会删除此文件,因此数据库无法启动。 The existence of this file indicates unclean shutdown of MongoDB. 此文件的存在表明MongoDB的不正常关闭。

Two things can be done: 有两件事可以做:

  1. If this is a development machine and you haven't been using your database (and neither have your programs), you can remove the file manually. 如果这是一台开发机器并且您没有使用过您的数据库(并且没有您的程序),则可以手动删除该文件。 For MongoDB 2.2.2 running on Ubuntu 12.10, it's in /var/lib/mongodb/mongod.lock . 对于在Ubuntu 12.10上运行的MongoDB 2.2.2,它位于/var/lib/mongodb/mongod.lock For other versions, the file could be in a different path or it could be named mongo.lock . 对于其他版本,该文件可以位于不同的路径中,也可以命名为mongo.lock

  2. The safer route is to follow MongoDB's Durability and Repair guide. 更安全的路线是遵循MongoDB的耐久性和维修指南。 In summary, for a machine with the above configuration, you should execute the following commands: 总之,对于具有上述配置的计算机,您应该执行以下命令:

     sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/ sudo service mongod start 

all I had to do was run: sudo mongod --repair 我所要做的就是跑:sudo mongod --repair

then: 然后:

sudo mongod sudo mongod

Based on my experience, I usually delete the "mongod.lock" file that is inside the database folder - In my case: 根据我的经验,我通常会删除数据库文件夹中的“mongod.lock”文件 - 在我的情况下:

*I browse to where the database is installed on my ubuntu ie "data" folder.(cd data); *我浏览到我的ubuntu即“data”文件夹中安装数据库的位置。(cd data); list the files (ls) *Then, I will remove the "mongod.lock" file that was automatically created when the database crashed, by issuing "rm mongod.lock" file. 列出文件(ls)*然后,我将通过发出“rm mongod.lock”文件删除数据库崩溃时自动创建的“mongod.lock”文件。

After which I will either issue "./mongod" to start the mongo deamon or mongo to start the mongo shell. 之后我会发出“./mongod”来启动mongo deamon或者mongo来启动mongo shell。 And everything will be fine. 一切都会好起来的。

If you did not use monitoring tools like Bluepill or Monit etc you will have to face this issue because after server crash due to some reason mongo didnot start its daemon automatically then you have to make it work manually, like sudo service mongod restart I figured this issue but it needs some more tasks to be done, please make sure your dbpath at /etc/mongod.conf before starting your mongo daemon. 如果你没有使用像BluepillMonit等监控工具,你将不得不面对这个问题,因为在服务器崩溃后由于某种原因mongo没有自动启动它的守护进程然后你必须让它手动工作,比如sudo service mongod restart我认为这个问题,但它需要完成更多的任务,请在启动你的mongo守护进程之前确保你的dbpath在/etc/mongod.conf

For me it was 对我来说是

storage:
  dbPath: /var/lib/mongodb

When i enter mongod command it shows me MongoDB starting : pid=10795 port=27017 dbpath=/data/db 64-bit host=xyz.com make sure your dbpath is as same as mentioned in /etc/mongod.conf 当我输入mongod命令时,它显示MongoDB starting : pid=10795 port=27017 dbpath=/data/db 64-bit host=xyz.com确保你的dbpath与/etc/mongod.conf中提到的相同

To do this you can type sudo mongod --dbpath /var/lib/mongodb and then use mongod command to start your mongo process at your desired dbpath. 为此,您可以键入sudo mongod --dbpath /var/lib/mongodb ,然后使用mongod命令在所需的dbpath启动mongo进程。

FYI: start your mongo process with mongod command 仅供参考:使用mongod命令启动mongo进程

Check to see if you have enough free space on your server. 检查服务器上是否有足够的可用空间。 If there is no room left mongodb won't start. 如果没有空间,mongodb将无法启动。

This is probably not the best solution, but if you're desperate you can try this. 这可能不是最好的解决方案,但如果你绝望,你可以试试这个。 It seemed that only the journal was a problem for me, so I took these steps: 似乎只有期刊对我来说是一个问题,所以我采取了以下步骤:

  1. Create a new data directory . 创建一个新的数据目录 Possibly /var/lib/mongodb2 可能是/ var / lib / mongodb2
  2. Update your mongod.conf to point to the new data dir. 更新你的mongod.conf以指向新的数据目录。
  3. Start mongoDB. 启动 mongoDB。
  4. If it starts successfully, then you can shut down mongo again and proceed, otherwise you can stop reading here. 如果它成功启动,那么你可以再次关闭mongo并继续,否则你可以在这里停止阅读。
  5. Locate your previous data dir and copy the files for your database(s) to your new data directory (example, admin.0 admin.1 admin.ns, etc) 找到以前的数据目录并将数据库的文件复制到新的数据目录(例如,admin.0 admin.1 admin.ns等)
  6. Start mongoDB again (still using the new data directory) 再次启动mongoDB (仍然使用新的数据目录)

After completing these steps (took less than 5 min), I was up and running and all data appeared to be ok. 完成这些步骤(花了不到5分钟)后,我启动并运行,所有数据似乎都没问题。

Thank's guys. 多谢你们。 We also faced an issue where MongoDB was restarting over and over again an it was complaining about old lock file . 我们还遇到了一个问题,即MongoDB一次又一次地重新启动它正在抱怨旧的锁文件 I stopped MongoDB from Windows service list and then I deleted the mongod.lock file. 我从Windows服务列表中删除了MongoDB,然后删除了mongod.lock文件。 After that I was able to start MongoDB service correctly and it worked fine. 之后我能够正确启动MongoDB服务并且运行正常。

Removing .lock file from mongo data directory dbpath works for me. 从mongo数据目录dbpath删除.lock文件对我.lock

eg sudo sudo rm {data-directory}/mongod.lock 例如sudo sudo rm {data-directory}/mongod.lock

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM