MongoDb: Fix error "couldn't connect to server 127.0.0.1:27017"

You can follow the below steps to resolve the error when trying to mongodb with command "mongo" on Ubuntu

MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017/%E2%80%93repair
2018-05-13T16:50:53.621+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-05-13T16:50:53.622+0000 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

1) Remove .lock file

sudo rm /var/lib/mongodb/mongod.lock

2) Repair the mongodb

sudo mongod --repair

3) Restart Mongodb

sudo service mongod restart

Written on 14 May 2018

Share your thoughts