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

SQLSTATE[HY000] [2002] Connection refused within Laravel homestead(SQLSTATE[HY000] [2002] Laravel 宅基地内的连接被拒绝)
本文介绍了SQLSTATE[HY000] [2002] Laravel 宅基地内的连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Mac OS X and Homestead 2.2.1 with Laravel 5.2.

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

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

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

I have setup a Laravel project with these .env settings

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

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

I have also tried localhost for DB_HOST and root for DB_USERNAME and DB_PASSWORD. And all possible variations of these put together!

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

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

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

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

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.

感谢您的帮助!!

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

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

推荐答案

问题

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

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

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

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

    '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,
    ],

注意没有设置端口!

虽然在我的 .env 文件中我已经设置了 DB_PORT=33060.但是那个 value (3306) 从未被读入 config/database.php.
所以不要像我一样笨,忘记检查database.php文件.

Although in my .env file 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.php file.

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


FIX
Simply add 'port' => env('DB_PORT', 3306), to your config/database.php and set that value in .env like this DB_PORT=3306

这篇关于SQLSTATE[HY000] [2002] Laravel 宅基地内的连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)