PHPWarningDatabase ErrorJune 26, 2026

Database Error

Error executing query: SQLSTATE[HY000] [2002] Connection refused

What This Error Means

This error occurs when the database connection is refused by the server, usually due to the database being down or not running. It can also happen if the connection details are incorrect, such as the wrong hostname or port number.

Why It Happens

This error typically happens when the database is not running, or when the connection details in the PHP script are incorrect. It can also occur if the firewall or network configuration blocks the connection to the database. Additionally, if the database server is down or not responding, this error will occur.

How to Fix It

  1. 1To fix this error, follow these steps:
  2. 21. Check if the database is running and accessible. If it's a local database, try restarting it. If it's a remote database, check the server status and ensure it's online.
  3. 32. Verify the connection details in the PHP script, including the hostname, port number, username, and password. Make sure they match the actual database settings.
  4. 43. Check the firewall and network configuration to ensure that the connection to the database is not blocked. You may need to add an exception or update the firewall rules.
  5. 54. If the issue persists, try using a different database driver or connection library to see if the problem is specific to the current one.

Example Code Solution

❌ Before (problematic code)
PHP
$dsn = 'mysql:host=localhost;dbname=mydb';
$username = 'myuser';
$password = 'mypassword';

try {
    $pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}
✅ After (fixed code)
PHP
$dsn = 'mysql:host=192.168.1.100;dbname=mydb';
$username = 'myuser';
$password = 'mypassword';

try {
    $pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
    echo 'Error: ' . $e->getMessage();
}

Fix for Error executing query: SQLSTATE[HY000] [2002] Connection refused

Related PHP Errors

Related PHP Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error