服务器崩溃后 MongoDB 无法启动
我的 Ubuntu 计算机崩溃了,当我重新启动它时,MongoDB 无法正常工作.我尝试了以下命令,并得到以下输出:
My Ubuntu computer had crashed, and when I restarted it MongoDB wasn't working. 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?
日志文件告诉您您有一个旧锁文件".MongoDB 在运行时保留一个 lock 文件.它在启动时创建此文件,并在停止时将其删除.当计算机崩溃(或 MongoDB 崩溃,例如通过 kill
)时,该文件不会被删除,因此数据库不会启动.该文件的存在表明MongoDB非正常关闭.
The log file is telling you that you have an "old lock file". MongoDB keeps a lock file while it's running. It creates this file when it is started, and deletes it when it's stopped. When the computer crashes (or MongoDB crashes, e.g. via kill
), this file is not deleted, and thus the database does not start. The existence of this file indicates unclean shutdown of MongoDB.
可以做两件事:
如果这是一台开发机器并且您没有使用过您的数据库(也没有使用过您的程序),您可以手动删除该文件.对于在 Ubuntu 12.10 上运行的 MongoDB 2.2.2,它位于
/var/lib/mongodb/mongod.lock
.对于其他版本,文件可能位于不同的路径中,也可能命名为mongo.lock
.
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
. For other versions, the file could be in a different path or it could be namedmongo.lock
.
更安全的方法是遵循 MongoDB 的耐久性和修复 指导.综上所述,对于具有上述配置的机器,您应该执行以下命令:
The safer route is to follow MongoDB's Durability and Repair guide. 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