Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo - ruby connection problem

Tags:

ruby

mongodb

I have installed mongo and bson_ext now I have created a .rb file with the following contents ::

require 'rubygems'
require 'mongo'

db = Mongo::Connection.new.db("mydb")
db = Mongo::Connection.new("localhost").db("mydb")
db = Mongo::Connection.new("localhost", 27017).db("mydb")

However I am getting following error on running the code

yuzaki@ubuntu:~$ ruby firstruby.rb
/home/ryuzaki/.rvm/gems/ruby-1.9.2-p136/gems/mongo-1.2.0/lib/mongo/connection.rb:451:in `connect': Failed to connect to a master node at localhost:27017 (Mongo::ConnectionFailure)
    from /home/ryuzaki/.rvm/gems/ruby-1.9.2-p136/gems/mongo-1.2.0/lib/mongo/connection.rb:554:in `setup'
    from /home/ryuzaki/.rvm/gems/ruby-1.9.2-p136/gems/mongo-1.2.0/lib/mongo/connection.rb:98:in `initialize'
    from firstruby.rb:4:in `new'
    from firstruby.rb:4:in `<main>'

Please help!

like image 734
Chani Avatar asked Jan 29 '11 14:01

Chani


2 Answers

had this happen several times now, and here is the solution that works for me:

sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo start mongodb
sudo status mongodb
like image 122
ktec Avatar answered Sep 23 '22 23:09

ktec


This is definitely due to your mongo server not running. Since you're on Ubuntu, try doing a sudo /etc/init.d/mongodb start and then see if your code works.

like image 20
Dylan Markow Avatar answered Sep 25 '22 23:09

Dylan Markow