How can I get Laravel 4 environment variables to work? - php

I have created an app with laravel and I have a set of environments I want to run on the site as always. The setup comes from the start.php file where I declare the environments like so:
$env = $app->detectEnvironment(array(
'local' => array('Mark-macbook.local'),
'development' => array('excelsior.servers.prgn.misp.co.uk'),
'production' => array('excelsior.servers.prgn.misp.co.uk'),
));
I then create files in the root where server.php is and create files that have the correct database details for each environment in like so:
.env.local.php
<?php
return array(
'DATABASE_HOST' => 'localhost',
'DATABASE_NAME' => 'borough',
'DATABASE_USER' => 'root',
'DATABASE_PASSWORD' => 'root',
'UNIX_SOCKET' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);
.env.development.php
<?php
return array(
'DATABASE_HOST' => 'localhost',
'DATABASE_NAME' => 'db-name',
'DATABASE_USER' => 'db-user',
'DATABASE_PASSWORD' => 'pass'
);
.env.production.php
etc etc
Then in my database.php file in app/config I have this setup:
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['DATABASE_HOST'],
'unix_socket' => $_ENV['UNIX_SOCKET'],
'database' => $_ENV['DATABASE_NAME'],
'username' => $_ENV['DATABASE_USER'],
'password' => $_ENV['DATABASE_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
So this is all setup as how I know it usually works but when I run php artisan serve I get this error:
{"error":{"type":"ErrorException","message":"Undefined index: DATABASE_HOST","file":"\/Freelance\/Current Projects\/borough\/build\/borough-cc\/app\/config\/database.php","line":67}}
Does anyone know why this would happen and what I may be doing wrong here?
Cheers

From memory, as I haven't used Laravel a lot, you are not using it correctly.
Basically, the database access will look at the configuration keys in a kind of global configuration. This global config is the result of the combination of your config files from the config folder and your environment config.
Therefore, you just need to redeclare (or simply declare) your environment specific variables in the environment config.
.env.local.php
<?php
return array(
'connections' => array(
'mysql' => array(
'host' => 'localhost',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'borough,
'username' => 'root,
'password' => 'root',
),
),
),
);
database.php
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
I'm a bit afraid in your example your are not defining the same keys in both config... Basically, you define the common config first, then you redefine whatever you need in the environment specific files.

Related

Laravel 5.2 Database [xxx] not configured, while being defined in database.php

So I have 3 mysql DB's defined in my database.php file. I had 2 for the longest time and everything worked fine. I have one titled mysql, one mysql1 and one mysql2. The first 2 work fine, I copied the second one, changed the name to mysql2 and added the proper parameters but it still always says "Database mysql1 not configured. Is there some sort of cache? or somewhere else I have to define it? I am calling the DB through a model like this.
protected $connection = 'mysql1';
This is my database.php for clarification
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql1' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
This was fixed by using the command
php artisan config:cache
I submitted a Laravel issue request to fix it since that absolutely should not be required to change the DB connections variable.
If you can not delete cache with php artisan config:cache because it throws error after you run it:
[InvalidArgumentException]
Database [] not configured
And if you are sure that your connections parameters are good then try to manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder.

Artisan migrate fails in development environment because of missing environment variable in production config

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?
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.
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}

how to change database name in laravel? changing config>database.php doesn't work

I created a database "mydatabase" and I changed config>database.php to:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
now inside route.php i have:
Route::get('/', function()
{
$data=DB::table('user')->get();
return $data;
});
laravel sends an Exception which shows that it tries to access:
homestead.user
instead of
mydatabase.user
now if i change route.php to:
Route::get('/', function()
{
$data=DB::table('mydatabase.user')->get();
return $data;
});
it will work!
Also according to this question I changed config>local>database.php to:
'mysql' => array(
'driver' => 'mysql',
'host' => 'mysite.local',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
But this time, even
$data=DB::table('mydatabase.user')->get();
doesn't work either! This time it thrown another exception :
PDOException (2002)
SQLSTATE[HY000] [2002] Connection refused
My question is why laravel tries to use "homestead" database instead of "mydatabase"? should I change something else?
EDIT:
I changed the config/local/database.php to
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydatabase',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and everything works fine! (I changed mysite.local to localhost)
I've not define local host in my /etc/hosts so why laravel looks for that host?
Your host should be localhost. The term localhost means the computer which laravel is running on. mysite.local is presumably a virtual site residing on this computer. It doesn't have its own installation of Mysql. All virtual sites will share the same mysql. They will just use different databases.
Thats how my setups work anyway.
The problem is in your config/database.php with default connection, currently default connection setting is getting from .env file as
'default' => env('DB_CONNECTION', 'mysql'),
So change it to :
'default' => 'mysql',

Artisan unable to access environment variables from $_ENV

Any artisan command I enter into the command line throws this error:
$ php artisan
<?
return array(
'DB_HOSTNAME' => 'localhost',
'DB_USERNAME' => 'root',
'DB_NAME' => 'pc_booking',
'DB_PASSWORD' => 'secret',
);
PHP Warning: Invalid argument supplied for foreach() in /home/martin/code/www/pc_backend/vendor/laravel/framework/src/Illuminate/Config/EnvironmentVariables.php on line 35
{"error":{"type":"ErrorException","message":"Undefined index: DB_HOSTNAME","file":"\/home\/martin\/code\/www\/pc_backend\/app\/config\/database.php","line":57}}
This is only on my local development system, where I recently installed apache and php. On my production system on a shared host artisan commands work just fine. The prod system has it's own .env.php, but other than that the code should be identical.
Relevant files:
.env.local.php
<?
return array(
'DB_HOSTNAME' => 'localhost',
'DB_USERNAME' => 'root',
'DB_NAME' => 'pc_booking',
'DB_PASSWORD' => 'secret',
);
app/config/database.php
<?php
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['DB_HOSTNAME'],
'database' => $_ENV['DB_NAME'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'migrations' => 'migrations',
),
);
The $_ENV array is populated as expected on the website - the problem appears to be with artisan only.
So I finally figured out how to fix it.
It turns out that the file was not processed as a php file because I was using a short opening tag in the .env.local.php file. Using a normal opening tag solved it. I don't know why though, as short tags work fine elsewhere.

Laravel 4 migration:install ErrorException

php artisan migrate:install
{"error":{"type":"ErrorException","message":"PDO::__construct(): [2002] Connection refused (trying to connect via tcp:\/\/127.0.0.1:3306)","file":"\/Applications\/MAMP\/htdocs\/DRCSports\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connectors\/Connector.php","line":47}}
In my database.php I have updated the information to mysql
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'Laravel_DRCSports',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I am not sure if I am understanding the error right, but to me it looks like my laravel isn't connecting to mysql right. If that is the case I have no clue how to fix it.
The problem was that mysql is running on port 8888, while Laravel's default port value is 3306 (as it's the default port of mysql servers).
The solution is to add 'port' key to the array (For example: 'port' => 8888) and it'll
do the work.
This is what i did... in /app/config/app.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:8889',
'database' => 'pic',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and at the bottom of the php code
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 8888,
'database' => 0,
),
),
it has to work it...
I experienced problems (Laravel 4) when I used MySQL on a port other than 3306.
A browser-run app expects the following app/config/database syntax:
'mysql' => array(
...
'host' => 'localhost',
'port' => '8889',
...
)
While the command-line run artisan expects the following syntax:
'mysql' => array(
...
'host' => 'localhost:8889',
...
)
The issue is described here:
https://github.com/laravel/laravel/issues/1182
Most articles suggest a workaround using Laravel environments, but it results in duplicate config files and violates the DRY principle (Don't Repeat Yourself), so here's another alternative:
At the top of app/config/database.php:
$my_hostname = 'localhost';
$my_port = '8889';
$my_database = 'database';
$my_username = 'username';
$my_password = 'password';
if (App::runningInConsole()) { // artisan runs from the command line
// change 'localhost' to 'localhost:8889'
$my_hostname = $my_hostname.':'.$my_port;
}
And further down:
'mysql' => array(
'driver' => 'mysql',
'host' => $my_hostname,
'port' => $my_port,
'database' => $my_database,
'username' => $my_username,
'password' => $my_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Cheers
Change your database information in your config/database.php and .env file.
I did these changes and worked like a champ :
in database.php :
host : localhost:8889
Port: 8889
and my mamp has a password so I there was two way to putting that password either in database.php file or in .env file I changed the password '' value to 'forge' and then use my MAMP password in the .env file
by the way, you can see your specific information about MAMP in MAMP application in the port tab (MySQL one)
Make sure to edit this part of ".env" file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=root
This worked for me.
i guess you can solve this problem by add the following code inside app/config/database syntax:
'mysql' => array(
...
'pconnect' => 'TRUE',
...
)

Categories