PHP Laravel:无法建立连接,因为目标计算机主动拒绝了它 [英] PHP Laravel: No connection could be made because the target machine actively refused it

查看:175
本文介绍了PHP Laravel:无法建立连接,因为目标计算机主动拒绝了它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

database.php文件中的mysql密钥为:

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,

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

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?

推荐答案

我将homestead.yaml中指定的IP地址从localhost更改为192.168.10.10.然后我运行了homestead provision,这似乎可以解决问题.主机无法解析localhost或127.0.0.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.

这篇关于PHP Laravel:无法建立连接,因为目标计算机主动拒绝了它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆