Laravel:SQLSTATE[HY000] [2002] 连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42390640/
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-09-14 15:23:42  来源:igfitidea点击:

Laravel: SQLSTATE[HY000] [2002] Connection refused

phpmysqllaravellaravel-5.4

提问by Toyler Lainf

I am using Vagrant with fresh laravel 5.4 installed. After I run auth I have ran the migrate to migrate the MySQL tables. All of these process successful but when I try to login or register to test the system I am getting error:

我正在使用安装了新 laravel 5.4 的 Vagrant。运行 auth 后,我运行了 migrate 来迁移 MySQL 表。所有这些过程都成功了,但是当我尝试登录或注册以测试系统时,出现错误:

SQLSTATE[HY000] [2002] Connection refused.

SQLSTATE[HY000] [2002] 连接被拒绝。

But my database working perfect since I already able to migrate the tables. Whats possible solution could be? check attached image (I'm using Ubuntu 16.04)

但是我的数据库运行良好,因为我已经能够迁移表。可能的解决方案是什么?检查附加图像(我使用的是 Ubuntu 16.04)

Connection refused.

拒绝连接。

回答by Onix

Try changing your DB_HOST from 127.0.0.1 to localhost. and if still not working try using the default mysql port to 3306

尝试将 DB_HOST 从 127.0.0.1 更改为 localhost。如果仍然无法正常工作,请尝试使用默认的 mysql 端口到 3306

回答by josef

if you use laravel maybe your PDO PHP Extension not set. in your php.ini uncomment extension=php_pdo_mysql.dll

如果你使用 laravel,可能你的 PDO PHP 扩展没有设置。在你的 php.ini 取消注释 extension=php_pdo_mysql.dll

if you had no access to php.ini do this: add this to config/database.php in mysql section

如果您无法访问 php.ini,请执行以下操作:将其添加到 mysql 部分的 config/database.php

'options' => [PDO::ATTR_EMULATE_PREPARES => true],

your code should be like this:

你的代码应该是这样的:

'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'options' => [PDO::ATTR_EMULATE_PREPARES => true],
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

回答by Disljenko

If you are trying to run php artisan migrate from host machine you need to set in .env conf file DB_PORT=33060 as that is a port vagrant/Homestead is listening on. But when i change that my application stop working. So i suggest ssh to vagrant and run php artisan migrate from VM and you will have no problems.

如果您尝试从主机运行 php artisan migrate,您需要在 .env conf 文件 DB_PORT=33060 中进行设置,因为这是一个 vagrant/Homestead 正在监听的端口。但是当我改变我的应用程序停止工作时。所以我建议ssh到vagrant并从VM运行php artisan migrate,你不会有问题。