mongod won't run without me specifying my own conf path

I recently upgraded my apt-get and now mongod isn't behaving correctly.

I currently have to run via: mongod --dbpath /var/lib/mongodb

and leave the terminal open for it to work.

If I just run mongod

root@ip-XXX-XX-XX-XX:/home/ubuntu# mongod
mongod --help for help and startup options
Sun Jun 22 21:48:19.403 [initandlisten] MongoDB starting : pid=2033 port=27017 dbpath=/data/db/ 64-bit host=ip-XXX-XX-XX-XX
Sun Jun 22 21:48:19.403 [initandlisten] db version v2.4.10
Sun Jun 22 21:48:19.403 [initandlisten] git version: e3d78955d181e475345ebd60053a4738a4c5268a
Sun Jun 22 21:48:19.403 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Sun Jun 22 21:48:19.403 [initandlisten] allocator: tcmalloc
Sun Jun 22 21:48:19.403 [initandlisten] options: {}
Sun Jun 22 21:48:19.404 [initandlisten] exception in initAndListen: 10296
*********************************************************************
 ERROR: dbpath (/data/db/) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating
Sun Jun 22 21:48:19.404 dbexit:
Sun Jun 22 21:48:19.404 [initandlisten] shutdown: going to close listening sockets...
Sun Jun 22 21:48:19.404 [initandlisten] shutdown: going to flush diaglog...
Sun Jun 22 21:48:19.404 [initandlisten] shutdown: going to close sockets...
Sun Jun 22 21:48:19.404 [initandlisten] shutdown: waiting for fs preallocator...
Sun Jun 22 21:48:19.404 [initandlisten] shutdown: lock for final commit...
Sun Jun 22 21:48:19.404 [initandlisten] shutdown: final commit...
Sun Jun 22 21:48:19.404 [initandlisten] shutdown: closing all files...
Sun Jun 22 21:48:19.405 [initandlisten] closeAllFiles() finished
Sun Jun 22 21:48:19.405 dbexit: really exiting now
root@ip-XXX-XX-XX-XX:/home/ubuntu#

Which I presume is using a different config file, but I have no idea what config file its using. I would like mongo to run automatically and using the right config.

Also worthy of note, If I do:

sudo service mongodb start

I get: mongodb start/running, process 2044

but any page using mongo errors with:

Failed to connect to: 127.0.0.1:27017: Connection refused.

Solution 1:

The error when you run just mongod is because the default database path is /data/db (i.e. it's not another config file, just what you get if you don't specify anything). You appear to be running mongod as root, which is a bad idea in general, and likely the source of your issues. If you start the service, it will run as a user (usually mongod or mongodb) and so if there are files in the dbpath now owned by the root user (and similarly your log files may now be owned by root), then it will fail to start with permissions errors.

You likely have to fix your permissions, as outlined in this answer to fix this, and generally should avoid running mongod as root in the future.

Solution 2:

Make sure that your dbpath directory exists and accessible by mongodb. This worked for me:

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

Replace /var/lib/mongodb with your dbpath directory.