Reputation: 7235
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?
Upvotes: 1
Views: 3029
Reputation: 817
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:[email protected]/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' => '',
],
Upvotes: 6
Reputation: 5261
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.
Upvotes: 0