What are the differences between running mongod as a service and starting it as a regular executable?

MongoDB official documentation says:

Run the following command to start mongod:

sudo service mongod start

However the respected MongoDB: The Definitive Guide, 2nd Edition says on page 11 that:

To start the server, run the mongod executable:

$ mongod

On my Ubuntu 16.04 system, the first one works fine and the second one starts up after creating the directory /data/db

and changing its permissions or executing with sudo mongod

.

I was wondering when should you use this way? Apart from the minor difference, the second method hijacks the terminal.

sudo service mongod start

      

against

mongod

      

I tried to see what the command is doing service

, but the documentation is too advanced for me.

+3


source to share


1 answer


There are quite a few advantages to running MongoDB as a service, or just starting a mongod process from the command line. I usually prefer to run mongod from the command line when I run locally on my laptop, but for any deployment to a real server in dev, qa, production, etc. I always recommend running it as a service.

Some of the reasons are:



  • Setting it right as a service will ensure that if / when the server reboots, the mongod process will automatically reboot.
  • The mongod service configuration usually ensures that some of the recommended OS level parameters are applied to the mongod process. What gets installed will depend on OS and version, but this will include things like correct ulimit settings.
  • Running a service means that you always use a config file and don't pass command line parameters.
  • Consistency, standardization, ease of use in terms of where things are installed, how mongod starts, how it gets updated, etc.

It's also worth noting that you can still run mongod from the command line and use a config file, and if you give the fork option it won't hijack your terminal.

+3


source







All Articles