12

MongoDB just supports versions of Ubuntu Long Term Support (LTS). The last is Ubuntu 14.04 LTS, where the init process is managed by Upstart proprietary of Canonical. However I'm using Ubuntu 15.10 with the Linux standard SystemD init process. So I can't start the MongoDB service on boot.

When I read the service status or try start it, show the message "failed to load":

> systemctl status mongod
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
> sudo systemctl start mongod
Failed to start mongod.service: Unit mongod.service failed to load: No such file or directory.

I'm running the official MongoDB 3.2 Community Edition (mongodb-org) from https://docs.mongodb.org/master/tutorial/install-mongodb-on-ubuntu/ not the MongoDB 2.6 meta-package (mongodb) from Ubuntu repository.

> sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
> echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
> sudo apt-get update
> sudo apt-get install -y mongodb-org

Someone knows how to start the MongoDB with SystemD?

  • 1
    my problem was a custom config file that was not forking mongod, here is the snippet from the default mongod conf that I was missing, processManagement: fork: true pidFilePath: /var/run/mongod.pid – Pykler Jan 10 '17 at 16:23
25

I managed start the MongoDB service with SystemD on boot:

I uninstalled the official meta-package (mongodb-org) v3.2, then I installed the meta-package (mongodb) v2.6 from Ubuntu repository:

> sudo apt-get remove mongodb-org
> sudo apt-get install mongodb

Create the service config file as shown below:

> cd /lib/systemd/system
> sudo touch mongodb.service
> sudo nano mongodb.service
[Unit]
Description=An object/document-oriented database
Documentation=man:mongod(1)
After=network.target

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

[Install]
WantedBy=multi-user.target

Verify in the list if the service is enabled or disabled using the command below:

> systemctl list-unit-files --type=service
...
mongodb.service             disabled
...

If it is disabled or not in the list, enable it:

> sudo systemctl enable mongodb.service

Check again:

> systemctl list-unit-files --type=service 
...
mongodb.service             enabled
...

Now you can managing the service on SystemD init process:

> systemctl status mongodb
> sudo systemctl stop mongodb
> sudo systemctl start mongodb
> sudo systemctl restart mongodb

Enjoy!

  • See askubuntu.com/a/617869/43344 for a pointer to the parts of the service definition that you missed. Also note that service units supplied and written by the local administrator should not go in /lib. – JdeBP Mar 22 '16 at 10:27
  • 1
    That service unit was written by Canonical, not by a local administrator, then they must reside in the /lib. The parts of the service definition are good enough to start the service, the additional settings do not take significant effect, so they are considered complementary. If you add the PIDFile=/var/run/mongodb/mongod.pid line and the run instruction on ExecStart= line, you get an error, because I'm using (mongodb) meta-package 2.6, not the (mongodb-org) meta-package 3.2. – Fernando Santucci Mar 23 '16 at 2:20
  • 1
    Why down voted, I upvoted. I also faced a similar issue and this answer helped, thanks. – Tien Do May 9 '16 at 14:43
  • This was super helpful moving from Ubuntu 14 to 18, my only addition would be that I had to recreate the data and log directories and chown them before mongod would start via systemctl. Nothing the install docs wouldn't have told me but in case it's helpful! – Tom J Nowell May 17 '19 at 16:30
6

On default install from MongoDB repository, in order to have automatic startup of mongod service, all you need to do is enable system.d service:

systemctl enable mongod.service (pay attention to mongod service name, not mongodb)

NOTE: this is tested on both Debian/GNU Jessie 8.x with systemd and Ubuntu Xerus 16.04 LTS.

  • This stands true for Ubuntu Xerus 16.04 LTS as well. After installing from official repo, you need to enable systemd service unit! I just confirmed it with another instance installation... :) – stamster Feb 9 '17 at 12:57
  • 1
    The file is present in /lib/systemd/system/mongod.service in my Ubuntu 16.04 – Jyotman Singh Apr 12 '17 at 11:31
  • Yes, systemd unit definition is there but it's disabled, so you need to enable it in order to have it automatically started each time you reboot your instance. – stamster Apr 12 '17 at 14:18

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.