If you are running Ubuntu, then there is an issue with folder ownership.
Run these commands:
-
Stop the MongoDB service
sudo service mongodb stop -
Remove the MongoDB lock file
sudo rm /var/lib/mongodb/mongod.lock -
Change ownership from root to the MongoDB path
sudo chown -R mongodb:mongodb /var/lib/mongodb/ -
Start the MongoDB service
sudo service mongodb start -
Test the mongo application
mongo
Then you will be able to execute successfully (I hope).
Reference: an answer on Stack Exchange site Database Administrators to Error: couldn’t connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91 when changing mongodb data directory!