1

I have installed Mongodb 3.0 using this tutorial -

https://docs.mongodb.com/v3.0/tutorial/install-mongodb-on-amazon/

It has installed fine. I have also given permissions to 'ec2-user' to all the data and log folders ie var/lib/mongo and var/log/mongodb but and have set conf file as well.

Now thing is that mongodb server always fails to start with command

sudo service mongod start

it just say failed, nothing else.

While if I run command -

mongod --dbpath var/lib/mongo

it starts the mongodb server correctly (though I have mentioned same dbpath in .conf file as well)

What is it I am doing wrong here?

Darpan
  • 5,623
  • 3
  • 48
  • 80

1 Answers1

1

When you run sudo mongod it does not load a config file at all, it literally starts with the compiled in defaults - port 27017, database path of /data/db etc. - that is why you got the error about not being able to find that folder. The "Ubuntu default" is only used when you point it at the config file (if you start using the service command, this is done for you behind the scenes).

Next you ran it like this:

sudo mongod -f /etc/mongodb.conf

If there weren't problems before, then there will be now - you have run the process, with your normal config (pointing at your usual dbpath and log) as the root user. That means that there are going to now be a number of files in that normal MongoDB folder with the user:group of root:root.

This will cause errors when you try to start it as a normal service again, because the mongodb user (which the service will attempt to run as) will not have permission to access those root:root files, and most notably, it will probably not be able to write to the log file to give you any information.

Therefore, to run it as a normal service, we need to fix those permissions. First, make sure MongoDB is not currently running as root, then:

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

That should fix it up (assuming the user:group is mongodb:mongodb), though it's probably best to verify with an ls -al or similar to be sure. Once this is done you should be able to get the service to start successfully again.

If you’re starting mongod as a service using:

sudo service mongod start

Make sure the directories defined for logpath, dbpath, and pidfilepath in your mongod.conf exist and are owned by mongod:mongod.

Piyush Patil
  • 14,512
  • 6
  • 35
  • 54
  • Well, I am reading the same answer http://stackoverflow.com/a/12232668/609782 What I didn't understand is, my service name is `mongod` and I am logged in as `ec2-user`, so while doing `chown` what should be my `user:group`? it is definitely not mongodb:mongodb, is it? – Darpan Jul 25 '16 at 14:28
  • Do ls -al and check what is the user group and change the permission accordingly. – Piyush Patil Jul 25 '16 at 15:11
  • need to call `ls -al` on which folder? (or it doesn't matter?) as when I did that on root directory, i got a list with different user:groups – Darpan Jul 25 '16 at 15:59
  • /etc/mongodb folder – Piyush Patil Jul 25 '16 at 16:03