Mongodb无法正常启动的解决方案
This evening I tried to connect to my MongoDB instance using the command line mongo tool and got the following error message: Error: couldn't connect to server 127.0.0.1} (anon):1137
. It turns out that the server wasn't running due to an improper shutdown at some point in the past. Here's what you need to do to fix it.
Note: my instructions are specific to Ubuntu 10.04 (& probably other versions of Ubuntu).
Why Won't MongoDB Start?
One of the lame things about MongoDB is that it doesn't handle a crash on it's own very well. If the database crashes or otherwise doesn't shut down properly, then the next time you try to start it, you'll get a nice error saying that there is an old lockfile and that you probably need to remove it and run mongod --repair. However, on my laptop running Ubuntu 10.04, I ran into a few confusing things that I'd like to share, hopefully to save someone else out there some time.
Symptoms
- Try to connect (type
mongo
on the command line). You should get a connect failed error. - Verify that MongoDB is not running. Run
sudo status mongodb
. It should reportmongodb stop/waiting
. - Verify that you are unable to start MongoDB. Run
sudo start mongodb
. It will reportmongodb start/running, process XXXX
no matter what. But if you runsudo status mongodb
again, you'll get stop/waiting. - Check your logs to see that you in fact have the same problem as me. Your logs should be at /var/log/mongodb/mongodb.log. You should see an error like this:
************** old lock file: /var/lib/mongodb/mongod.lock. probably means unclean shutdown recommend removing file and running --repair see: http://dochub.mongodb.org/core/repair for more information *************
Instructions To Get MongoDB Up Again
- Manually remove the lockfile:
sudo rm /var/lib/mongodb/mongod.lock
- Run the repair script:
sudo -u mongodb mongod -f /etc/mongodb.conf --repair.
- You must run this command as the mongodb user. If you run it as root, then root will own files in /var/lib/mongodb/ that are necessary to run the mongodb daemon and therefore when the daemon trys to run later as the mongodb user, it won't have permissions to start. In that case you'll get this error:
Unable to create / open lock file for lockfilepath: /var/lib/mongodb/mongod.lock errno:13 Permission denied, terminating
. - On Ubuntu, you must specify the configuration file /etc/mongodb.conf using the -f flag. Otherwise it will look for the data files in the wrong place and you will see the following error:
dbpath (/data/db/) does not exist, terminating
.
- You must run this command as the mongodb user. If you run it as root, then root will own files in /var/lib/mongodb/ that are necessary to run the mongodb daemon and therefore when the daemon trys to run later as the mongodb user, it won't have permissions to start. In that case you'll get this error:
- Now you can start your MongoDB server with
sudo start mongodb
and verify it is running withsudo status mongodb
and by trying to connect to it withmongo test
.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)