问题描述
在没有stop mongodb服务的情况下reboot,导致在下次开机的时候mongodb无法连接,报错如下
MongoDB shell version v3.6.0-rc8
connecting to: mongodb://127.0.0.1:27017
2017-12-13T23:17:45.122+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2017-12-13T23:17:45.122+0800 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed
原因分析
打开日志目录,大致内容如下
分析不出什么结论,网上一查原来是有个mongod.lock文件占用了,删除即可,那就行动了。
解决步骤
- 移除mongod.lock文件
sudo rm /var/lib/mongodb/mongod.lock
- 修复mongodb
sudo mongod --repair
- 启动mongdb
sudo service mongod start
- 查看mongodb状态
service mongod status
显示如下:
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset:
Active: active (running) since Wed 2017-12-13 23:50:56 CST; 12min ago
Docs: https://docs.mongodb.org/manual
Main PID: 2662 (mongod)
CGroup: /system.slice/mongod.service
└─2662 /usr/bin/mongod --config /etc/mongod.conf
- 打开mongo控制台
mongo
显示如下
总结
问题成功解决,归根原因还在于上次关机的时候没有将mongod服务停止,导致mongod处于被锁状态,形成mongod.lock文件,而mongod服务启动又要去检查是否有mongod.lock文件,检查到有该文件,所以无法启动。