Most straightforward way to restart mongodb.

sudo service mongod restart
Restart Mongodb Service

Auto restart mongodb when it crashes

  1. Edit your mongod service: sudo vim /lib/systemd/system/mongod.service
  2. Add Restart=always under service
  3. Reload systemctl daemon: sudo systemctl daemon-reload

Now whenever mongod gets killed. It’ll get respawned by systemctl

[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual

[Service]
User=mongodb
Group=mongodb
Restart=always
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
PIDFile=/var/run/mongodb/mongod.pid
# file size
LimitFSIZE=infinity
# cpu time
LimitCPU=infinity
# virtual memory size
LimitAS=infinity
# open files
LimitNOFILE=64000
# processes/threads
LimitNPROC=64000
# locked memory
LimitMEMLOCK=infinity
# total threads (user+kernel)
TasksMax=infinity
TasksAccounting=false
/lib/systemd/system/mongod.service