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

62,725

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.

Share:
62,725

Related videos on Youtube

jstein
Author by

jstein

Updated on December 02, 2021

Comments

  • jstein
    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
      aldrin27 over 8 years
      Are you running your file through homestead? or local?
    • jstein
      jstein over 8 years
      I get the same message either way
    • aldrin27
      aldrin27 over 8 years
      Maybe this will help you stackoverflow.com/questions/32373592/…
    • Gokigooooks
      Gokigooooks over 8 years
      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?
    • jstein
      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
    jstein over 8 years
    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.
  • Brainmaniac
    Brainmaniac almost 7 years
    for me it woks with 127.0.0.1 as long as I am on my vm (homestead ssh)
  • lehel
    lehel about 6 years
    For me changing the DB_HOST to 192.168.10.10 solved it.
  • Linga
    Linga about 4 years
    This could be the answer.
  • joun
    joun about 4 years
    great, sometimes this is one of the factor..glad it can help you
  • Mike
    Mike about 4 years
    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.
  • Zack
    Zack almost 3 years
    This was true for my case
  • Akshay Shrivastav
    Akshay Shrivastav almost 3 years
    For me it solved by removing the port value, i just kept it blank
  • DestinyB
    DestinyB over 2 years
    Sometimes, it is very frustrating to forget a thing like this and you start searching for solutions. Lol.!
  • Kiran RS
    Kiran RS over 2 years
    Even I tried optimize:clear it doesn't help me, but this answer did.