SQLSTATE[HY000] [2002] Connection refused
PHP could not open a connection to MySQL at the host and port Laravel used. Start the server or correct DB_HOST and DB_PORT, then clear the config cache.
- Message
- SQLSTATE[HY000] [2002] Connection refused
- Exception
- Illuminate\Database\QueryException
What it means
PHP tried to connect to MySQL and nothing accepted the connection at that address. No SQL ran: PDO threw a PDOException and Laravel wrapped it in Illuminate\Database\QueryException. Laravel appends the address it used, such as (Connection: mysql, Host: 127.0.0.1, Port: 3399, ...), or Socket: ... when DB_SOCKET is set. Read that first: it shows the values the app really used, which are not always the ones in your .env.
Why it happens
- MySQL is not running, or it listens on another port. A
DB_PORTthat points at a closed port gives exactly this message. DB_HOST=localhostdoes not mean127.0.0.1. The PHP manual says that on Unix the host namelocalhostconnects through a socket file instead of TCP. If that file is not where PHP looks (thepdo_mysql.default_socketsetting), you get a sibling of this error:SQLSTATE[HY000] [2002] No such file or directory.- The host name does not resolve. Laravel Sail tells you to set
DB_HOST=mysql, a name that only exists on the container network. Run the same app outside the containers and you get the third sibling:SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mysql failed: nodename nor servname provided, or not known. The words afterfailed:come from the operating system, so they read differently on Linux. - The config cache is stale. After
php artisan config:cache, Laravel no longer reads.env. Correcting the port in.envchanges nothing until you rebuild the cache: the message keeps showing the old port.
How to fix it
Check what Laravel uses and whether it connects:
php artisan config:show database.connections.mysql
php artisan db:show
config:show prints the values from the config cache when there is one, so a wrong port there and a right one in .env points at a stale cache.
If MySQL is not running, start it. If the port is wrong, set the one your server listens on:
DB_HOST=127.0.0.1
DB_PORT=3306
For a No such file or directory error, either use 127.0.0.1 so PHP connects over TCP, or keep localhost and tell Laravel where the socket is. It is often /tmp/mysql.sock with Homebrew and /var/run/mysqld/mysqld.sock on Debian and Ubuntu. Ask MySQL for the path:
SHOW VARIABLES LIKE 'socket';
DB_HOST=localhost
DB_SOCKET=/tmp/mysql.sock
For a getaddrinfo error, use a host name that resolves where the code runs. Inside Sail's containers that is mysql. From your own machine, connect to the port Sail forwards: DB_HOST=127.0.0.1 and DB_PORT=3306, or the value of FORWARD_DB_PORT.
After any change to .env on a server that caches its config, rebuild the cache:
php artisan config:cache
Find it in production
With APP_DEBUG=false the visitor gets a 500 page titled "Server Error". With the default SESSION_DRIVER=database, every page that uses the web middleware fails this way, even a page that runs no query of its own, because Laravel reads the session from the sessions table first. The built-in /up health route kept answering 200 while the database was down, so a check on /up does not catch this, unless a listener on Illuminate\Foundation\Events\DiagnosingHealth runs DB::select('select 1'). Then /up answers 500 too.
Laravel reports the exception: each failed request writes a SQLSTATE[HY000] [2002] line at error level to storage/logs/laravel.log. Ranetrace's uptime check requests your site about every minute and alerts you once it has been down for two minutes.
Reproduced on Laravel 13.45, PHP 8.5, MySQL 8.0.27, 5 December 2026.
Related
Read more
Start monitoring today
Websites go down and code throws errors. Ranetrace tells you when it happens to yours, why it matters and what to do next.