Fix SQLSTATE[HY000] [2002] No Such File Error
SQLSTATE[HY000] [2002] No such file or directory usually means PHP tried to reach MySQL through a Unix socket file that does not exist or is not accessible from the application’s runtime. For a local Laravel app running on the same machine as MySQL, the usual fix is to use TCP with DB_HOST=127.0.0.1 instead of localhost, then clear Laravel’s cached configuration. If the app runs in Docker, CI, or a managed hosting environment, the correct database host is usually different.
Use this symptom map before changing application code, database tables, sessions, or migrations.
| Symptom | Likely Cause | Fix |
|---|---|---|
Error mentions “No such file or directory” and DB_HOST=localhost |
PHP is trying to use a missing Unix socket | Use DB_HOST=127.0.0.1 for a local TCP connection, or set the correct socket path |
| MySQL works in the terminal but Laravel fails | Laravel is using different environment variables, a cached config, or a different PHP/container runtime | Check the environment Laravel actually runs in and clear its configuration cache |
Laravel runs in Docker and uses 127.0.0.1 |
127.0.0.1 points to the application container, not the database container |
Use the database service name as DB_HOST |
| App connects to a managed database with a local host value | Laravel is targeting the local machine instead of the remote database endpoint | Use the provider hostname, port, credentials, and required TLS settings |
| CI reports “Connection refused” | The MySQL service is missing, uses another port, or is not ready yet | Match the workflow port to the CI environment and add a health check |
What SQLSTATE[HY000] [2002] No Such File or Directory Means
SQLSTATE is the error category returned through PHP’s PDO database layer. HY000 is a general database error class, while MySQL error 2002 means the client could not establish a connection.
The phrase “No such file or directory” is misleading because it usually does not mean a Laravel file, migration, model, or database table is missing. It usually refers to a Unix socket: a local file MySQL creates so programs on the same host can connect without using TCP networking.
When a MySQL client receives localhost as its host, it may choose a Unix socket connection rather than TCP. That works only when:
- MySQL is running on the same machine or container as PHP.
- PHP can see the socket file.
- PHP and MySQL agree on the socket path.
- The PHP process has permission to access it.
A connection to 127.0.0.1 explicitly uses TCP instead. That is why changing one host value often fixes the error.
Treat Laravel error 2002 as a connection-target problem before changing application code or database data.
How to Fix SQLSTATE[HY000] [2002] in Laravel
Fix the connection by checking the failing environment, then using TCP settings or the exact MySQL socket path.
Step 1: Identify the Environment That Is Actually Failing
First, determine where Laravel is running when the error occurs. The answer may be different for:
- A local
php artisancommand. - A browser request handled by PHP-FPM, Apache, or Nginx.
- A queue worker or scheduled task.
- A Docker container.
- A CI runner.
- A deployed application server.
The .env file on your laptop may not be the configuration used by the failing process. Docker environment variables, deployment settings, CI secrets, and cached Laravel configuration can override it.
Clear Laravel’s cached configuration from the same directory and environment where the failure happens:
php artisan config:clear
If the error appears only in a browser request, make sure the web server is using the same project, container, and environment values as the terminal command.
I use a database client to make a separate connection with the same host, port, user, and TLS settings because it helps separate a database-access problem from an application configuration problem.
Step 2: Use TCP for a Local MySQL Connection
If Laravel and MySQL both run directly on your development machine, update the database connection settings in the .env file used by that Laravel environment.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_SOCKET=
Use DB_HOST=127.0.0.1, not localhost, when you want Laravel to connect through TCP. Port 3306 is the common local MySQL port, but use the port configured for your MySQL installation.
Remove a stale DB_SOCKET value or leave it empty. A configured socket path can cause Laravel to keep attempting a socket connection even after you change the host.
Use this approach only when the database is reachable from the same runtime as Laravel. For example, 127.0.0.1 is correct when PHP and MySQL run directly on the same machine, but it is usually wrong when PHP runs in a container and MySQL runs in another container.
Step 3: Configure the Actual Socket Path When You Need a Socket Connection
A Unix socket connection is valid when PHP and MySQL intentionally run on the same Linux or macOS host and you want to use the socket instead of TCP.
Find the socket path reported by the MySQL server. Replace root with a MySQL user that can connect locally.
mysql -u root -p -e "SELECT @@socket;"
The result is the socket path that MySQL is using. Put that exact path in Laravel’s .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_SOCKET=/path/reported-by-mysql
Do not copy the example path literally. The correct path depends on the MySQL or MariaDB installation and the environment where PHP is running.
If MySQL is running on the host machine but PHP runs in Docker, the container normally cannot see the host’s socket file. In that situation, use TCP or connect through the Docker network instead of mounting a socket path unless you deliberately manage that setup.
Step 4: Verify MySQL Is Reachable Through the Target You Chose
Test the TCP endpoint separately from Laravel. This command forces a TCP connection rather than falling back to a local socket:
mysql --protocol=TCP -h 127.0.0.1 -P 3306 -u root -p
If this command succeeds but Laravel still reports error 2002, the MySQL server is reachable. Recheck Laravel’s effective environment variables, the DB_SOCKET setting, and cached configuration.
If the command fails, Laravel cannot connect because the database service is not listening at that host and port. Start the correct local database service, check whether it uses a different port, or correct the host if the database is remote.
Do not assume that a successful plain mysql -u root -p test proves the TCP connection works. Without an explicit host, the MySQL client may have connected through a socket.
Step 5: Reload Laravel Configuration and Run a Safe Connection Test
After correcting .env, clear Laravel’s configuration cache again:
php artisan config:clear
Then test the connection without applying migrations:
php artisan migrate:status
A migration status list confirms Laravel can authenticate and query the database. If this succeeds, retry the browser request, queue job, or command that originally failed.
If the app still fails only through a web request or worker, restart or redeploy that long-running PHP process using your platform’s normal process management method. A process that was already running may still have old environment values in memory.
Docker and Remote Database Connection Targets
Use the db service name and internal port between Docker containers, or the provider hostname and required TLS settings for managed databases.
Laravel and MySQL in Separate Docker Containers
Inside a Docker container, localhost and 127.0.0.1 mean “this container.” They do not mean the host computer or another service container.
If your Docker Compose database service is named db, use that service name:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_SOCKET=
The Docker network resolves db to the MySQL container. Use the MySQL container’s internal port, commonly 3306, rather than the host port mapping used by tools on your laptop.
For example, a mapping exposed for host access may let a desktop database client connect through another host port, while Laravel must still use db:3306 from inside its own container network.
Laravel Connecting to a Managed Database
A managed database is not local to the Laravel runtime, even if your laptop can reach it. Set DB_HOST to the database provider’s hostname, not localhost or 127.0.0.1.
Also match the provider’s required port, username, password, database name, and TLS configuration. If the provider requires encrypted connections, configure Laravel and the MySQL driver for that requirement rather than trying to bypass it with a local socket setting.
Fixing Error 2002 in GitHub Actions
GitHub Actions errors often say Connection refused instead of No such file or directory, but the diagnosis is similar: Laravel is targeting the wrong database endpoint, port, or startup state.
When a workflow runs directly on the GitHub-hosted runner and maps MySQL to an alternate host port, set the same port in both the service mapping and .env.ci. For example, this uses port 33306 on the runner:
services:
mysql:
ports:
- "33306:3306"
options: >-
--health-cmd=" mysqladmin ping "
--health-interval=10s
--health-timeout=5s
--health-retries=3
Use matching Laravel settings:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=33306
The health check matters because a container can start before MySQL is ready to accept queries. mysqladmin ping checks availability every 10s, allows a 5s timeout per check, and retries 3 times.
If your GitHub Actions job itself runs inside a container, use the MySQL service name as the host instead of 127.0.0.1. The service’s internal MySQL port is then used rather than the runner’s mapped host port.
Confirm the Fix and Prevent It From Returning
Verify each Laravel and MySQL connection setting after making a change:
- Laravel’s
DB_HOSTmatches the machine or container where MySQL actually runs. DB_SOCKETis blank unless you intentionally use a Unix socket.- A direct MySQL TCP connection works when you use
127.0.0.1and the configured port. - Docker-based Laravel apps use a database service name rather than
localhost. - Managed database connections use the provider hostname and required TLS settings.
- Laravel’s configuration cache was cleared after editing
.env. - Long-running PHP workers or web processes were restarted if they retain old configuration.
Once php artisan migrate:status succeeds from the failing environment, run the original request or test suite again. If it fails only in one environment, compare that environment’s effective host, port, socket, and TLS settings rather than changing database tables or Laravel session configuration.
Frequently Asked Questions
What does SQLSTATE[HY000] [2002] Connection Refused mean?
Connection refused means Laravel reached the target network address but no MySQL server was accepting connections there. The usual causes are a stopped MySQL service, an incorrect port, a missing CI service, or MySQL not being ready when tests begin. Unlike “No such file or directory,” this usually points to a TCP networking target rather than a missing Unix socket.
How do I fix error 2002 when I want to use a MySQL socket?
Use the exact socket path reported by MySQL in Laravel’s DB_SOCKET setting. Keep DB_HOST=localhost for that intentional local socket connection, and ensure PHP runs on the same host or has access to the socket file. A socket path from the host machine will not normally work inside an isolated Docker container.
Why does MySQL work in the terminal but fail in Laravel?
The terminal and Laravel may use different connection methods or environments. Your terminal may connect through a default Unix socket while Laravel tries a stale socket path, a different TCP host, cached configuration, or container-specific variables. Test MySQL with an explicit TCP host and port, then make Laravel use the same target.
What database host should GitHub Actions use for Laravel tests?
Use DB_HOST=127.0.0.1 when the job runs directly on the runner and MySQL is exposed through a mapped service port such as 33306. Use the service name, such as mysql, when the job runs inside its own container on the same service network. In both cases, Laravel’s port must match the route used by that job.
