Ubuntu – Run MongoDB service as daemon of SystemD on Ubuntu 15.10

15.10mongodbservicessystemdupstart

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?

Best Answer

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!