Skip to content

Instantly share code, notes, and snippets.

@jwilm
Created June 22, 2013 22:49
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save jwilm/5842956 to your computer and use it in GitHub Desktop.
Save jwilm/5842956 to your computer and use it in GitHub Desktop.
MongoDB systemd service unit configuration
[Unit]
Description=MongoDB Database Service
Wants=network.target
After=network.target
[Service]
Type=forking
PIDFile=/var/run/mongodb/mongod.pid
ExecStart=/usr/local/bin/mongod --config /etc/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
User=mongodb
Group=mongodb
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target
@jwilm
Copy link
Author

jwilm commented Jun 22, 2013

Edit Jan 2024: this is 11 years old and you probably don't want to use this

Assumes mongod config file specifies pidfilepath = /var/run/mongodb/mongod.pid and is located at /etc/mongod.conf.

@eitanshapiro
Copy link

Need to make sure you don't have forking turned on in mongos or mongodb config file !

@Genda1ph
Copy link

Genda1ph commented Nov 7, 2017

I stumbled upon this gist googling for mongod systemd service config. And this is not really working properly for Ubuntu 16.

Assuming following mongod.conf:

storage:
  dbPath: "/var/lib/mongodb"
  journal:
    enabled: true

net:
  port: 27017
  bindIp: "127.0.0.1"

You want something like this:

[Unit]
Description=MongoDB Database Service
Wants=network.target
After=network.target

[Service]
Type=simple
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p /var/lib/mongod
ExecStartPre=/bin/chown mongodb:mongodb /var/lib/mongod
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
User=mongodb
Group=mongodb

[Install]
WantedBy=multi-user.target

This ensures:

  • /var/lib/mongodb exists and mongod is allowed to write there
  • mongod is started with proper privileges
  • mongod does not fork and is properly monitored and maintained by systemd
  • reload is supported

@AnupRaj
Copy link

AnupRaj commented Dec 14, 2017

@Genda1ph works for Ubuntu 17.10

@AlexVonB
Copy link

AlexVonB commented Sep 14, 2018

If you have services that depend on mongodb, the config is slightly more difficult because the default mongodb service returns before the database is online. That can cause quick applications to try and fail to connect to the database. To prevent this, add the following to /etc/mongod.conf:

processManagement:
    fork: true

Next, create /etc/systemd/system/mongod.service.d/mongod.conf (which extends the existing /lib/systemd/system/mongod.service) with the following content:

[Service]
Type=oneshot
RemainAfterExit=yes

That causes sudo systemctl start mongod to start the server in a forked process and return only AFTER the database is up and ready for connections. This way, dependent services only get invoked after the db is live.

@miqmago
Copy link

miqmago commented Apr 5, 2019

@AlexVonB I've tried your configuration but still having the fast application in trouble. See the time of start of each service:

● mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
  Drop-In: /etc/systemd/system/mongod.service.d
           └─mongod.conf
   Active: active (exited) since Fri 2019-04-05 19:02:04 CEST; 3min 16s ago
     Docs: https://docs.mongodb.org/manual
  Process: 1323 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=0/SUCCESS)
 Main PID: 1323 (code=exited, status=0/SUCCESS)
    Tasks: 52
   Memory: 352.2M
      CPU: 5.884s
   CGroup: /system.slice/mongod.service
           └─1538 /usr/bin/mongod --config /etc/mongod.conf

Apr 05 19:01:51 vps100000 systemd[1]: Starting High-performance, schema-free document-oriented database...
Apr 05 19:01:52 vps100000 mongod[1323]: about to fork child process, waiting until server is ready for connections.
Apr 05 19:01:52 vps100000 mongod[1323]: forked process: 1538
Apr 05 19:02:04 vps100000 systemd[1]: Started High-performance, schema-free document-oriented database.
● widestage.service - widestage node.js server
   Loaded: loaded (/mnt/widestage/widestage.service; disabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-04-05 19:01:51 CEST; 1min 32s ago
 Main PID: 1347 (node)
    Tasks: 10
   Memory: 49.5M
      CPU: 2.195s
   CGroup: /system.slice/widestage.service
           └─1347 /home/ubuntu/.nvm/versions/node/v6.10.0/bin/node /mnt/widestage/server.js

Apr 05 19:01:51 vps100000 systemd[1]: Started widestage node.js server.
Apr 05 19:01:57 vps100000 widestage[1347]: mongo DB connection
Apr 05 19:02:01 vps100000 widestage[1347]: Server running at http://0.0.0.0:3111/ in worker 1347
Apr 05 19:02:01 vps100000 widestage[1347]: Mongoose default connection disconnected
Apr 05 19:02:01 vps100000 widestage[1347]: Mongoose default connection error: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

Widestage service:

[Unit]
Description=widestage node.js server
After=network.target nginx.target mongod.target

[Service]
ExecStart=/usr/bin/taskset -c 0 /home/ubuntu/.nvm/versions/node/v6.10.0/bin/node /mnt/widestage/server.js
# Required on some systems
WorkingDirectory=/mnt/widestage
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to syslog
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=widestage
User=ubuntu
#Group=<alternate group>
Environment=NODE_ENV=production
Environment=FOREVER_ROOT=/mnt/widestage
Environment=PORT=3111

[Install]
WantedBy=multi-user.target

@danmutblix
Copy link

@jwilm thanks for a nice unit file, I have one question though, how is the $MAINPID set?

Assumes mongod config file specifies pidfilepath = /var/run/mongodb/mongod.pid and is located at /etc/mongod.conf.

@danmutblix
Copy link

@jwilm, I figured it out, it's an environment variable set by systemctl

systemctl show mongod will print the MainPID among other things 👍

@KojoEnch
Copy link

KojoEnch commented Sep 9, 2020

@Genda1ph yours is perfect for Debian 9.
systemctl enable mongod and voila 👍

@Auze
Copy link

Auze commented Jan 13, 2021

if relevant, here for CentOS 7.8 :

[Unit]
Description=MongoDB Database Service
Wants=network.target
After=network.target

[Service]
Type=forking
PIDFile=/var/run/mongodb/mongod.pid
ExecStartPre=/bin/mkdir -p /var/lib/mongo
ExecStartPre=/bin/chown mongod:mongod /var/lib/mongo
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
User=mongod
Group=mongod
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target

@Mohamed-Bala
Copy link

mongodb.service
Loaded: masked (Reason: Unit mongodb.service is masked.)
Active: failed (Result: signal) since Wed 2021-04-28 10:04:49 CAT; 44min a>
Main PID: 6242 (code=killed, signal=ABRT)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment