Skip to content

Instantly share code, notes, and snippets.

@jwilm
Created June 22, 2013 22:49
Show Gist options
  • 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
@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