SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost'

SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost'

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoding SQLSTATE[HY000] [1045]: When CakePHP Can't Talk to MySQL

Encountering SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' is a common frustration when wiring up a PHP application with a MySQL database. It typically means the credentials provided to your application do not match what MySQL expects, or the connection parameters are misaligned with your environment's reality. This error often appears during schema migrations or initial setup when frameworks like CakePHP attempt to connect via PDO.

The stack trace usually points to PDODriverTrait, where the underlying PDO constructor fails before any SQL is executed. In the specific scenario involving a Bitnami WAMP stack running CakePHP, the issue rarely lies in the application code itself but rather in the intersection of MySQL user privileges and environment-specific configurations.

The Illusion of "Root" Privileges

A frequent misconception during development is assuming that creating a user with GRANT ALL PRIVILEGES grants access from everywhere. However, MySQL distinguishes between hosts explicitly defined in the user table. A user named test2@localhost is distinct from test2@%. If your application configuration specifies 'host' => 'localhost', but you created the user with a wildcard host (%), or vice versa, authentication will fail regardless of the password.

In the provided configuration example, the application attempts to connect using:

'username' => 'test2',
'password' => 'computer',
'host' => 'localhost',
'database' => 'jobs',

If the MySQL user table does not contain an entry for test2@localhost with the password computer, PDO will throw the access denied exception. Simply running GRANT ALL PRIVILEGES ON *.* TO 'test2'@'%' IDENTIFIED BY 'computer'; is insufficient if your application insists on connecting as localhost. You must ensure a matching entry exists:

CREATE USER 'test2'@'localhost' IDENTIFIED BY 'computer';
GRANT ALL PRIVILEES ON jobs.* TO 'test2'@'localhost';
FLUSH PRIVILEGES;

Environment Mismatch in Bitnami WAMP

Bitnami stacks often run MySQL on a non-standard port, such as 3307 or even higher, rather than the default 3306. If your application is configured to connect to mysql:host=localhost without specifying a port, and the server is listening on 3307, the connection attempt times out or fails with authentication errors depending on how the error is handled by the driver.

The configuration snippet shows:

'host' => 'localhost',
// 'port' => 'nonstandard_port_number', // commented out

If the Bitnami instance is listening on a different port, you must explicitly set it:

'host' => 'localhost',
'port' => 3307, // Adjust based on your actual server configuration

Additionally, Bitnami environments sometimes have strict file permissions or network bindings that restrict access. Disabling the local firewall can sometimes resolve connectivity issues if the application is trying to reach a socket or port blocked by security software. However, the primary fix usually involves aligning the host, port, and user entries precisely with what MySQL reports in SHOW STATUS LIKE 'Uptime' or SHOW VARIABLES.

Troubleshooting Connectivity Flow

When debugging this error, verify the following sequence:

  1. Check User Host: Ensure the user account exists for the exact host string used in the config (localhost vs %).
  2. Verify Port: Confirm the actual port via mysqladmin -u root -p status. Compare it with your app.php config.
  3. Test Direct Connection: Before relying on CakePHP's ORM, test the connection using a raw PHP script or phpMyAdmin to isolate whether the issue is with the driver or the credentials.
<?php
$dsn = 'mysql:host=localhost;dbname=jobs;charset=utf8';
$user = 'test2';
$pass = 'computer';
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];

try {
    $pdo = new PDO($dsn, $user, $pass, $options);
    echo "Connection successful.";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

If this script fails with the same error, the problem is strictly within MySQL's user management or network configuration, not the CakePHP application logic. Once the credentials and host/port match exactly, the bake all command will proceed without throwing a PdoException.

For developers moving between different environments like Bitnami WAMP and production servers, always treat database credentials as environment-specific variables rather than hardcoding them, ensuring flexibility across hosts and ports.

Our network