PDOException: SQLSTATE[HY000] [2002] Connection refused
The error "PDOException: SQLSTATE[HY000] [2002] Connection refused" is a common error in PHP when trying to connect to a MySQL database. This error typically occurs when the database server refuses the connection. This can be due to a variety of reasons such as incorrect connection details (hostname, port, username, password), the MySQL server not running, or the server being blocked by a firewall.
Example:
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'root', 'wrongPassword');
echo 'Connected';
} catch(PDOException $exception) {
//
}
- We are not pushy
- We only send a few emails every month. That's all.
- No spam
- We only send articles, and helpful tips for developers, not SPAM.