Viewed   881 times

I am building a web application in Laravel 5. The application is supposed to get "category names" stored on a MySQL database and display a form to add new "category names". When I execute the command php artisan serve and I navigate to http://localhost:8000/admin/categories/, I receive the following error message:

PDOException in Connector.php line 50:
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.

According to several posts I read on , many users encountering this error did not properly configure the .env file, which overrides the default settings for the PHP Data Object (PDO), as specified in the database.php file. The .env file is defined below:


And the mysql key within the database.php file is given as:

 'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'homestead'),
            'username'  => env('DB_USERNAME', 'homestead'),
            'password'  => env('DB_PASSWORD', 'secret'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,

Oddly, when I ssh into my virtual machine and I run mysql -uhomestead -psecret homestead, I am able to connect to the database. The question is, why is Laravel unable to connect to MySQL when I can connect to it directly with the same parameters? What else could be denying access to Laravel?



I changed the ip address specified in the homestead.yaml from localhost to Then I ran homestead provision and that seemed to fix the problem. The host machine cannot resolve localhost or because that is already mapped to itself.

Thursday, December 8, 2022

There's a conflict between the PHP that is used by Apache and the PHP that is linked to the command line. (It happens more often that it should to be honest).

What is typically done is:

which php

This tells you which php will be expecuted when running in the command line. e.g. /usr/bin/php

mv /usr/bin/php /usr/bin/php.old

Then link or copy the correct PHP version to an executable path:

ln -s /path/to/php/bin/php /usr/bin/php

or the following should also work.

cp /path/to/php/bin/php /usr/bin/php

Also suggested if you want to be able to manually install mods:

ln -s /path/to/php/bin/phpize /usr/bin/phpize
ln -s /path/to/php/bin/php-config /usr/bin/php-config

This way your CLI will match your webserver.


If as noted in this answer if you are using Ubuntu with multiple alternative installations of PHP you can do:

sudo update-alternatives --set php /usr/bin/php<version>
sudo update-alternatives --set phar /usr/bin/phar<version>
sudo update-alternatives --set phar.phar /usr/bin/phar.phar<version> 
sudo update-alternatives --set phpize /usr/bin/phpize<version> 
sudo update-alternatives --set php-config /usr/bin/php-config<version>
Thursday, December 22, 2022

The only way I could get it working was to replace the prepared statement with an 'exec' call:

$rowCount = DB::connection()->getpdo()->exec(
            "LOAD DATA LOCAL INFILE '$fileName'
            INTO TABLE $tableName
            CHARACTER SET utf8mb4
            FIELDS TERMINATED BY ','
            LINES TERMINATED BY '\n'"

I have no idea why it wouldn't work using a prepared statement in Laravel - it definitely does work with a pure PDO prepared statement.

Sunday, October 9, 2022

Well this might be late but for future visitors,

I found out back then that refuses any connections to it's DB from outside source which means that only the web-apps hosted on have the access to their DB other than that you will get rejected.

Saturday, November 19, 2022

didn't you say your mysql port is 3306:

$db = new PDO("mysql:host=".DBHOST.";port=8889

this might help:

$db = new PDO("mysql:host=".DBHOST.";port=3306
Thursday, December 15, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :