Mongodb – Mongo exception: connect failed on reboot

mongodbUbuntu

I have Mongo installed on Ubuntu 16.04 machine, on VMWare. I start it using "mongo" command, last week there was no problem. But recently, it gives this error:

MongoDB shell version: 3.2.15
connecting to: test
2017-08-09T09:31:33.634+0300 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: errno:111 Connection refused
2017-08-09T09:31:33.712+0300 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:229:14
@(connect):1:6

exception: connect failed

I know how to solve it, it is here: https://stackoverflow.com/questions/12831939/couldnt-connect-to-server-127-0-0-127017/34835813#34835813

But everytime I reboot Ubuntu machine, the problem repeats. Why, and how can I prevent this happen everytime?
Thanks.

Edit: Problem solved. You can see the solution below, in my comments to JJussi's answer.

Best Answer

mongod is actual mongodb daemon what "is" the database. mongo is just client program what connects to mongod process (in this case; with sharded cluster it connects to mongos -process).

So, mongod process must be running before mongo can connect it.

You wrote that sudo systemctl enable mongod.service don't work anymore? Check if that service has (some how) changed its name. Just give command sudo systemctl|grep -i mongo and you can see if it is there and it is enabled. IF still your mongod process don't start automatically after reboot, check /var/log/mongodb/mongodb.log (you can check that lock file name with sudo grep path /etc/mongod.conf -command) says. What is the error? Fix that error (maybe chown of directory and files?) and try again with sudo systemctl restart mongod.service (or what ever is services name)