Skip to content
Advertisement

Running mongod in shell works but it does not work as a service

I am running mongod under Linux OS. I wanted to change my data directory from the default /var/lib/mongodb to another location say /nfs/mongodb.

When I run mongodb from shell(i.e. sudo /usr/bin/mongod –dbpath /nfs/mongodb) It works just fine.

Next step, I tried to run mongodb as a service(sudo service mongodb start)

I modified the file /etc/mongodb.conf and changed the line dppath=xxx to point to the new directory I created. When I run mongodb as service I get this error:

couldn't open file /nfs/mongodb/journal/j._0 for writing errno:1 Operation not permitted, terminating

Why the mongodb works in the shell and not as a service?

Advertisement

Answer

This is very likely because of permission issue.

When you run sudo mongod, it runs as root and can write to any directory.

In contrast, when running as a service, MongoDB typically runs as a limited-privilege user to prevent any security issue to escalate to root level access.

The solution is to chmod or chown the intended dbpath directory so that it’s writeable by the service’s user.

Note: you may want to check out the Production Notes for tips on running MongoDB optimally.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement