SQLSTATE [HY000] [2002] Laravel宅基地内的连接被拒绝 [英] SQLSTATE[HY000] [2002] Connection refused within Laravel homestead

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

问题描述

将Mac OS X和Homestead 2.2.1与Laravel 5.2结合使用.

Using Mac OS X and Homestead 2.2.1 with Laravel 5.2.

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

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 设置

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

我还尝试了 localhost 的DB_HOST和 root 的DB_USERNAME和DB_PASSWORD.并将所有这些可能的变化放在一起!

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 migration时,会出现相同的SQLSTATE[HY000] [2002] Connection refused错误.
现在我完全迷路了...

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.


FIX
只需将'port' => env('DB_PORT', 3306),添加到您的config/database.php中,然后像这样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宅基地内的连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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