After installing Mongodb , You may face the following error when use mongodb from terminal:
mongo.js:L112 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112
If you are running Ubuntu, then there is an issue with folder ownership.
Run these commands:
- Stop mongodb service
sudo service mongodb stop
- Remove mongodb lock file
sudo rm /var/lib/mongodb/mongod.lock
- Change ownership from root to mongodb path
sudo chown -R mongodb:mongodb /var/lib/mongodb/
- Start mongodb service
sudo service mongodb start
- Test mongo app
mongo
Source : stackoverflow