mongo - ruby connection problem
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
.
Related videos on Youtube
![Chani](https://i.stack.imgur.com/grrEJ.jpg?s=256&g=1)
Comments
-
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 over 13 yearsIs your mongo server actually running? What happens when you do a
telnet localhost 27017
? -
ricard.robin over 13 yearsIf mongo isn't running, just do in an another terminal:
mongod
-
-
Chani over 13 yearsthere is no mongodb in my init.d.
-
Dylan Markow over 13 yearsDo 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 almost 13 yearssudo invoke-rc.d mongodb restart on any debian system
-
vise over 12 yearsI had a similar problem after an unclean shutdown and removing the lock solved it. Thanks!
-
Tass about 12 yearsThanks, @Globalkeith! Had just run an update on ubuntu and restarted. Naughty ubuntu.
-
ck3g almost 12 yearsYou helped me again after long time. Thanks
-
tokhi over 9 yearsI think its
mongod
notmongodb