我正在尝试使用 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 是否需要该初始化文件?
任何帮助,将不胜感激!