Q:

Error running MongoDb on MacOSX

I am trying to run mongo with rails i get the following Error
Harshas-MacBook-Pro:~ harshamv$ mongo
MongoDB shell version: 2.6.1
connecting to: test
2014-06-14T12:07:46.356+0530 warning: Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2014-06-14T12:07:46.357+0530 Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

When i try to repair MongoDB
Harshas-MacBook-Pro:nomad harshamv$ mongod --repair
2014-06-14T11:06:52.964+0530 [initandlisten] MongoDB starting : pid=5504 port=27017 dbpath=/data/db 64-bit host=Harshas-MacBook-Pro.local
2014-06-14T11:06:52.964+0530 [initandlisten] 
2014-06-14T11:06:52.964+0530 [initandlisten] ** WARNING: soft rlimits too low.  Number of files is 256, should be at least 1000
2014-06-14T11:06:52.964+0530 [initandlisten] db version v2.6.1
2014-06-14T11:06:52.964+0530 [initandlisten] git version: nogitversion
2014-06-14T11:06:52.964+0530 [initandlisten] build info: Darwin minimavericks.local 13.1.0 Darwin Kernel Version 13.1.0: Wed Apr  2 23:52:02 PDT 2014; root:xnu-2422.92.1~2/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2014-06-14T11:06:52.964+0530 [initandlisten] allocator: tcmalloc
2014-06-14T11:06:52.964+0530 [initandlisten] options: { repair: true }
2014-06-14T11:06:52.964+0530 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?,  terminating
2014-06-14T11:06:52.964+0530 [initandlisten] dbexit: 
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: going to close listening sockets...
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: going to flush diaglog...
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: going to close sockets...
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: waiting for fs preallocator...
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: closing all files...
2014-06-14T11:06:52.964+0530 [initandlisten] closeAllFiles() finished
2014-06-14T11:06:52.964+0530 [initandlisten] shutdown: removing fs lock...
2014-06-14T11:06:52.964+0530 [initandlisten] couldn't remove fs lock errno:9 Bad file descriptor
2014-06-14T11:06:52.964+0530 [initandlisten] dbexit: really exiting now

This is the Error i get when i am trying to run my Rails app
Moped::Errors::ConnectionFailure in VenuesController#index
Could not connect to a primary node for replica set #<Moped::Cluster:70100620147140 @seeds=[<Moped::Node resolved_address="127.0.0.1:27017">]>

A:

Error Unable to create/open lock file can be caused by these three things:

MongoDB process is running and is un-responsive
Your previous MongoDB process didn't shutdown cleanly.  
You don't have write permissions on that folder / file. 

Case 1:

You need to check if mongod process is active.  In your terminal console, enter: 
ps aux | grep mongod

If you can see a process you can kill it with:
kill $(pidof mongod) 
or kill -2 $(pidof mongod)
Use -9 option only as a last resort. 
You will also need to remove the old mongod.lock file and then start mongod. 

Case 2:

If there wasn't an active process, then MongoDB didn't shut down cleanly.  
You just need to remove mongod.lock file and then then start mongod.  

Case 3:

If you removed the mongod.lock file and you're getting the same error, you should check the permissions on your dbpath folder (/data/db/).  This can happen if you started mongod with sudo.  
Your user or mongod should be the owner of the folder.  You can change it with:
chown -R $(id -u) /data/db

