views:

182

answers:

1

Hi all:

This is just for my weekend project/study, I am very new to Sinatra and MongoDB.

I've installed the gems for mongoDB, such as: mongo, mongo_mapper and mongoid.

When I tried connecting to my database on MongoHQ from localhost, it encountered such an error:

Mongo::ConnectionFailure at /
failed to connect to any given host:port

    * file: connection.rb
    * location: connect
    * line: 489

I found a similar thread on SO, but frankly speaking, I don't quite understand the answers...

Here is my code snippet:

require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongo_mapper'

get '/' do
  MongoMapper.connection = Mongo::Connection.new('flame.mongohq.com', 27044)
  MongoMapper.database = 'notes'
  MongoMapper.database.authenticate('foo', 'bar')
  erb :list
end

I took the above code from here, but it seems not working...

Which part is wrong? Is there another way to do this? In the end this test web app will be deployed onto heroku, so I hope the solution can work with both localhost and my heroku server.

Updated:

I just created a minimal code snippet to test the mongodb connection:
require 'rubygems'
require 'mongo'

db = Mongo::Connection.new("flame.mongohq.com", 27044).db("notes")

But still got the error, after timeout:

$ ruby mongodbtest.rb 
/Library/Ruby/Gems/1.8/gems/mongo-1.0.8/lib/../lib/mongo/connection.rb:489:in
`connect': failed to connect to any given host:port (Mongo::ConnectionFailure)
from /Library/Ruby/Gems/1.8/gems/mongo-1.0.8/lib/../lib/mongo/connection.rb:137:in
`initialize'
from mongodbtest.rb:4:in `new'
from mongodbtest.rb:4

The hostname and port are according to mongoHQ documentation, so they must be right.

Thanks for the help in advance.

2nd Update:

I just tested the mongodb connection string using terminal:

mongo mongodb://flame.mongohq.com:27044/notes -u foo -p bar

Unfortunately this would get me an connection failed error, honestly, I don't know why...

A: 

Hi all:

Just gave this another try, this time, I was using the ip address taken from ping:

So, if I change :

db = Mongo::Connection.new('flame.mongohq.com', 27060).db("notes")
db.authenticate('fake', 'info')

To:

db = Mongo::Connection.new('184.73.224.5', 27060).db("notes")
db.authenticate('fake', 'info')

That will work...

I still don't understand why the domain name approach won't work, but at least I can finish this off :)

Michael Mao