当前位置:  开发笔记 > 数据库 > 正文

heroku mongohq和mongoid Mongo :: ConnectionFailure

如何解决《herokumongohq和mongoidMongo::ConnectionFailure》经验,为你挑选了2个好方法。

更新2012年6月9日:

在heroku上使用mongoid 3.0.0.rc进行设置,请参阅此要点:https://gist.github.com/2900804

2011年1月22日更新:

Uri现在优先于mongoid.yml

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

更新2010年8月12日:虽然我得到了Jackues Crocker的第6个可接受的答案,但是这个问题的某些方面让人很容易搞砸!它再次发生在我身上,我决定研究mongoid源代码.所以,在这里:

目前,主机:端口:名称/数据库:设置超过uri:设置.因此,由于对localhost:xxxx的请求而不是对flame.local.mongohq.com:xxxx的请求,发生了非常无法提供信息的错误消息

这会打破!

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'] %>

修复它,删除主机:默认情况下,和/或删除<<:*默认值


原文问:

我已经在heroku为mongodb添加了mongoHQ插件.它崩溃了:

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

正如我所看到的,在线描述(heroku mongohq)更像是针对mongomapper.我正在运行ruby 1.9.1并使用mongoid导轨3-beta .

我的感觉说有一些东西ENV['MONGOHQ_URL'],它说MongoHQ插件集,但我没有MONGOHQ_URL在我的应用程序中设置任何地方.我想问题出在我的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'] %>

它本地工作正常,但在heroku失败,堆栈跟踪更多:

==> 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 '
    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 '
    from /disk1/home/slugs/176479_b14df52_b875/mnt/.bundle/gems/gems/railties-3.0.0.beta3/lib/rails/initializable.rb:25:in `instance_exec'
.....

它都在本地工作,包括测试和应用程序.我没有想法......有什么建议吗?

PS:有高兴趣的人会创建标签'mongohq'吗?



1> 小智..:

Mongoid(master)现在在mongoid.yml中有一个URI选项.所以你可以这样做:

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

要在项目中使用mongoid master,请在Gemfile中设置它

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

希望很快就能发布新的宝石,这样可以清理干净的东西.



2> 小智..:

在我看来,在defaults哈希中指定host会覆盖uri中的值.要修复它,只需从默认设置中删除主机,这是我的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'] %>

这是来自mongoid的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

推荐阅读
拾味湖
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有