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.
在终端(在我的项目文件夹中的宅基地内)我可以执行 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
相关文章