Resolving Heroku application Error : Failed to authenticate user ‘heroku’ on db – Mongo::AuthenticationError

Problem Scenario

I was revamping a Locomotive CMS application from scratch. As a part of deployment of this new application, I copied the MongoDB database from its previous version to the new app. However on accessing the application after deployment I got the following error.

This is a very generic error message and to get into the actual root cause I checked the application log. It contains the following error.

Failed to authenticate user ‘heroku’ on db ‘{My Application Database Name}’ (Mongo::AuthenticationError)

To check the application logs use the following command

>> heroku logs –app {Application Name}

Root cause

When I copied the database from my previous application, it also copied all system tables including “users”. This overrides the existing users defined in the database with the new users.  My old and new application contains this user – “heroku” which is the default user created when adding the mongoHQ addon.  The password for this user will be auto-generated by the system initially.

Now on copying the database, the password got changed and it is not matching with the password defined in the environment variable – MONGOHQ_URL

Solution

Get the value set for the environment variable – MONGOHQ_URL for the application.

>> heroku config –app {appname}

One of the environment variables will be MONGO_URL, which defines the database connection information.

MONGOHQ_URL:  mongodb://heroku:2a85b@alex.mongohq.com:10091/app123

The URL format is as follows

MONGOHQ_URL:  mongodb://{USER_NAME}:{PASSWORD}@{SERVER}:{PORT}/{DATABASE_NAME}

Solution -1:  Reset the password for user – heroku in MongoDB database

[1]  Go to the Heroku console and click on the MongoDB addon.  It will open up the administrative console for the database in a new browser window.

[2]  In the administrative console, select the Admin-> Users option to see the list of all users of this database.

[3] Using the “Change Password” option, change the password of “heroku” user with one defined in MONGO_URL

MONGOHQ_URL:  mongodb://heroku:2a85b@alex.mongohq.com:10091/app123

In this example, 2a85b is the password

Solution -2:  Change the password in MONGO_URL

You can use this solution if you know the password of ‘heroku” user in the previous application database.  All we need here is modify the environment variable – MONGO_URL with the correct password.

[1] Get the MOGO_URL environment variable defined for the application

>> heroku config –app {Application Name}

MONGOHQ_URL:  mongodb://heroku:2a85b@alex.mongohq.com:10091/app123

[2] Update the Environment variable with the correct password.

If the password is ‘abc123” then

>> heroku config:add MONGOHQ_URL=mongodb://heroku:abc123@alex.mongohq.com:10091/app123

Verification

Now check the application to see this issue resolved.

Posted in: Cloud, Programming

3 Comments

  1. Amit says:

    Thanks a lot man!! I was feeling very helpless and lost with this issue, but u saved me 🙂 .
    Very nice explanation of the problem and the solutions. Keep it up

  2. Pink says:

    Nice one! This was driving me crazy – thanks for going through this and explaining it so well.

  3. prasad says:

    Excellent explanation………..

Leave a Comment