PHP Laravel:无法建立连接,因为目标机器主动拒绝它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32514241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:01:35  来源:igfitidea点击:

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

phpmysqllaravelpdo

提问by jstein

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 serveand I navigate to http://localhost:8000/admin/categories/, I receive the following error message:

我正在 Laravel 5 中构建一个 Web 应用程序。该应用程序应该将“类别名称”存储在 MySQL 数据库中,并显示一个表单以添加新的“类别名称”。当我执行命令php artisan serve并导航到http://localhost:8000/admin/categories/ 时,我收到以下错误消息:

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:

根据我在堆栈溢出上阅读的几篇文章,许多遇到此错误的用户没有正确配置 .env 文件,该文件覆盖了在 database.php 文件中指定的 PHP 数据对象 (PDO) 的默认设置。.env 文件定义如下:

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

And the mysql key within the database.php file is given as:

database.php 文件中的 mysql 键为:

 '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?

奇怪的是,当我 ssh 进入我的虚拟机并运行时mysql -uhomestead -psecret homestead,我能够连接到数据库。问题是,当我可以用相同的参数直接连接到它时,为什么 Laravel 无法连接到 MySQL?还有什么可以拒绝访问 Laravel?

采纳答案by jstein

I changed the ip address specified in the homestead.yaml from localhost to 192.168.10.10. Then I ran homestead provisionand that seemed to fix the problem. The host machine cannot resolve localhost or 127.0.0.1 because that is already mapped to itself.

我将 homestead.yaml 中指定的 ip 地址从 localhost 更改为 192.168.10.10。然后我跑了homestead provision,这似乎解决了问题。主机无法解析 localhost 或 127.0.0.1,因为它们已经映射到自身。

回答by Jigs Virani

I'm having the same problem with Wampserver. It's worked for me:

我在 Wampserver 上遇到了同样的问题。它对我有用:

You must change this file: "C:\wamp\bin\mysql[mysql_version]\my.ini" For example: "C:\wamp\bin\mysql[mysql5.6.12]\my.ini"

您必须更改此文件:“C:\wamp\bin\mysql[mysql_version]\my.ini” 例如:“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

并将默认端口 3306 更改为 80。(第 20 和 27 行,在两者中)端口 = 3306 到端口 = 80

I hope this is helpful.

我希望这是有帮助的。

And then Just Go to your Control panel and start Apache & MySQL Services.

然后只需转到您的控制面板并启动 Apache 和 MySQL 服务。

回答by Your Common Sense

why is Laravel unable to connect to MySQL when I can connect to it directly with the same parameters?

当我可以使用相同的参数直接连接到 MySQL 时,为什么 Laravel 无法连接到它?

Come on, env('DB_HOST', 'localhost')is nowhere the same as NULLand env('DB_USERNAME', 'homestead')is nowhere the same as homestead

来吧,env('DB_HOST', 'localhost')是不一样的NULLenv('DB_USERNAME', 'homestead')不一样的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 differentmatters!

您不能将诸如明确提供的文字、不存在的参数或某个函数的结果这样不同的事情称为“相同”!这是三个不同的问题!

You can say "the same" only if you provide literallysame 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.

对于 Laravel。

回答by joun

sometimes maybe caching problem:

有时可能缓存问题:

    php artisan config:clear

回答by Moslem Deris

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 33060and the problem solved.

我只是将我的 DB_PORT 设置为33060并解决了问题。

回答by Parveen Shukhala

Reasons can be:

原因可能是:

If you are trying to access DB from different serverthen most conman problem faced is DB access is restricted only for localhost/127.0.0.1

如果您尝试从不同的服务器访问 DB,那么面临的大多数骗子问题是DB 访问仅限于 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:

解决方法:可以修改my.cnf(在Ubuntu上再次位于/etc/mysql/my.cnf)并更改以下内容:

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.

我建议创建一个 db 用户而不是 root 并删除 root 使用,因为使用 root 用户从任何 IP 提供数据库访问是不安全的。

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.

解决方案:只需使用 TCP/IP 而不是 Unix 套接字。您可以在连接时使用 127.0.0.1 而不是 localhost 来完成此操作。不过,Unix 套接字可能会更快更安全地使用。

After changes made you have restart mysql service

更改后,您已重新启动 mysql 服务

sudo service mysql restart

回答by Rohit Behera

I changed

我变了

DB_HOST=localhost

DB_HOST=本地主机

to

DB_HOST=localhost:3307

DB_HOST=本地主机:3307

in .env file

在 .env 文件中

回答by iMichaelOwolabi

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

"PDOException with a message 'SQLSTATE[HY000] [2002]无法建立连接,因为目标机器主动拒绝了它。”

was because i have not started Apache and MySQL in Xamppcontrol panel, so the application could not fetch the desired result because there was no coonection.

是因为我还没有启动Apache and MySQL in Xampp控制面板,所以应用程序无法获取所需的结果,因为没有连接。

Immediately I started both Apache and MySQLin Xampp control panel, the error was fixed. Hope this works for you

我立即Apache and MySQL在 Xampp 控制面板中启动了the error was fixed. 希望这对你有用

回答by Paul Burilichev

In my case I had written to the .envfile in DB_HOSTthe 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.

就我而言,我已将VM 地址中的.env文件写入到DB_HOSTHomestead 的默认地址 (192.168.10.10) 中。但它只适用于artisan migrate. 但是,使用应用程序“远程”连接此数据库需要使用 127.0.0.1 的 IP。

Hope it'll help someone.

希望它会帮助某人。

回答by Henry

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

在 Windows 中,只需关闭 User Access Persimissions (UAC) .-open run -type msconfing 并按 Enter -go to tools and look for.UAC -set to never notice -restart the PC,它会

Remember to allow it through firewall when prompted.

请记住在出现提示时允许它通过防火墙。