Appearance
Warning: mysqli_connect(): (HY000/2002): No such file or directory
This error message indicates that PHP's MySQLi extension cannot locate the Unix socket file required to connect to the MySQL server. The "No such file or directory" message specifically refers to the missing socket path, not a generic network or credential issue.
This error typically occurs due to:
- The MySQL server is not running or the socket file was not created.
- The
hostparameter is set tolocalhost, which forces a Unix socket connection, but the expected socket path is missing or misconfigured. - The
mysqli.default_socketsetting inphp.inipoints to an incorrect location.
You should troubleshoot by:
- Changing the host from
localhostto127.0.0.1in your connection string to force a TCP/IP connection:php$conn = mysqli_connect('127.0.0.1', 'username', 'password', 'database'); - Verifying the correct socket path using
php -i | grep mysqli.default_socketand updatingphp.iniif necessary. - Ensuring the MySQL server is running and the socket file exists at the specified path.