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
- Verify the database service is running
- Check DB_HOST and DB_PORT in .env
- Test the connection with a database client
- Check firewall rules for remote databases
- 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.