Php artisan migrate no connection could be made because the target machine actively refused it

September 5, 2020 Category : Laravel

Show

I fetched this issue when i was new in laravel and i clone project on my bitbucket. I was tring to run my project, but when i composer update at last i had found this error :

no connection could be made because the target machine actively refused it laravel.

If you also found this error then Check your Database Configration (database.php). maybe you haven't setting of your database configration details. After this it will run try this, it was worked for me....

Php artisan migrate no connection could be made because the target machine actively refused it

Hardik Savani

I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.

Follow Me:

We are Recommending you

    Problem :

    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?

    Tags : php,mysql,laravel,pdo



    Answer 1:

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


    Answer 2:

    sometimes maybe caching problem:

        php artisan config:clear
    

    Answer 3:

    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.


    Answer 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.


    Answer 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.


    Answer 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.


    Answer 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
    

    Answer 8:

    I changed

    DB_HOST=localhost

    to

    DB_HOST=localhost:3307

    in .env file


    Answer 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


    Answer 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.


    Answer 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.


    Answer 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.


    Answer 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.


    Answer 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.


    Answer 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