Martin Drozdik
Martin Drozdik

Reputation: 13343

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

The official MongoDB documentation says:

Issue the following command to start mongod:

sudo service mongod start

However, the reputable 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 runs ok, and the second one runs after I create the /data/db directory and change its permissions or run with sudo mongod.

I was wondering, when should you use which manner? Aside from the minor difference that the second way grabs the terminal.

sudo service mongod start

vs

mongod

I tried to look up what the service command does, but the documentation is too advanced for me.

Upvotes: 3

Views: 1291

Answers (1)

helmy
helmy

Reputation: 9517

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

Here are some of the reasons:

  • Proper installation as a service ensures that the if/when the server is restarted that the mongod process is automatically restarted
  • The mongod service configuration will typically ensure that some of the recommended OS level settings are applied to the mongod process. What gets set will vary based on the OS and version but this will include things like proper ulimit settings
  • Running as a service means that you're always using a config file rather than passing command line parameters.
  • Consistency, standardization, ease of use in terms of where things are installed, how mongod is started, how it is upgraded, etc.

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

Upvotes: 5

Related Questions