How to use MongoHQ on Heroku without Verifying your Account

dec 18, 2012

Disclaimer: This post describes a way to use a free Heroku add-on with a free Heroku account, without verifying the account with a credit card. I am in no way intending to defraud Heroku or Heroku add-on providers. This information is for educational purposes only.

For those developers who have been living under a rock, Heroku is a cloud computing PaaS. It supports many languages and frameworks and provides additional features through add-ons.

For my basic usage of Heroku, I wanted to run a small Bottle app on my free account and use MongoDB for data storage. MongoDB is not directly available through Heroku, but there are third-party add-ons that provide it as a service (with free plans). MongoHQ is one of them.

If my Heroku account was verified, I could easily install the MongoHQ add-on using the following command.

$ heroku addons:add mongohq:sandbox
Adding mongohq:sandbox on foo-bar-1234... failed
 !    Please verify your account to install this add-on
 !    For more information, see http://devcenter.heroku.com/categories/billing
 !    Verify now at https://heroku.com/verify

But as you can see, the command fails, even when trying to add the free (sandbox) add-on.
Account verification requires a credit card, and I don't like to toss mine around the internet unless I have to.
Luckily, there is a way around this... It turns out that installing the MongoHQ add-on using the Heroku toolbelt just provisions a database on the MongoHQ platform and sets the MONGOHQ_URL configuration variable to contain the correct database connection string. Both of these things can be done "by hand".

Instructions

First, create a database on MongoHQ. Simply sign-up for an account and follow the instructions on their site. After you have a database, copy the Mongo URI from the Admin > Overview page of the database on MongoHQ. It should look like this (host and port may vary):

mongodb://<user>:<password>@alex.mongohq.com:10007/<database>

Fill in any missing info. Example:

mongodb://foo:bar@alex.mongohq.com:10007/mymongodb

Next, using the Heroku Toolbelt, set the MONGOHQ_URL (can actually be named anything) configuration variable to this value.

$ heroku config:set MONGOHQ_URL="mongodb://foo:bar@alex.mongohq.com:10007/mymongodb"

That's it.

The configuration variable that we set is exposed as an environment variable and can be used to open a connection to the database. The free MongoDB, provided by MongoHQ, is now ready for use.
A Python example:

con = pymongo.Connection(os.getenv('MONGOHQ_URL'))

Do good, not evil.

blog comments powered by Disqus