4

I've been trying to use Mongoid with Rails on Ubuntu. Mongodb was working fine for me with rails app 3.0 to 3.2.

Today suddenly, I am getting an error:

/home/pravinmishra/.rvm/gems/ruby-1.9.2-p318/gems/mongo-1.5.2/lib/mongo/connection.rb:413:in `connect': Failed to connect to a master node at localhost:27017 (Mongo::ConnectionFailure)

Many times I faced this issue, and the commands below worked for me:

sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo start mongodb
sudo status mongodb

When I'm connecting from Ubuntu terminal using mongo then I get this error:

MongoDB shell version: 2.0.1 connecting to: test Mon Jul 9 22:47:06 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84 exception: connect failed

Any suggestion would be appreciated.

techraf
  • 4,243
  • 8
  • 29
  • 44
Pravin Mishra
  • 43
  • 1
  • 6

3 Answers3

2

Maybe your mongodb instance quited unexpectedly and you have stale locks and the database in inconsistent state.

As you already guessed start mongoDB with the --repair flag, but after that you need to restart it again without it.

Also make sure that all the processes are really terminated or you won't be able to bind the server on the same port. I would recommend to execute netstat -tulnp and double check if there is nothing listening on the 27017 port.

golja
  • 1,621
  • 10
  • 14
1

Based on your comment, about the old lock file error, you either have a mongod already running and you are trying to connect to the wrong port, or you have a case of an unclean shutdown of a previously running mongod process and you need to remove the lock file and restart (possibly with repair depending on the nature of the unclean shutdown).

If this happens again, definitely make sure you are running with journaling enabled (default in 2.0), then repair should not be necessary.

Adam C
  • 5,222
  • 2
  • 30
  • 52
0

You may not be able to connect to the default localhost:27017 as the previous instance must have terminated incorrectly due to errors or exceptions or locks. Please try killing the previous processes and then running it again.