I am using ubuntu and can run mongodb server by the following command

systemctl start mongodb

But, if i want to run Mongodb by mongod

mongod --dbpath /var/lib/mongodb

it gives me Permission Denied error. Adding sudo before the mongod command fix the error and the Server starts. But now, earlier command

systemctl start mongodb

won't work and says too many files open

So, I change the owner of the folder /var/log/mongodb and /var/lib/mongodb by the following commands.

sudo chown -R  mongodb:mongodb /var/log/mongodb
sudo chown -R  mongodb:mongodb /var/lib/mongodb

Now, MongoDb server started by systemctl command but again mongod command gives me error. So, Is there any way to run Mongodb Server by both ways i.e, by using systemctl and mongod command.



The following command has solved my problem

sudo -u mongodb mongod --dbpath /var/lib/mongodb

The -u mongodb after sudo runs the mongod with mongodb user. So,I don't have to change owner of folder /var/lib/mongodb to mongodb.

Now, I am able to start Mongodb Server as Service and also by mongod.

  • 1
    To avoid potential problems like permission issues it would be best to start and stop MongoDB consistently. I recommend always using the service definition. If you really need to start manually (for example, to temporarily add an extra parameter without editing the config) it would be better to use the same config file eg: sudo -u mongodb mongod --config /etc/mongod.conf. – Stennie Feb 22 at 5:24
  • Actually, I don't need so often to run it manually but sometimes i need to use bindip to let access database if i connected to network. But my problem is that there is an error cannot assign requested header, if i am not connected to network. then, I need that – Mohammad Faisal Feb 22 at 5:30
  • I don't want to edit /etc/mongod.conf everytime. – Mohammad Faisal Feb 22 at 5:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.