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

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://
2018-05-13T16:50:53.621+0000 W NETWORK  [thread1] Failed to connect to, 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, connection attempt failed :
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