Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB on Ubuntu won't start as a service, nothing in the log

Am running MongoDB 2.2 on Ubuntu and if I run:

sudo mongod

I get an error that it can't find /data/db, which is not where the database is. In mongod.conf the database path is specified as the Ubuntu 10gen default /var/lib/mongodb which is where the db is located. Seems like mongod is not finding the conf file. So when I run:

sudo mongod -f /etc/mongodb.conf

The server starts up fine and output is logged to the log file: /var/log/mongodb/mongodb.log. All is happy. I can switch to another shell, log into mongo shell, see the databases and run queries.

So, I cancel out of that and try to run as a service:

> sudo status mongodb
mongodb stop/waiting
> sudo start mongodb
mongodb start/running, process 10468

Looks good so far, but the mongo server did not start. Running another:

> sudo status mongodb
mongodb stop/waiting
> mongo
MongoDB shell version: 2.2.0
connecting to: test
Sat Sep  1 19:07:43 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed

"test" is not the correct database, and nothing appears in the log file.

I am at a loss as to what could be wrong. I checked the upstart scripts and they seem fine. /etc/init/mongodb.conf runs:

mongodb --exec  /usr/bin/mongod -- --config /etc/mongodb.conf
like image 982
BishopZ Avatar asked Sep 01 '12 23:09

BishopZ


People also ask

Can not start MongoDB service?

lock and storage. bson under the folder "D:\mongodb\data". Then type net start MongoDB in the cmd using administrator privilege, the issue will be solved.

How do I know if MongoDB is running as a service?

To verify the status of the service, type: sudo systemctl status mongodb.


4 Answers

OK, this all comes down to permissions, but let's take it step by step. 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.

like image 158
Adam Comerford Avatar answered Oct 07 '22 20:10

Adam Comerford


First confirm that the mongodb user/group has permission to write to both the data directory and log file:

$ sudo chown -R mongodb:mongodb /var/lib/mongodb/.

$ sudo chown -R mongodb:mongodb /var/log/mongodb.log

Start up MongoDB as a Daemon (background process) using the following command:

$ mongod --fork --dbpath /var/lib/mongodb/ --smallfiles --logpath /var/log/mongodb.log --logappend

To Shut Down MongoDB enter the Mongo CLI, access the admin and issue the shutdown command:

$ ./mongo

> use admin

> db.shutdownServer()

Ref: http://www.mongodb.org/display/DOCS/Starting+and+Stopping+Mongo

like image 30
nelsonic Avatar answered Oct 07 '22 20:10

nelsonic


I too had the same problem. So I went to cd /var/lib/mongodb/ and deleted the mongod.lock file Then it worked for me.

like image 9
Damodaran Avatar answered Oct 07 '22 19:10

Damodaran


After checking all permission in the data, journal and log folders as suggested by @nelsonic, my problem was solved by giving permission to lock file in the /tmp folder

sudo chown mongod:mongod mongodb-27017.sock  

I was running it as a AWS Amazon Linux instance. I figured that out by executing as the mongod user as below, and then, researching the error code. It might be useful for other troubleshooting.

sudo -S -u mongod mongod -f /etc/mongod.conf
like image 3
Pedro Israel Avatar answered Oct 07 '22 21:10

Pedro Israel