php SQLSTATE[HY000] [2002] Laravel 宅基地内的连接被拒绝

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

SQLSTATE[HY000] [2002] Connection refused within Laravel homestead

phplaravel-5migrationhomesteadartisan

提问by nclsvh

Using Mac OS X and Homestead 2.2.1 with Laravel 5.2.

在 Laravel 5.2 中使用 Mac OS X 和 Homestead 2.2.1。

In terminal (within homestead in my project folder) I can do php artisan to see all the available commands. When I try to run php artisan migrate I get a connection error:
SQLSTATE[HY000] [2002] Connection refused

在终端(在我的项目文件夹中的 homestead 中)我可以执行 php artisan 来查看所有可用的命令。当我尝试运行 php artisan migrate 时,出现连接错误:
SQLSTATE[HY000] [2002] Connection refused



I have setup a Laravel project with these .envsettings

我已经使用这些.env设置 设置了一个 Laravel 项目

DB_HOST=127.0.0.1
DB_DATABASE=tcv
DB_USERNAME=homestead
DB_PASSWORD=secret

I have also tried localhostfor DB_HOST and rootfor DB_USERNAME and DB_PASSWORD. And all possible variations of these put together!

我还为 DB_HOST尝试了localhost,为 DB_USERNAME 和 DB_PASSWORD尝试了root。以及所有这些可能的变体放在一起!



In Sequel Pro (db management application) I CAN connect with these settings

在 Sequel Pro(数据库管理应用程序)中,我可以使用这些设置进行连接

Host       127.0.0.1
Username   homestead
Password   secret
Database   tcv
Port       33060

But this database is obviously empty, because I cant migrate to it from terminal ...

但是这个数据库显然是空的,因为我无法从终端迁移到它......

As far as I can make out it is a configuration issue, since I can connect to it with Sequel Pro. But I have honestly no freaking idea what is setup wrong.

据我所知,这是一个配置问题,因为我可以使用 Sequel Pro 连接到它。但老实说,我不知道什么设置错误。

Thanks for the help !!

谢谢您的帮助 !!

EDIT
For some reason I get the same SQLSTATE[HY000] [2002] Connection refusederror when moving my project to MAMP and running php artisan migrate.
Now I am completely lost ...

编辑
出于某种原因SQLSTATE[HY000] [2002] Connection refused,将我的项目移动到 MAMP 并运行 php artisan migrate 时,我遇到了同样的错误。
现在我完全迷失了......

采纳答案by nclsvh

Problem

问题

In Laravel you have config/database.phpwhere all the setup for the connection is located. You also have a .envfile in the root directory in your project (which everyone uses for timesaving). This contains variables that you can use for the entire project.

在 Laravel 中,您拥有config/database.php所有连接设置所在的位置。您.env在项目的根目录中也有一个文件(每个人都使用它来节省时间)。这包含可用于整个项目的变量。

On a standard L5 project the MySql section of config/database.phplooks like this:

在标准 L5 项目中,MySql 部分config/database.php如下所示:

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
        'engine'    => null,
    ],

Notice there is no port set!

注意没有设置端口!

Although in my .envfile I had set DB_PORT=33060. But that value (3306)was never read into the config/database.php.
So don't be a dumbass like myself and forget to check the database.phpfile.

虽然在我的.env文件中我已经设置了DB_PORT=33060. 但这value (3306)从未读入config/database.php.
所以不要像我一样愚蠢而忘记检查database.php文件。



FIXFIX


只需添加'port' => env('DB_PORT', 3306),'port' => env('DB_PORT', 3306),到您的 config/database.php 并在 .env 中设置该值,如下所示DB_PORT=3306DB_PORT=3306

回答by Richard Hood

I just ran into this and found that changing this in the .envfile from 127.0.0.1to localhostfixed it.

我刚刚遇到这个问题,发现在.env文件中将其更改127.0.0.1localhost修复它。

DB_HOST=localhost

回答by Albert A

If you are using MAMP on a mac OS add the following line to your mysql database config file

如果您在 mac OS 上使用 MAMP,请将以下行添加到您的 mysql 数据库配置文件中

'unix_socket' => env('DB_SOCKET', ''),

and on your .env file add

并在您的 .env 文件中添加

DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

回答by Lode Alaert

Use localhostinstead of 127.0.0.1(in your .envfile), then run command:

使用localhost代替127.0.0.1(在您的.env文件中),然后运行命令:

php artisan config:cache

回答by Andrei

Another solution for anyone else who has a problem. I had all the settings correct but for some reason my changes weren't updated. Laravel actually caches the config file (which I find completely stupid).

其他有问题的人的另一种解决方案。我的所有设置都正确,但由于某种原因,我的更改没有更新。Laravel 实际上缓存了配置文件(我觉得这很愚蠢)。

Here was my solution after updating the configs:

这是我更新配置后的解决方案:

php artisan config:clear

php artisan config:clear

回答by Tom Kincaid

I was having this problem. When connecting with Sequel Pro, I need to use 33060as the port, but in the .envfile it has to be 3306. I had 33060in the .envfile. Changed it to 3306and it worked.

我遇到了这个问题。与 Sequel Pro 连接时,我需要33060用作端口,但在.env文件中它必须是3306. 我33060.env文件中。将其更改为3306并且有效。

回答by Ashwani Garg

PROBLEM SOLVEDenter image description hereI had the same problem and fixed just by converting DB_HOST constant in the .env File FROM 127.0.0.1into localhost *JUST DO THIS AS GIVEN IN THE BELOW LINE* DB_HOST = localhost.

问题已解决在此处输入图片说明我遇到了同样的问题,只需将 DB_HOST 常量转换.env File FROM 127.0.0.1localhost *JUST DO THIS AS GIVEN IN THE BELOW LINE* DB_HOST = localhost.

No need to change anything into config/database.php

无需更改任何内容 config/database.php

enter image description here

在此处输入图片说明

回答by SouthernBoy

This is a simple fix. Your mysql database has lost its connection to the server. If you are running a local server run this in your terminal:

这是一个简单的修复。您的 mysql 数据库已失去与服务器的连接。如果您正在运行本地服务器,请在终端中运行:

mysqld

That will reconnect your database. Then (if you are using homebrew) run:

这将重新连接您的数据库。然后(如果您使用自制软件)运行:

brew services start mysql

This will automatically connect your database on login.

这将在登录时自动连接您的数据库。

回答by Diego Santa Cruz Mendezú

I had the same problem, try this works

我有同样的问题,试试这个工作

DB_HOST=localhost

回答by Emmanuel de Carvalho Garcia

After you put all configuration on .envfile, if you already run php artisan serve, RESTART IT.

.env文件上放置所有配置后,如果您已经运行php artisan serve,请重新启动