沃梦达 / 编程问答 / php问题 / 正文

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

PHP Laravel: No connection could be made because the target machine actively refused it(PHP Laravel:无法建立连接,因为目标机器主动拒绝它)

本文介绍了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 时,我能够连接到数据库.问题是,当我可以用相同的参数直接连接到它时,为什么 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:无法建立连接,因为目标机器主动拒绝它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐