0

I am trying to configure my Rails3 application with MongoMapper to connect to the MongoHQ database that was created through my Heroku account. Right now when I push my code to Heroku, it crashes with a Mongo::AuthenticationError. The stack trace from the logs is below.

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)

Here's the full thing

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

I think my problem has to do with an initializer file that tries to create a MongoMapper connection. Here is the code, which can also be found here.

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

I am pretty sure something is wrong in the line where I define a new MongoMapper connection. The database.yml file that it loads from is here:

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

I know this is probably a simple solution, but I'm not quite sure how to put two and two together here. What do I need to change if I want to use my MongoHQ application? Is that initializer file even necessary using Heroku and MongoHQ?

Any help would be appreciated!

KJ50
  • 765
  • 1
  • 10
  • 27

1 Answers1

0

So the reason why it was not authenticating the user is because I updated the username/password combination in my MongoHQ web console, but then I forgot to update the MONGOHQ_URL environment variable. Therefore, the previously generated env variable was

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

Where old_password was a hash of my actual password. Since the hashes looked similar, I did not realize that when I created a new password, it created a new hash. When I changed the env variable to

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

I did not get the "Failed authentication" error anymore. However, my application now generates a new error...

KJ50
  • 765
  • 1
  • 10
  • 27