Laravel Sail - SQLSTATE[HY000] [2002] No such file or directory

 Because you are trying to connect to DB container via socket from different container.

There are two separate containers - Laravel and MySQL. Thinks of it like two separate servers.

When you have both unix_socket and db_host in env, you are telling laravel to connect to db at specified host via unix_socket. That is why it didn't work.

When you update the unix_socket env, laravel try to connect to DB via TCP and it work.

P.S: I know this issue is already resolved for you. But in case someone else stumble upon this question, they should have some explanation on why it work and how to fix it in the future.


The error "SQLSTATE[HY000] [2002] No such file or directory" usually occurs when Laravel Sail is unable to connect to the database because the MySQL service is not running or there is a misconfiguration in the database connection settings.

Here are some steps you can follow to troubleshoot this issue:

  1. Check if the MySQL service is running

Make sure that the MySQL service is running on your local machine or within the container. If it is not running, you can start it by running the following command:

sql
sudo service mysql start
  1. Check the database connection settings

Ensure that the database connection settings in the .env file are correct. Check the following variables:

makefile
DB_CONNECTION=mysql 
DB_HOST=mysql 
DB_PORT=3306 
DB_DATABASE=laravel 
DB_USERNAME=sail 
DB_PASSWORD=password

Make sure that the database name, username, and password match the values set in the docker-compose.yml file.

  1. Check if the database container is running

If the MySQL service is running but you still get the error, check if the MySQL container is running by running the following command:

docker-compose ps

If the container is not running, you can start it by running the following command:

docker-compose up -d mysql
  1. Check the MySQL logs

If the above steps do not work, you can check the MySQL logs to see if there are any errors. Run the following command to view the logs:

docker-compose logs mysql

This will display the logs for the MySQL container. Look for any error messages that might be related to the connection issue.

If none of the above steps work, you may need to reinstall Laravel Sail and try again.


Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755