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!
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With