Failed to connect to mongodb in ubuntu?

Problem

I followed this tutorial: http://howtonode.org/express-mongodb

Its strange, the first time I followed the tutorial everything worked. But now, one day after I wanted to play with the code again I coudn't start the server. This is what I get:

alex@alex-System-Product-Name:~/blog2$ node app.js Express server listening on port 3000 in development mode

node.js:134 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: failed to connect to [localhost:27017] at [object Object]. (/home/alex/blog2/node_modules/mongodb/lib/mongodb/connection/server.js:189:47) at [object Object].emit (events.js:67:17) at [object Object]. (/home/alex/blog2/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:110:14) at [object Object].emit (events.js:67:17) at Socket. (/home/alex/blog2/node_modules/mongodb/lib/mongodb/connection/connection.js:301:10) at Socket.emit (events.js:64:17) at Array. (net.js:836:27) at EventEmitter._tickCallback (node.js:126:26) alex@alex-System-Product-Name:~/blog2$

Any suggestions to fix this problem?

EDIT:

I just did:

alex@alex-System-Product-Name:~/blog2$ sudo service mongodb start
mongodb start/running, process 9847
alex@alex-System-Product-Name:~/blog2$ sudo service mongodb status
mongodb stop/waiting
alex@alex-System-Product-Name:~/blog2$ 

Not sure if it is running.

and this is what I get if I type $ mongo in the command line:

alex@alex-System-Product-Name:~/blog2$ mongo
MongoDB shell version: 1.8.2
Tue Nov 15 08:09:50 *** warning: spider monkey build without utf8 support.  consider rebuilding with utf8 support
connecting to: test
Tue Nov 15 08:09:50 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:79
exception: connect failed
Problem courtesy of: alexchenco

Solution

This fixed the problem:

http://www.synchrosinteractive.com/blog/1-software/47-mongodb-repair-on-ubuntu

Apparently mongodb creates a lock file when the database crashes or something. Deleting that file and repairing mongodb fixes the problem:

Manually remove the lockfile: sudo rm /var/lib/mongodb/mongod.lock

Run the repair script: sudo -u mongodb mongod -f /etc/mongodb.conf --repair

Solution courtesy of: alexchenco

Discussion

The error seems to indicate that MongoDB is not running.

When running MongoDB unattended, you provide the mongod process with a --logpath parameter which points to a file. Check that file to see the reason mongod is failing.

Discussion courtesy of: Gates VP

This recipe can be found in it's original form on Stack Over Flow.