PHP Laravel: No connection could be made because the target machine actively refused it
Solution 1
I changed the ip address specified in the homestead.yaml from localhost to 192.168.10.10. Then I ran homestead provision
and that seemed to fix the problem. The host machine cannot resolve localhost or 127.0.0.1 because that is already mapped to itself.
Solution 2
I got this error because I forgot to start MySQL in the XAMPP controller.
Solution 3
sometimes maybe caching problem:
php artisan config:clear
Solution 4
I'm having the same problem with Wampserver. It’s worked for me:
You must change this file: "C:\wamp\bin\mysql[mysql_version]\my.ini" For example: "C:\wamp\bin\mysql[mysql5.6.12]\my.ini"
And change default port 3306 to 80. (Lines 20 & 27, in both) port = 3306 To port = 80
I hope this is helpful.
And then Just Go to your Control panel and start Apache & MySQL Services.
Solution 5
I faced the same problem and read all the answers on internet but none of them helped! finally found the answer and I hope this solution helps somebody.
I just set my DB_PORT to 33060
and the problem solved.
Related videos on Youtube
jstein
Updated on December 02, 2021Comments
-
jstein over 2 years
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 stack overflow, 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:
DB_HOST=localhost DB_DATABASE=homestead DB_USERNAME=homestead DB_PASSWORD=secret
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?-
aldrin27 over 8 yearsAre you running your file through homestead? or local?
-
jstein over 8 yearsI get the same message either way
-
aldrin27 over 8 yearsMaybe this will help you stackoverflow.com/questions/32373592/…
-
Gokigooooks over 8 yearsI know this suggestion is far-fetched but can you try if your php is up to date or does it have the PDO extensions in it?
-
jstein over 8 years"If you eliminate the impossible, whatever remains, however improbable, must be the truth." I am using php 5.6, which should support PDO extension
-
-
jstein over 8 yearsTrue, I am calling a function to import the connection parameters. Unfortunately, when I fill in the array with the literal key-value pairs, I am still getting the same error message.
-
Brainmaniac almost 7 yearsfor me it woks with 127.0.0.1 as long as I am on my vm (homestead ssh)
-
lehel about 6 yearsFor me changing the DB_HOST to 192.168.10.10 solved it.
-
Linga about 4 yearsThis could be the answer.
-
joun about 4 yearsgreat, sometimes this is one of the factor..glad it can help you
-
Mike about 4 yearsI tried in many places and ways to change the port, but in the end, switching it back to 3306 for MySQL was the easiest answer.
-
Zack almost 3 yearsThis was true for my case
-
Akshay Shrivastav almost 3 yearsFor me it solved by removing the port value, i just kept it blank
-
DestinyB over 2 yearsSometimes, it is very frustrating to forget a thing like this and you start searching for solutions. Lol.!
-
Kiran RS over 2 yearsEven I tried optimize:clear it doesn't help me, but this answer did.