Ruby Sinatra - failed to connect to mongoDB on mongoHQ

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

I installed gems for mongoDB, for example: mongo, mongo_mapper and mongoid.

When I tried to connect to my MongoHQ database from the local host, it encountered this 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, 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 like it is not working ...

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

:

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")

:

$ 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

mongoHQ, .

.

:

mongodb :

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

, , , , ...

+5
2

uri =  URI.parse(ENV['MONGOHQ_URL'])
@mongo_connection = Mongo::Connection.from_uri( uri )
@mongo_db = @mongo_connection.db(uri.path.gsub(/^\//, ''))
@mongo_db.authenticate(uri.user, uri.password)

URL- heroku config --long

+8

, ip-, ping:

, :

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')

...

, , :)

0

All Articles