Sometimes a MySQL PDO error message below would be got

SQLSTATE[HY000] [2002] No such file or directory

The error message may be shown due to the line below

$db = new PDO('mysql:host=localhost;dbname=db_name;charset=utf8', 'username', 'password');

What’s going on? Try to change “localhost” to “127.0.0.1”

$db = new PDO('mysql:host=127.0.0.1;dbname=db_name;charset=utf8', 'username', 'password');

 

The error message should then be solved.