1

I want to run an app using MySQL on heroku. I have already installed and configured cleardb for this purpose. All I have left to do is configure environment variables. However, I can't find a proper way to do this.

in this article, Matt explains how to configure the mentioned variables:

// config/database.php

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

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

// . . .

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

And he also says:

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

So, how exactly should environment variables be configured in production to run MySQL using cleardb on heroku?

Alex Lomia
  • 6,705
  • 12
  • 53
  • 87

2 Answers2

6

Being tagged as Laravel 5.2, you need to set the environment variable in the .env file. This file is not included in version control, because you are supposed to have different versions on each installation. A .env.example is provided by default.

As an example, if your URL is:

CLEARDB_DATABASE_URL => mysql://adffdadf2341:adf4234@us-cdbr-east.cleardb.com/heroku_db?reconnect=true

then in your .env file you can put:

DB_CONNECTION=mysql
DB_HOST=us-cdbr-east.cleardb.com
DB_PORT=3306
DB_DATABASE=heroku_db
DB_USERNAME=adffdadf2341
DB_PASSWORD=adf4234

and leave the config/database.php untouched.

The other solution would be to define the URL above in the .env file and parse it dynamically as suggested. Note that these variables are accessed using env("CLEARDB_DATABASE_URL").

EDIT

Since the CLEARDB_DATABASE_URL is set automatically, it will probably be better to make use of it.

// config/database.php

if ($url = env('CLEARDB_DATABASE_URL', false)) {
    $parts = parse_url($url);
    $host = $parts["host"];
    $username = $parts["user"];
    $password = $parts["pass"];
    $database = substr($parts["path"], 1);
} else {
    $host = env('DB_HOST', 'localhost');
    $username = env('DB_USERNAME', 'forge');
    $password = env('DB_PASSWORD', '');
    $database = env('DB_DATABASE', 'forge');
}

// ...
        'mysql' => [
            'driver' => 'mysql',
            'host' => $host,
            'database' => $database,
            'username' => $username,
            'password' => $password,
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
        ],
BeS
  • 817
  • 4
  • 9
  • Thanks for an answer! Can you please clarify the following issues? 1. Is it guaranteed, that the database url won't get changed? Otherwise hardcoding the credentials would not be a good solution. and 2. Is parsing the url dynamically safe? – Alex Lomia Apr 23 '16 at 09:05
  • Actually, it is not hard-coding because it is a configuration file that doesn't need to go with your source files. If the database URL changes (it probably will if you create another database) you can adapt these in the server. The only issue that you need to consider is a small performance hit because the file will need to be parsed on each request. The solution for this will be to define these same variables via the Heroku CLI. – BeS Apr 24 '16 at 08:46
0

Setting environment variables on Heroku is very simple. You can use the Heroku CLI, as follows: heroku config:set CLEARDB_DATABASE_URL="Your DB URL"

You can also easily configure environment variables via the Heroku dashboard.

Yoni Rabinovitch
  • 5,171
  • 1
  • 23
  • 34
  • CLEARDB_DATABASE_URL is set automatically as soon as you install cleardb. The question is - how to make use of that url? – Alex Lomia Apr 18 '16 at 21:01