27

For whatever reason, whenever I try to start MongoDB as a service (sudo service mongod start) I get the following error:

Failed to start mongod.service: Unit mongod.service not found.

I followed the installation guide on the MongoDB site.

59

The instructions/packages on the MongoDB site are currently available for LTS Ubuntu 12.04 and 14.04 only. The packages are set up to use upstart instead of systemd. You need to create a systemctl file to be ready for systemd. Create it by

sudo nano /etc/systemd/system/mongodb.service

It will look like

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target

[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf

[Install]
WantedBy=multi-user.target

Now you can start the service and check its state by

sudo systemctl start mongodb
sudo systemctl status mongodb

and finally enable it permanently by

sudo systemctl enable mongodb

(Source: How to Install MongoDB on Ubuntu 16.04)

5
  • Sometimes it fails to start by an NETWORK error. In such case try deleting the sock file in /tmp Jul 23 '16 at 7:24
  • The official way to do it is to use mongod for the service name
    – Luca Steeb
    Sep 12 '16 at 15:25
  • 4
    Followed these instructions and tried to run sudo systemctl start mongodb, but received the following error message: "Failed to start mongodb.service: Unit mongodb.service is masked." Running "sudo systemctl unmask mongodb" fixed the error Nov 29 '16 at 15:23
  • you just saved my day Mar 9 '17 at 9:52
  • Thanks a log JeffRSon, it saved my day, I was struggling to get this working.
    – Pioneer
    Jul 17 '19 at 12:41
5

Just run sudo mongod to start the daemon and then sudo mongo to access the shell

3
  • 11
    Do you always run databases on your servers this way?
    – ruX
    Jul 28 '16 at 23:11
  • sadly this worked for me... So I needed to change permissions to mongo user Dec 19 '16 at 13:19
  • this worked for me, but I also needed to cd /, then sudo mkdir data then sudo mkdir /data/db.
    – edencorbin
    Apr 26 '17 at 20:18
1

Running following command worked for me. Please run mongo with config file.
Just Run mongod --config /etc/mongod.conf.

1
  • 2
    this option is removed in mongo 3.6.6
    – vijay
    Jul 24 '18 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.