To start installing MongoDB from the repository we just added, we issue the command:<\/p>\n
# sudo apt-get install -y mongodb-org<\/pre>\nAlthough the MongoDB repository now provides the unit file in the package, we left this part of the tutorial for educational purposes as it can be used to install other services.<\/p>\n
Now we need to create a systemd unit file for MongoDB. First, let us explain briefly what systemd unit files are. Unit files keep information about services, sockets, devices, basically, any resource managed by systemd which is an init system used by a large number of Linux distributions.<\/p>\n
Create the file in the \/etc\/systemd\/system\/ directory using nano:<\/p>\n
# sudo nano \/etc\/systemd\/system\/mongodb.service<\/pre>\nPaste the following text below:<\/p>\n
[Unit]\r\nDescription=High-performance, schema-free document-oriented database\r\nAfter=network.target\r\nDocumentation=https:\/\/docs.mongodb.org\/manual\r\n\r\n[Service]\r\nUser=mongodb\r\nGroup=mongodb\r\nExecStart=\/usr\/bin\/mongod --quiet --config \/etc\/mongod.conf\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n<\/pre>\nMake sure to save (press Ctrl+O) and close (press Ctrl+X) the file.
\nNow we have to update systemd to include our newly created service and we enable and start the service:<\/p>\n
# sudo systemctl daemon-reload\r\n # sudo systemctl enable mongod\r\n # sudo systemctl start mongod\r\n<\/pre>\nCheck to see if the service is running:<\/p>\n
# systemctl status mongod<\/pre>\nThe output should look something like this:<\/p>\n
\u25cf mongodb.service - High-performance, schema-free document-oriented database\r\n Loaded: loaded (\/lib\/systemd\/system\/mongod.service; enabled; vendor preset: enabled)\r\n Active: active (running) since Thu 2017-06-29 07:13:54 CDT; 8s ago\r\n Docs: https:\/\/docs.mongodb.org\/manual\r\n Main PID: 4734 (mongod)\r\n CGroup: \/system.slice\/mongodb.service\r\n \u2514\u25004734 \/usr\/bin\/mongod --quiet --config \/etc\/mongod.conf\r\n\r\nJun 29 07:13:54 test systemd[1]: Started High-performance, schema-free document-oriented database.\r\n<\/pre>\n