PHP Laravel: No connection could be made because the target machine actively refused it
Asked Answered
H

23

25

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?

Heritage answered 11/9, 2015 at 1:42 Comment(5)
Are you running your file through homestead? or local?Votive
I get the same message either wayHeritage
Maybe this will help you #32374092Votive
I 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?Lyonnaise
"If you eliminate the impossible, whatever remains, however improbable, must be the truth." I am using php 5.6, which should support PDO extensionHeritage
H
7

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.

Heritage answered 18/9, 2015 at 16:23 Comment(2)
for me it woks with 127.0.0.1 as long as I am on my vm (homestead ssh)Lodgings
For me changing the DB_HOST to 192.168.10.10 solved it.Chitchat
C
25

I got this error because I forgot to start MySQL in the XAMPP controller.

Capuano answered 5/11, 2020 at 12:5 Comment(3)
This was true for my caseCorporative
Sometimes, it is very frustrating to forget a thing like this and you start searching for solutions. Lol.!Chickenlivered
well my case too!Creed
D
14

sometimes maybe caching problem:

    php artisan config:clear
Decern answered 1/6, 2018 at 1:3 Comment(3)
This could be the answer.Weekly
great, sometimes this is one of the factor..glad it can help youDecern
Even I tried optimize:clear it doesn't help me, but this answer did.Admirable
H
7

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.

Heritage answered 18/9, 2015 at 16:23 Comment(2)
for me it woks with 127.0.0.1 as long as I am on my vm (homestead ssh)Lodgings
For me changing the DB_HOST to 192.168.10.10 solved it.Chitchat
M
3

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.

Merciless answered 11/9, 2015 at 5:10 Comment(1)
For me it solved by removing the port value, i just kept it blankLumumba
S
2

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.

Stertorous answered 11/9, 2015 at 8:3 Comment(1)
True, 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.Heritage
M
2

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
Megadeath answered 14/2, 2017 at 11:19 Comment(0)
Y
2

I changed

DB_HOST=localhost

to

DB_HOST=localhost:3307

in .env file

Yoder answered 21/2, 2018 at 19:24 Comment(0)
P
2

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.

Plugugly answered 29/8, 2018 at 4:32 Comment(1)
I 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.Jacquenette
H
1

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

Hardfeatured answered 19/4, 2018 at 5:50 Comment(0)
G
1

It seems obvious but, sometimes WAMP or XAMP are runnig with some problems or they are not on, so they can cause this problem.

Gules answered 19/7, 2021 at 20:47 Comment(0)
A
0

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.

Aho answered 29/11, 2015 at 7:24 Comment(0)
I
0

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.

Impressive answered 5/4, 2017 at 10:21 Comment(0)
P
0

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.

Ptah answered 25/2, 2019 at 21:2 Comment(0)
A
0

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.

Antechamber answered 31/8, 2019 at 3:8 Comment(0)
T
0

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

Tittle answered 29/1, 2020 at 11:11 Comment(0)
E
0

In env file I changed ==>

CACHE_DRIVER=redis

TO

CACHE_DRIVER=file

Now it's working.

Equipoise answered 8/2, 2020 at 14:46 Comment(0)
H
0

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.

Hax answered 16/5, 2020 at 9:38 Comment(0)
T
0

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.

Tew answered 30/7, 2020 at 5:59 Comment(0)
C
0

If you're using Bitnami Wamp Stack you will want to confirm the PORT the MySQL Database is configured too.

Picture

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

Crab answered 28/9, 2020 at 3:29 Comment(0)
H
0

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.

Hidebound answered 29/6, 2021 at 2:32 Comment(0)
H
0

I got this error because mySql and apache were not started before php artisan serve.

Hunt answered 1/12, 2021 at 15:30 Comment(0)
F
0

for me personally it was because I forgot to start my Redis server in my windows... . I just launched it and it worked

Finstad answered 10/12, 2023 at 12:8 Comment(0)
F
0

in my case, it was about my migrations table in db. it was not about connection or port number at all. everything was fine except my migrations db was empty. but migrations effects where presented in my db. so I synced my migrations table with migrations where actually applied to my db ( I entered the migration file names to migration table, those migrations that where run already but where not present in the table....) and I was fine

Finstad answered 6/2 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.