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.
Related
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
I am trying to Run php artisan migrate command using terminal.
Any type of help will be appreciated.
If you are using MAMP be sure to add the unix_socket key with a value of the path that the mysql.sock resides in MAMP.
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
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}
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.
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',
...
)
I am getting this error when I am accessing ORM in a script run from the command line:
Database_Exception [ 2 ]: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) ~ MODPATH/database/classes/kohana/database/mysql.php [ 67 ]
This is my directory structure
application
--bootstrap.php
modules
content
--index.php
system
This is my database config:
$database_config = array
(
'default' => array
(
'type' => 'mysql',
'connection' => array(
'hostname' => 'localhost',
'database' => 'driverslife',
'username' => 'root',
'password' => 'root',
'persistent' => FALSE,
'port' => 8889
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
),
);
When I echo MODPATH in the command line, it shows me the right path(slash appended). Is there anything I might be missing?
Use 127.0.0.1 instead of localhost in your config. More info here:
Error when connecting to MySQL using PHP/PDO