2

I have two database configs, one for production and one for development:

// app/config/database.php
'connections' => array(
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => $_SERVER['RDS_HOSTNAME'],
        'database'  => $_SERVER['RDS_DB_NAME'],
        'username'  => $_SERVER['RDS_USERNAME'],
        'password'  => $_SERVER['RDS_PASSWORD'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    )
)

// app/config/development/database.php
'connections' => array(
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => $_SERVER['MYSQL_PORT_3306_TCP_ADDR'],
        'database'  => $_SERVER['MYSQL_ENV_MYSQL_DATABASE'],
        'username'  => $_SERVER['MYSQL_ENV_MYSQL_USER'],
        'password'  => $_SERVER['MYSQL_ENV_MYSQL_PASSWORD'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    )
)

The relevant database environment variables exist (the ones beginning with MYSQL_), and when running the migrate command:

php artisan migrate --env=development

the following exception gets thrown:

{
    "error":{
        "type":"ErrorException",
        "message":"Undefined index: RDS_HOSTNAME",
        "file":"/var/www/app/config/database.php",
        "line":50
    }
}

Why does Laravel care if the environment variable in my production config doesn't exist when I don't even want to use the production configuration? How do I get around this?

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
John Dorean
  • 3,744
  • 9
  • 51
  • 82
  • Possibly because it's PHP warning which is handled by Laravel and it treats warnings as errors. – insanebits Mar 05 '15 at 15:32
  • Does the App::environment(); output "development" ? – Th3Alchemist Mar 05 '15 at 15:36
  • Yeah, but why would Laravel even be executing the production config when I specified the development environment? Is there a way to get Laravel to ignore warnings such as this? – John Dorean Mar 05 '15 at 15:36
  • @Th3Alchemist Yeah, putting `die(var_dump(App::environment()));` at the top of the file causes it to print `development`. – John Dorean Mar 05 '15 at 15:37
  • What Laravel version do you run? could you check your /bootstrap/start.php (for 4.2) and check if your hostname is registered as development envirnoment? – Th3Alchemist Mar 05 '15 at 15:43

2 Answers2

1

This problem can happen when in your .env file you have the following

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=

Instead, you should have

RDS_CONNECTION=mysql
RDS_HOSTNAME=localhost
RDS_PORT=3306
RDS_DB_NAME=test_db
RDS_USERNAME=root
RDS_PASSWORD=

The difference is in the name of the keys. In addition to that, this might be of interest in case that problem is related with bash and AWS.

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
0

My suggestion would be to use environment variables instead, and make sure the keys match between environments (I think that's your biggest issue).

// File: .env.development.php
return [
    'database_mysql_host' => '',
    'database_mysql_database' => '',
    'database_mysql_username' => '',
    'database_mysql_password' => '',
];

Then you can remove both of your config files and just modify app/config/database.php:

'connections' => array(
    'mysql' => array(
        'driver'    => 'mysql',
        'host'      => $_ENV['database_mysql_host'],
        'database'  => $_ENV['database_mysql_database'],
        'username'  => $_ENV['database_mysql_username'],
        'password'  => $_ENV['database_mysql_password'],
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
    )
)

I'm assuming for the moment that you're setting the environment variables on the production server in the vhost or some other apache configuration file. Change those keys where they are being set from the 'special' ones you picked out for your production environment to match the keys you're using in your development environment:

SetEnv database_mysql_host {your value for RDS_HOSTNAME}
SetEnv database_mysql_database {your value for RDS_DB_NAME}
SetEnv database_mysql_username {your value for RDS_USERNAME}
SetEnv database_mysql_password {your value for RDS_PASSWORD}
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • Not an option unfortunately. Docker automatically sets the development variables and it would be a PITA to change them. The RDS variables are set by Amazon Web Services. – John Dorean Mar 05 '15 at 15:39
  • an alternate solution could be to just make sure that both sets of environment config keys are at least available (even if empty) in both environments – Jeff Lambert Mar 05 '15 at 15:40