Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

UPDATE 9th june 2012:

Setup with mongoid 3.0.0.rc at heroku, see this gist: https://gist.github.com/2900804

UPDATE 22th jan 2011:

Uri now takes precedence in mongoid.yml

https://github.com/mongoid/mongoid/issues/issue/266

UPDATE 12th aug 2010: Although I got an accepted answer 6th may from Jackues Crocker, there are aspects of this issue that makes it easy to mess up! It happened to me yet again and I decided to research the mongoid source code. So, here it goes:

Currently, host: port: name/database: settings TAKE PRECEDENCE OVER the uri: setting. Hence, the awfully uninformative error message is happening due to a request to localhost:xxxx and not to flame.local.mongohq.com:xxxx

This will break!

defaults: &defaults
  host: localhost  <- THIS 'OVERWRITES' host in the uri!

production:
  <<: *defaults    <- BE CAREFUL WITH WHAT YOU BRING IN. THE host: FROM DEFAULTS WILL BE THE ONE APPLIED, not your uri host.
  uri: <%= ENV['MONGOHQ_URL'] %>

fix it with either removing the host: in defaults, and/or removing the <<: *defaults


ORIGINAL Q:

I have added the mongoHQ addon for mongodb at heroku. It crashes with :

connect_to_master': failed to connect to any given host:port (Mongo::ConnectionFailure)

The descriptions online (heroku mongohq) are more directed towards mongomapper, as I see it. I'm running ruby 1.9.1 and rails 3-beta with mongoid.

My feeling says that there's something with ENV['MONGOHQ_URL'], which it says the MongoHQ addon sets, but I haven't set MONGOHQ_URL anywhere in my app. I guess the problem is in my mongoid.yml ?

defaults: &defaults
  host: localhost

development:
  <<: *defaults
  database: aliado_development

test:
  <<: *defaults
  database: aliado_test

# set these environment variables on your prod server
production:
  <<: *defaults
  host: <%= ENV['MONGOID_HOST'] %>
  port: <%= ENV['MONGOID_PORT'] %>
  username: <%= ENV['MONGOID_USERNAME'] %>
  password: <%= ENV['MONGOID_PASSWORD'] %>
  database: <%= ENV['MONGOID_DATABASE'] %>

It works fine locally, but fails at heroku, more stack trace:

==> crashlog.log <==
Cannot write to outdated .bundle/environment.rb to update it
/disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/rack-1.1.0/lib/rack.rb:14: warning: already initialized constant VERSION
/disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/mongo-0.20.1/lib/mongo/connection.rb:435:in `connect_to_master': failed to connect to any given host:port (Mongo::ConnectionFailure)
    from /disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/mongo-0.20.1/lib/mongo/connection.rb:112:in `initialize'
    from /disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/mongoid-2.0.0.beta4
/lib/mongoid/railtie.rb:32:in `new'
    from /disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/mongoid-2.0.0.beta4/lib/mongoid/railtie.rb:32:in `block (2 levels) in <class:Railtie>'
    from /disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/mongoid-2.0.0.beta4/lib/mongoid.rb:110:in `configure'
    from /disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/mongoid-2.0.0.beta4/lib/mongoid/railtie.rb:21:in `block in <class:Railtie>'
    from /disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/railties-3.0.0.beta3/lib/rails/initializable.rb:25:in `instance_exec'
.....

It all works locally, both tests and app. I'm out of ideas... Any suggestions?

PS: Somebody with high repu mind create the tag 'mongohq'?

share|improve this question
add comment

4 Answers

up vote 25 down vote accepted

Mongoid (master) now has a URI option in mongoid.yml. So you could do:

production:
  uri: <%= ENV['MONGOHQ_URL'] %>

To use mongoid master in your project, set this in your Gemfile

gem "mongoid", :git => "git@github.com:mongoid/mongoid.git"

Hopefully a new gem will be released soon which will clean things up.

share|improve this answer
add comment

It seems to me that specifying host in the defaults hash overrides the value in uri. To fix it just remove the host from the default, here is my config/mongo.yml:

defaults: &defaults
  allow_dynamic_fields: true
  parameterize_keys: true
  persist_in_safe_mode: true
  raise_not_found_error: true
  reconnect_time: 3
  use_object_ids: true

production:
  <<: *defaults
  uri: <%= ENV['MONGOHQ_URL'] %>

Here is the snippet from mongoid's config.rb:

  mongo_uri = settings["uri"].present? ? URI.parse(settings["uri"]) : OpenStruct.new

  name = settings["database"] || mongo_uri.path.to_s.sub("/", "")
  host = settings["host"] || mongo_uri.host || "localhost" # <= look here
  port = settings["port"] || mongo_uri.port || 27017
share|improve this answer
add comment

We have some mongoid docs on our heroku section of our docs. They haven't been released officially yet but you can get it to it already. Don't expect much in the way of styles and content yet, but it does have some info that you might find useful for mongoid.

https://devcenter.heroku.com/articles/mongohq

share|improve this answer
add comment

Just a note that this works for me without any issues, as advertised on http://mongoid.github.com/docs/installation/

Gemfile:

gem "rails", '3.0.0.beta3'
gem "mongoid", "2.0.0.beta4"
gem "bson_ext", "0.20.1"

mongoid.yml:

host: xxx.mongohq.com
port: xxx
database: db
username: user
password: xxx
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["        <li><a href=\"http://area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"http://careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2013 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc-wiki</a> \r\n with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"footer-flair\">\r\n                <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" class=\"cc-wiki-link\"></a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2013.12.13.1225\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overfl", "ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2013 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc-wiki</a> \r\n with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"footer-flair\">\r\n                <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" class=\"cc-wiki-link\"></a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2013.12.13.1225\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </noscript>\r\n<script type=\"text/javascript\">var p = \"http\", d = \"static\"; if (document.location.protocol == \"https:\") { p += \"s\"; d = \"engine\"; } va"], "copyright__copyright__copyright": ["r me without any issues, as advertised on http://mongoid.github.com/docs/installation/\n\nGemfile:\n\ngem \"rails\", '3.0.0.beta3'\ngem \"mongoid\", \"2.0.0.beta4\"\ngem \"bson_ext\", \"0.20.1\"\n\nmongoid.yml:\n\nhost: xxx.mongohq.com\nport: xxx\ndatabase: db\nusername: user\npassword: xxx\nshare|improve this answer\nadd comment\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}