Back to Error Center

SQLSTATE connection refused

Error Message

SQLSTATE[HY000] [2002] Connection refused

Cause

The database server is not running, the host/port configuration is wrong, or the database server is not accepting connections.

Solution

Start the database server. Check DB_HOST and DB_PORT in .env. For MySQL, ensure the service is running: sudo systemctl status mysql. For SQLite, ensure the database file path is correct.

Code Example

Fix
# Check MySQL is running
sudo systemctl status mysql

# Check .env configuration
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

# Test connection manually
mysql -h 127.0.0.1 -P 3306 -u root -p

Debugging Steps

  1. Verify the database service is running
  2. Check DB_HOST and DB_PORT in .env
  3. Test the connection with a database client
  4. Check firewall rules for remote databases
  5. For SQLite, verify the file path and permissions

Prevention

Always verify database service is running before deploying. Use connection health checks in production. Consider using environment-specific database configs.