Database Error
SQLSTATE[HY000] [2002] Connection refused PDO connection attempt failed to 'localhost' at /var/www/html/application.php on line 17
What This Error Means
This error occurs when the PHP application attempts to connect to a MySQL database, but the database server is not running or is refusing connections due to a misconfigured firewall.
Why It Happens
The most common reasons for this error are: 1) the database server is not running or has crashed, 2) the MySQL port (default is 3306) is blocked by a firewall, 3) the database username or password is incorrect, or 4) the database host or server name is misconfigured.
How to Fix It
- 1To resolve this issue, follow these steps:
- 21. Ensure the database server is running and configured correctly.
- 32. Check the MySQL port is open in the firewall settings.
- 43. Verify the database username, password, and host are correct.
- 54. If using a local development environment, try connecting to a different database server or host.
- 65. Update the PHP code to use a different connection method, such as using the socket instead of TCP/IP.
Example Code Solution
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');$pdo = new PDO('mysql:host=127.0.0.1;dbname=mydatabase', 'username', 'password');Fix for SQLSTATE[HY000] [2002] Connection refused PDO connection attempt failed to 'localhost' at /var/www/html/application.php on line 17
Browse Related Clusters
Related PHP Errors
The controller method 'render' is not defined in the controller 'App\C
Notice: Undefined variable: database_connection in /var/www/html/datab
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Du
Notice: Trying to access array offset on value of type null in /var/ww
Related PHP Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error