Laravel on Heroku - Using a MySQL database

Posted on May 01, 2014 | By Matt Stauffer


Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

My last post was about getting Laravel up and running on Heroku. But, of course, that's not enough; how many Laravel apps don't have some sort of data store?

So, there's good news and bad news (but more good than bad).

First, the bad: Heroku doesn't use MySQL on its servers. But that's it for the bad news.

The good news: Heroku uses PostgreSQL, which is significantly better than MySQL in many ways. Also, Laravel has a PostgreSQL driver built in. Also, there is a MySQL Herku add-on you can purchase for smaller scale work, and it has a free intro version.

As you can see, we're in great shape here. This post will cover Laravel, Heroku, and MySQL, and the next post will cover the same with PostgreSQL.

Adding ClearDB to your app

If you're set on MySQL, there's a Heroku Add-on called ClearDB that provides relatively first-class MySQL support to Heroku apps.

So, first, let's install ClearDB. Navigate to your app directory locally and use the Heroku toolbelt to install the add-on:

$ heroku addons:add cleardb

You should see the following:

Adding cleardb on app-name-here... done, v6 (free)
Use `heroku addons:docs cleardb` to view documentation.

You're now on the limited free tier of the ClearDB add-on. You can retrieve your database URL at any point by running the following command, which retrieves your Heroku config and then greps out just the line beginning with CLEARDB_DATABASE_URL:

$ heroku config | grep CLEARDB_DATABASE_URL

It should look something like this:

CLEARDB_DATABASE_URL: mysql://h95b1k2b5k2kj:ont1948@us-cdbr-east-05.cleardb.net/heroku_nt9102903498235n?reconnect=true

Don't worry about writing that down, though, because it's going to be passed into our app as an environment variable.

For more thorough instructions on setting up ClearDB, check out their provisioning docs.

Authenticating to ClearDB in your Laravel site

Next, let's modify our Laravel app to connect to ClearDB.

First, let's add a few quick lines to our Laravel app that make it actually need a database. Thankfully, there's already a user authentication model and system built into Laravel, so let's just hit it for our default route. Edit routes.php and change its contents to the following:

Route::get('/', function()
{
    return User::all();
});

Now generate a migration to create the users table:

$ php artisan migrate:make create_users_table --create=users

Next, let's add in our Heroku creds. Again, if you're actually working on a real site, you should be making sure you're just editing the database credentials specifically for your production environment here, but since we're just hacking out a dummy app here, we're going to edit app/config/database.php directly.

For now let's just do a bit of procedural code at the top of database.php. We're telling our app to get the CLEARDB_DATABASE_URL environment variable and then split it out.

$url = parse_url(getenv("CLEARDB_DATABASE_URL"));

$host = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$database = substr($url["path"], 1);

Remember, the CLEARDB_DATABASE_URL value we looked at before was just a URL, so we're using PHP's parse_url function to pull out the pieces of that URL and convert them into Laravel-config-friendly variables.

Now just find the 'mysql' entry in the database.php config array, and change the values accordingly:

    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => $host,
        'database'  => $database,
        'username'  => $username,
        'password'  => $password,
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    ),

Of course, this will break locally, so let's test it out on Heroku.

$ git add .
$ git commit -m "Add Heroku creds and update default route to hit the DB."
$ git push heroku master

Now we need to remotely run our migration, which we do with the following:

$ heroku run php /app/artisan migrate

If everything runs without errors, you should be able to visit your site in your browser and see the seeds of your future Laravel app:

[]

(This is the JSON-encoded dump of your users table, which, at the moment, is empty).

That's it! You now know how to use MySQL on Heroku, run migrations and other artisan commands remotely, and deploy your code to your Heroku app.

Conclusion

You can see that it takes a bit of work, but you can get MySQL databases up and running on Heroku with Laravel quickly and simply. Check out my next post for how to get Laravel working with Heroku PostgreSQL.


Comments? I'm @stauffermatt on Twitter


Tags: php  •  laravel  •  mysql  •  heroku