title: heroku mongohq and mongoid Mongo::ConnectionFailure
body: <p>UPDATE 9th june 2012:</p>
  
  <p>Setup with mongoid 3.0.0.rc at heroku, see this gist: <a href="https://gist.github.com/2900804" rel="nofollow noreferrer">https://gist.github.com/2900804</a></p>
  
  <p>UPDATE 22th jan 2011: </p>
  
  <p>Uri now takes precedence in mongoid.yml </p>
  
  <p><a href="https://github.com/mongoid/mongoid/issues/issue/266" rel="nofollow noreferrer">https://github.com/mongoid/mongoid/issues/issue/266</a></p>
  
  <p>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: </p>
  
  <p>Currently, <strong><em>host: port: name/database:</em> settings TAKE PRECEDENCE OVER the <em>uri:</em> setting.</strong> Hence, the awfully uninformative error message is happening due to a request to <em>localhost:xxxx</em> and not to <em>flame.local.mongohq.com:xxxx</em></p>
  
  <p>This will break!</p>
  
  <pre><code>defaults: &amp;defaults
    host: localhost  &lt;- THIS 'OVERWRITES' host in the uri!
  
  production:
    &lt;&lt;: *defaults    &lt;- BE CAREFUL WITH WHAT YOU BRING IN. THE host: FROM DEFAULTS WILL BE THE ONE APPLIED, not your uri host.
    uri: &lt;%= ENV['MONGOHQ_URL'] %&gt;
  </code></pre>
  
  <p>fix it with either removing the <em>host:</em> in defaults, and/or removing the <em>&lt;&lt;: *defaults</em> </p>
  
  <hr>
  
  <p>ORIGINAL Q:</p>
  
  <p>I have added the <a href="http://blog.heroku.com/archives/2010/4/30/mongohq_add_on_public_beta/" rel="nofollow noreferrer">mongoHQ</a> addon for <a href="http://www.mongodb.org/display/DOCS/Rails+3+-+Getting+Started" rel="nofollow noreferrer">mongodb</a> at heroku. It crashes with :</p>
  
  <pre><code>connect_to_master': failed to connect to any given host:port (Mongo::ConnectionFailure)
  </code></pre>
  
  <p>The descriptions online (<a href="http://docs.heroku.com/mongohq" rel="nofollow noreferrer">heroku mongohq</a>) are more directed towards mongomapper, as I see it. I'm running ruby 1.9.1 and rails 3-beta with <a href="http://docs.heroku.com/mongohq" rel="nofollow noreferrer">mongoid</a>.</p>
  
  <p>My feeling says that there's something with <code>ENV['MONGOHQ_URL']</code>, which it says the MongoHQ addon sets, but I haven't set <code>MONGOHQ_URL</code> anywhere in my app. I guess the problem is in my 
  mongoid.yml ? </p>
  
  <pre><code>defaults: &amp;defaults
    host: localhost
  
  development:
    &lt;&lt;: *defaults
    database: aliado_development
  
  test:
    &lt;&lt;: *defaults
    database: aliado_test
  
  # set these environment variables on your prod server
  production:
    &lt;&lt;: *defaults
    host: &lt;%= ENV['MONGOID_HOST'] %&gt;
    port: &lt;%= ENV['MONGOID_PORT'] %&gt;
    username: &lt;%= ENV['MONGOID_USERNAME'] %&gt;
    password: &lt;%= ENV['MONGOID_PASSWORD'] %&gt;
    database: &lt;%= ENV['MONGOID_DATABASE'] %&gt;
  </code></pre>
  
  <p>It works fine locally, but fails at heroku, more stack trace:</p>
  
  <pre><code>==&gt; crashlog.log &lt;==
  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 &lt;class:Railtie&gt;'
      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 &lt;class:Railtie&gt;'
      from /disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/railties-3.0.0.beta3/lib/rails/initializable.rb:25:in `instance_exec'
  .....
  </code></pre>
  
  <p>It all works locally, both tests and app. I'm out of ideas... Any suggestions?</p>
  
  <p>PS: Somebody with high repu mind create the tag 'mongohq'?</p>