0

我正在尝试使用 MongoMapper 配置我的 Rails3 应用程序,以连接到通过我的 Heroku 帐户创建的 MongoHQ 数据库。现在,当我将代码推送到 Heroku 时,它会因 Mongo::AuthenticationError 而崩溃。日志中的堆栈跟踪如下。

TLDR /app/vendor/bundle/ruby/1.9.1/gems/mongo-1.9.2/lib/mongo/db.rb:175:in `issue_authentication': Failed to authenticate user 'heroku' on db 'app19340960' (Mongo::AuthenticationError)

这是完整的东西

Nov 11 22:55:03 qwikstubs heroku/web.1:  Starting process with command `bundle exec rails server -p 16121`
Nov 11 22:55:06 qwikstubs app/web.1:  => Booting Thin
Nov 11 22:55:06 qwikstubs app/web.1:  => Rails 3.2.11 application starting in production on http://0.0.0.0:16121
Nov 11 22:55:06 qwikstubs app/web.1:  => Call with -d to detach
Nov 11 22:55:06 qwikstubs app/web.1:  => Ctrl-C to shutdown server
Nov 11 22:55:07 qwikstubs app/web.1:  Exiting
Nov 11 22:55:07 qwikstubs app/web.1:  /app/vendor/bundle/ruby/1.9.1/gems/mongo-1.9.2/lib/mongo/db.rb:175:in `issue_authentication': Failed to authenticate user 'heroku' on db 'app19340960' (Mongo::AuthenticationError)
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/mongo-1.9.2/lib/mongo/db.rb:147:in `authenticate'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/mongo_mapper-0.12.0/lib/mongo_mapper/connection.rb:83:in `connect'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/mongo_mapper-0.12.0/lib/mongo_mapper/connection.rb:89:in `setup'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/mongo_mapper-0.12.0/lib/mongo_mapper/railtie.rb:28:in `block in <class:Railtie>'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/config/environment.rb:5:in `<top (required)>'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/config.ru:3:in `require'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/config.ru:3:in `block in <main>'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/config.ru:in `new'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/config.ru:in `<main>'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:40:in `eval'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:40:in `parse_file'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/server.rb:200:in `app'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands/server.rb:46:in `app'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/server.rb:304:in `wrapped_app'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/server.rb:254:in `start'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands/server.rb:70:in `start'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:55:in `block in <top (required)>'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:50:in `tap'
Nov 11 22:55:07 qwikstubs app/web.1:        from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/commands.rb:50:in `<top (required)>'
Nov 11 22:55:07 qwikstubs app/web.1:        from script/rails:6:in `require'
Nov 11 22:55:07 qwikstubs app/web.1:        from script/rails:6:in `<main>'
Nov 11 22:55:08 qwikstubs heroku/web.1:  Process exited with status 1

我认为我的问题与尝试创建 MongoMapper 连接的初始化文件有关。这是代码,也可以在这里找到

db_config = YAML::load(File.read(Rails.root.to_s + "/config/database.yml"))

if db_config[Rails.env] && db_config[Rails.env]['adapter'] == 'mongodb'
  mongo = db_config[Rails.env]
  MongoMapper.connection = Mongo::Connection.new(mongo['hostname'])
  MongoMapper.database = mongo['database']
end

我很确定在我定义新的 MongoMapper 连接的那一行有问题。它从中加载的 database.yml 文件在这里

defaults: &defaults
  host: 127.0.0.1
  port: 27017
  adapter: mongodb

development:
  <<: *defaults
  database: qwikstubs-dev

test: 
  <<: *defaults
  database: qwikstubs-test

production:
  <<: *defaults
  uri: <%= ENV['MONGOHQ_URL'] %>
  database: qwikstubs
  username: <%= ENV['MONGO_USERNAME'] %>
  password: <%= ENV['MONGO_PASSWORD'] %>

我知道这可能是一个简单的解决方案,但我不太确定如何在这里将两个和两个放在一起。如果我想使用 MongoHQ 应用程序,我需要更改哪些内容?使用 Heroku 和 MongoHQ 是否需要该初始化文件?

任何帮助,将不胜感激!

4

1 回答 1

0

所以它没有对用户进行身份验证的原因是因为我在 MongoHQ Web 控制台中更新了用户名/密码组合,但后来我忘记了更新MONGOHQ_URL环境变量。因此,之前生成的环境变量是

mongodb://<user>:<old_password>@paulo.mongohq.com:10033/my_app

old_password我的实际密码的哈希值在哪里。由于哈希看起来很相似,我没有意识到当我创建一个新密码时,它会创建一个新的哈希。当我将环境变量更改为

mongodb://<user>:<new_password>@paulo.mongohq.com:10033/my_app

我不再收到“身份验证失败”错误。但是,我的应用程序现在会生成一个新错误...

于 2013-11-13T05:44:16.203 回答