本地执行php artisan 命令报错如下:
admindeMBP:laravel56 admin$ php artisan config:clear
In Connection.php line 664:
SQLSTATE[HY000] [2002] Connection refused (SQL: select `name`, `value` from
`admin_config`)
In Connector.php line 68:
SQLSTATE[HY000] [2002] Connection refused
网上很多资料让配置php.ini中的pdo_mysql.default_socket= /tmp/mysqld.sock文件。
也有让修改配置文件DB_HOST 127.0.0.1改成localhost
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', ''),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
上面两个方法配置没问题,但还是报错,查看laravel.log
[2019-02-18 01:48:26] local.ERROR: SQLSTATE[HY000] [2002] No such file or directory {"exception":"[object] (PDOException(code: 2002): SQLSTATE[HY000] [2002] No such file or directory at /Users/admin/data/study_model/laravel56/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68)
[stacktrace]
#0 /Users/admin/data/study_model/laravel56/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(68): PDO->__construct('mysql:host=loca...', 'root', 'root', Array)
#1 /Users/admin/data/study_model/laravel56/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(44): Illuminate\\Database\\Connectors\\Connector->createPdoConnection('mysql:host=loca...', 'root', 'root', Array)
PDO连接数据库报错:“SQLSTATE[HY000] [2002] No such file or directory”。
出现这个问题的原因是还是PDO无法找到mysql.sock或者mysqld.sock。
看配置文件中unix_socket参数没有配置,加上下面配置后,OK了。
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock