简体   繁体   中英

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?

有时可能是缓存问题:

    php artisan config:clear

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.

我收到此错误是因为我忘记在 XAMPP 控制器中启动 MySQL。

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.

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.

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.

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

I changed

DB_HOST=localhost

to

DB_HOST=localhost:3307

in .env file

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

看起来很明显,但是,有时 WAMP 或 XAMP 运行时会出现一些问题,或者它们没有打开,因此它们可能会导致此问题。

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.

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.

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.

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.

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

In env file I changed ==>

CACHE_DRIVER=redis

TO

CACHE_DRIVER=file

Now it's working.

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.

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.

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

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.

我收到此错误是因为在 php artisan 服务之前没有启动 mySql 和 apache。

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM