mongo - ruby connection problem

20,832

Solution 1

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.

Solution 2

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

Solution 3

I just encountered this due to my /etc/hosts file not containing an entry for "localhost" - consequently Ruby couldn't resolve "localhost". I suppose you can hardcode 127.0.0.1 into your code rather than "localhost" - or fix /etc/hosts to contain:

127.0.0.1 localhost

Solution 4

If you're on a Mac and used Brew, restarting the service solved it for me:

launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

You can find this info by running brew info mongodb.

Share:
20,832

Related videos on Youtube

Chani
Author by

Chani

Air of mystery

Updated on October 30, 2020

Comments

  • Chani
    Chani over 3 years

    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!

    • Dylan Markow
      Dylan Markow over 13 years
      Is your mongo server actually running? What happens when you do a telnet localhost 27017?
    • ricard.robin
      ricard.robin over 13 years
      If mongo isn't running, just do in an another terminal: mongod
  • Chani
    Chani over 13 years
    there is no mongodb in my init.d.
  • Dylan Markow
    Dylan Markow over 13 years
    Do you have mongo installed on your machine (I don't just mean the gem, I mean an actual mongo server). If not, you'll need to install it: sudo apt-get install mongodb.
  • puchu
    puchu almost 13 years
    sudo invoke-rc.d mongodb restart on any debian system
  • vise
    vise over 12 years
    I had a similar problem after an unclean shutdown and removing the lock solved it. Thanks!
  • Tass
    Tass about 12 years
    Thanks, @Globalkeith! Had just run an update on ubuntu and restarted. Naughty ubuntu.
  • ck3g
    ck3g almost 12 years
    You helped me again after long time. Thanks
  • tokhi
    tokhi over 9 years
    I think its mongod not mongodb