Home >Backend Development >PHP Tutorial >Why Am I Getting 'PDOException: SQLSTATE[HY000] [2002] No Such File or Directory' When Connecting to My Database?
Symptoms:
When attempting to use database-related commands like php artisan migrate or php artisan db:seed, users may encounter the error message:
[PDOException] SQLSTATE[HY000] [2002] No such file or directory
Possible Causes and Solutions:
Verify that the MySQL server is up and running. If not, start it using the appropriate command.
In Laravel 4, edit the app/config/database.php file and change the "host" field from "localhost" to "127.0.0.1".
In Laravel 5 , modify the .env file and change the "DB_HOST" variable from "localhost" to "127.0.0.1".
By default, "localhost" establishes a UNIX socket connection, which can fail to find the database if it's not located in the standard directory. Switching to "127.0.0.1" uses TCP (Transmission Control Protocol), which is more reliable in this scenario.
The above is the detailed content of Why Am I Getting 'PDOException: SQLSTATE[HY000] [2002] No Such File or Directory' When Connecting to My Database?. For more information, please follow other related articles on the PHP Chinese website!