Search code examples
linuxmongodbserviceconfigurationstartup

How to start as a service on Linux with configuration file


I downloaded MongoDB from the website: https://www.mongodb.org/dl/linux/x86_64-rhel70

The version I downloaded: linux/mongodb-linux-x86_64-rhel70-3.6.9.tgz

I extracted the downloaded MongoDB package on my CentOS 7 machine and I am able to start Mongod using the binary.

I would like to start MongoDB as a service with a configuration file.

I know the command for the Windows side of things which is:

"C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\3.2\mongod.cfg" –install

However, I do not know how to accomplish this on a Linux box?


Solution

  • If MongoDB doesn't install a service, you can make your own:

    1) Create a file in /etc/systemd/system/ called whatever you like, with .service as the file extension, like so MongoDB.service

    2) Write the following into that file:

    [Unit]
    Description=MongoDB Service
    
    [Service]
    ExecStart=<Path to binary along with arguments>
    
    [Install]
    WantedBy=multi-user.target
    

    3) To start the serivce call sudo systemctl start <your filename>.service