Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory

执行下面的代码报错:

$pdo = new PDO('mysql:dbname=db_test;host=localhost', 'root', '123456');
PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in test.php:2
Stack trace:
#0 test.php(2): PDO->__construct('mysql:dbname=db...', 'root', '123456')
#1 {main}
  thrown in test.php on line 2

将上面 "host=localhost" 修改成 "host=127.0.0.1" 即可

$pdo = new PDO('mysql:dbname=db_test;host=127.0.0.1', 'root', '123456');

 

你可能感兴趣的:(Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory)