We're live-coding on Twitch! Join us!
Using MySQL on Heroku

Using MySQL on Heroku

How to install a MySQL Heroku Add-on and use it with Codeigniter 3.x

This is a guide to follow up to Deploying a PHP and MySQL web app with Heroku. So we’ve created a new Heroku app and done the necessary, now let’s install MySQL. First, open your apps listing here and choose the app you want to install MySQL on. You should see something like this

In the Add-ons search field, type ClearDB MySQL and select it. Choose the free plan (Ignite) and click provision. So now you’ve got MySQL installed, now we need to set it up so the app uses our local database.php file on our machine when we deploy to heroku. Go to your apps listing on heroku and choose your app. Click on the Settings tab. You should now see a config tab like in the picture below.

Click Reveal Config Varsand copy theCLEARDB_DATABASE_URL value. I’ll use mine as an example: mysql://b5bf52783a17e5:90ac0673@us-cdbr-iron-east-05.cleardb.net/heroku_f677d75e73d39a7?reconnect=true. Everything after the @ symbol until the / is the DB_HOST, us-cdbr-iron-east-05.cleardb.net Everything after / until ? is DB_DATABASE, heroku_f677d75e73d39a7 The string after the // until : is the DB_USERNAME, b5bf52783a17e5 The string between : and @ is the DB_PASSWORD, 90ac0673 Now that we have our heroku DB credentials, let’s update our database.php file so that the app can access the ClearDB database when deployed to heroku. My credentials now look like this.

Now we can git add . && git commit -m "Added DB Config", and afterwards we can run git push heroku master. And just like that, you have a working staging environment with a MySQL database ready to go. Note: ClearDB has an auto-increment value of 10 so the record ID’s will range from 2, 12, 22 and so on. The Ignite plan has a maximum database size of 5MB.

Like this article? Follow @LK_wapiti on Twitter