解决Connection refused:connect@src/mongo/shell/mongo.js:374:17 @(connect):2:6 exception: connect failed exiting with code 1
报错信息如下:
由于进行了误操作,导致登录mongoDB后,不小心关了登录界面,重新输入mongo --port 提示上述信息。
关键错误信息:
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused : connect@src/mongo/shell/mongo.js:374:17 @(connect):2:6 exception: connect failed exiting with code 1
并且在宝塔上,可见服务已经关闭,而且也开启不了:
解决方案:
1、打开对应的log日志(一般在安装目录的log文件夹下),查看对应的操作详细,我这里可见是:{"t":{"$date":"2022-05-30T23:29:52.028+08:00"},"s":"E", "c":"NETWORK", "id":23024, "ctx":"initandlisten","msg":"Failed to unlink socket file","attr":{"path":"/tmp/mongodb-27017.sock","error":"Operation not permitted"}}
2、使用 rm -rf /tmp/mongodb-27017.sock
删除对应的文件(如果删除log中记录的sock文件依旧不行,请去到对应的路径下,把mongodb-XXX.sock都删掉)
3、开启mongoDB服务(我这是直接在宝塔开启,点击开启可见服务已经启动了(非宝塔,可用相关的命令启动服务))
4、重新输入mongodb --port XXX
可见可重新进入界面。
第三方工具验证效果:
至此,已成功。
注:
添加用户密码:
use admin
db.createUser({user:"user1",pwd:"123456",roles:[{role:"root",db:"admin"}]})
db.auth("user1","123456")