59

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
BishopZ
  • 6,269
  • 8
  • 45
  • 58

10 Answers10

120

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.

Mike
  • 14,010
  • 29
  • 101
  • 161
Adam Comerford
  • 21,336
  • 4
  • 65
  • 85
  • 4
    as is true with any good answer, as soon as you say it, the whole thing becomes obvious. thanks! – BishopZ Sep 02 '12 at 01:51
  • Took me ages.. and this one works.. in case someone searches for this, this came up as "Failed to start SYSV: Mongo is a scalable," when doing the suggested "systemctl status mongod.service". – Andreas Reiff Jan 18 '15 at 17:01
  • 1
    @AndreasReiff I tried changing the permission to the folders you mentioned but I still cant start mongod in my ubuntu machine. – Vinay Mar 04 '16 at 04:03
  • `vinayak@vinayak-Lenovo-G500:~$ mongod 2016-03-04T09:31:29.942+0530 I CONTROL [initandlisten] MongoDB starting : pid=3211 port=27017 dbpath=/data/db 64-bit host=vinayak-Lenovo-G500 2016-03-04T09:31:29.942+0530 I CONTROL [initandlisten] db version v3.2.3 2016-03-04T09:31:29.942+0530 I CONTROL [initandlisten] git version: b326ba837cf6f49d65c2f85e1b70f6f31ece7937 2016-03-04T09:31:29.942+0530 I CONTROL [initandlisten] OpenSSL – Vinay Mar 04 '16 at 04:10
  • @Vinay I do not see your error message, just your console out. Still, if this is unrelated to the question and answer here (or has substantial changes) I would suggest starting a new topic. – Andreas Reiff Mar 07 '16 at 18:27
  • Happened to me too, but why do permissions disappear after an unsafe reboot? – Anuraag Vaidya Mar 04 '17 at 04:58
  • worked for me as well. Not sure how my droplet lost these permissions all of a sudden, as Mongodb was working fine then all a sudden POOF... i did these permissions and now it restarted fine. thank you – Joel Grannas Jan 04 '18 at 04:31
39

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

Community
  • 1
  • 1
nelsonic
  • 31,111
  • 21
  • 89
  • 120
9

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.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
Damodaran
  • 10,882
  • 10
  • 60
  • 81
4

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
Pedro Israel
  • 119
  • 5
4

Just try this command:

sudo chown mongodb /tmp/mongodb-27017.sock
J.Jai
  • 597
  • 1
  • 5
  • 9
2

Nothing worked for me, then I've found that it was a permissions problem on /tmp directory:

sudo chmod 1777 /tmp
sudo chown root:root /tmp
bekce
  • 3,782
  • 29
  • 30
  • There are quite a few permission issues that can lead to `mongod` not starting. This is definitely one of them. – frhd Jul 22 '16 at 09:01
1

None of the above answers worked for me. I finally figured it out by debugging the init script with:

sudo bash -x /etc/init.d/mongodb start

And seeing it was passing the wrong config path to mongod. I simply changed the line in /etc/init.d/mongodb from "CONF=/etc/mongodb.conf" to "CONF=/etc/mongod.conf". Version 2 uses the former, and installing version 3 added /etc/mongod.conf with the new format but apparently did not update the init script.

UPDATE: I now have a much stranger problem where the init script works, but only if I run it with "sudo bash -x /etc/init.d/mongodb start" and not with "sudo service mongodb start". Same thing for stop.

Abram
  • 413
  • 1
  • 3
  • 13
1

My mongodb was starting when launched from the command line as the mongod user, but not as a service with User=mongod. After an hour checking permissions, definition of the service, sockets... it was SElinux !

In /etc/selinux/config I switched from enforcing to permissive and reboot. It is now ok.

0

After none of the above answers worked for me, deleting my log file brought Mongo back to life.

Dave
  • 2,506
  • 2
  • 20
  • 27
0

These days this error can occur if you've updated mongod and you are running and old database. Mongod will be using the wiredTiger engine by default and you'll have a mmapv1 database

edit the engine setting in /etc/mongod.conf

# engine: wiredTiger
engine: mmapv1

Careful - YAML is whitespace sensitive

journalctl/systemd won't see this problem. Check the mongod log in /var/log/mongodb/mongod.log

I presume you can convert the database with something like the steps outlined here

https://docs.mongodb.com/manual/tutorial/change-standalone-wiredtiger/

Mark
  • 86
  • 3