'PHP Laravel: No connection could be made because the target machine actively refused it
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?
Solution 1:[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:[2]
I got this error because I forgot to start MySQL in the XAMPP controller.
Solution 3:[3]
sometimes maybe caching problem:
php artisan config:clear
Solution 4:[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:[5]
why is Laravel unable to connect to MySQL when I can connect to it directly with the same parameters?
Come on, env('DB_HOST', 'localhost') is nowhere the same as NULL and env('DB_USERNAME', 'homestead') is nowhere the same as homestead
You cannot call "the same" such different matters like explicitly provided literal, an absent parameter or a result of some function! That's three different matters!
You can say "the same" only if you provide literally same parameters in both cases:
mysql -hlocalhost -uhomestead -psecret homestead
for the shell, and
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
for Laravel.
Solution 6:[6]
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.
Solution 7:[7]
Reasons can be:
If you are trying to access DB from different server then most conman problem faced is DB access is restricted only for localhost/127.0.0.1
Solution: You can modify my.cnf (on Ubuntu again is located in /etc/mysql/my.cnf) and change the following:
bind-address = 0.0.0.0
or
bind-address = SERVER_PUBLIC_IP_ADDRESS
I Recommend to create a db user instead of root and delete root use as It is not secure to provide DB access from any IP with root user.
If you are trying to connect DB from same server:
Solution: Just use TCP/IP instead of the Unix socket. You would do this by using 127.0.0.1 instead of localhost when you connect. The Unix socket might by faster and safer to use, though.
After changes made you have restart mysql service
sudo service mysql restart
Solution 8:[8]
I changed
DB_HOST=localhost
to
DB_HOST=localhost:3307
in .env file
Solution 9:[9]
I had the same problem once but in my own case, I was running on the local server. I later discovered that the error:
"PDOException with a message 'SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it."
was because i have not started Apache and MySQL in Xampp control panel, so the application could not fetch the desired result because there was no coonection.
Immediately I started both Apache and MySQL in Xampp control panel, the error was fixed.
Hope this works for you
Solution 10:[10]
It seems obvious but, sometimes WAMP or XAMP are runnig with some problems or they are not on, so they can cause this problem.
Solution 11:[11]
In windows, just turn off User Access Persimissions (UAC) .-open run -type msconfing and hit Enter -go to tools and look for.UAC -set to never notify -restart the PC, it will
Remember to allow it through firewall when prompted.
Solution 12:[12]
In my case I had written to the .env file in DB_HOST the address of the VM like default of the Homestead (192.168.10.10). But it only works for artisan migrate. However, using apps to connect this db "remotely" requires using an IP of 127.0.0.1.
Hope it'll help someone.
Solution 13:[13]
I got this error with SQLite database. Unlike in other environments, where the database file might be created if it does not exist, in Laravel, I had to manually create the database file. I created it after running my server; and this lead me to this error. After restarting the server it was OK.
Solution 14:[14]
Check your XAMPP (if you're using). I got the same error over and over again until I realized that my xampp is not enabled or offline, so I enabled my xampp, then it finally works.
Solution 15:[15]
I just switched to windows from ubuntu homestead and this happens. What I did was change the env to:
DB_HOST=localhost DB_PORT=33060
also do: cd ~/code/project-name/ then run php artisan config:clear
Solution 16:[16]
In env file I changed ==>
CACHE_DRIVER=redis
TO
CACHE_DRIVER=file
Now it's working.
Solution 17:[17]
I had a similar issue, but in my case, I forgot to start apache and MySQL. When Apache and MySQL is not running you won't have access to the database.
Solution 18:[18]
I faced the same problem. finally found the answer and I hope this solution helps somebody.
php artisan config:clear
and don't forget to start the xaamp or wamp server.
Solution 19:[19]
If you're using Bitnami Wamp Stack you will want to confirm the PORT the MySQL Database is configured too.
If you want to use a different port than the predefined 3306 then you can configure your config/database.php file to
'port' => env('DB_PORT', '3306'), based on the connection type you are using for your projec
Solution 20:[20]
Illuminate\Database\QueryException SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it
When I ran to this error, I tried to come out of it by clearing the cache
php artisan config:clear
php artisan cache:clear
It didn't help but I remembered that the Xampp(my local server) was not started.
Solution 21:[21]
I got this error because mySql and apache were not started before php artisan serve.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
