Laravel SQLSTATE[HY000] [2002] Connection refused 错误解决

如果使用了homestead来搭建laravel的运行环境,mysql的端口不是本地的3306而是映射到33060(可修改,这里是默认值)

打开config/database.php

1
2
3
4
5
6
7
8
9
10
11
12
'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,
],

添加一行,加入端口配置

1
'port' => env('DB_PORT', 'localhost'),

再到环境文件.env中加入一行

1
DB_PORT=33060