Quick answer: What does SQLSTATE[HY000] [1045] mean?

MySQL error 1045 means access was denied for the supplied database credentials or account context. In PHP PDO, an error such as SQLSTATE[HY000] [1045] Access denied for user 'db_user'@'localhost' usually points to the wrong username, password, host, authentication setup or permissions. It does not automatically mean your PHP syntax is wrong.

On shared hosting, the database name and username often include an account prefix. Copy them from the hosting database manager instead of guessing.

Check these five settings in your hosting panel

  1. Database name: Does the name exactly match the one created in your panel?
  2. Database user: Is the correct user assigned to that database?
  3. Password: Was the password reset without updating the PHP configuration?
  4. Database hostname: Does the host require localhost, 127.0.0.1 or a separate server address?
  5. User privileges: Does this user have the database permissions needed by the application?

In control panels such as Hostinger or cPanel, check the database user assignment and host details in the provider's own database section. A 1045 error is different from a missing database or missing table error.

Safe PDO connection example

Use the settings provided by your host. Store secrets outside the public web directory or use environment variables; never paste real passwords into a blog or send them to a client-side script.

<?php
$host = getenv('DB_HOST') ?: 'localhost';
$db   = getenv('DB_NAME');
$user = getenv('DB_USER');
$pass = getenv('DB_PASS');
try {
    $pdo = new PDO(
        "mysql:host={$host};dbname={$db};charset=utf8mb4",
        $user,
        $pass,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );
} catch (PDOException $e) {
    error_log('Database connection failed: ' . $e->getCode());
    http_response_code(503);
    exit('Database temporarily unavailable.');
}

Make sure the environment variables are actually configured on your server. This example deliberately hides internal database details from website visitors.

What if localhost works in one PHP file but not another?

Compare the configuration included by both files. A wrong relative require path may load an old configuration, or a deployment may reference a different account. Also verify that CLI PHP and web PHP are using the same environment and credentials.

Do not make the database user a global administrator just to clear an error. Apply only the permissions required for the application. PHP's PDO constructor manual documents the connection parameters.

How to separate error 1045 from similar errors

Error 1045: the database server was contacted but rejected access for the supplied user or authentication context. Check account credentials and privileges.

Error 1049: the named database may not exist. Check its exact name and whether the user has access.

Connection refused or error 2002: the client may be unable to reach the MySQL service at the selected host or socket. Check the server address and service availability.

These errors require different fixes. Before contacting hosting support, note the time, host value (not the secret password) and exact error code. Never share your password in support screenshots or publicly accessible log files.

Frequently asked questions

Should I replace localhost with 127.0.0.1?

Only if your hosting provider specifies it or testing shows a host/socket difference.

Will changing the PHP version fix error 1045?

Usually not; first verify credentials and database account setup.

Can I show the full PDO exception on my live website?

No. Log errors securely and show visitors a generic message to avoid exposing account details.

For clean PHP deployment, see MTP DigiTech web development services and our dynamic PHP sitemap tutorial.

NEED HELP?

Need help with PHP or technical SEO?

MTP DigiTech builds PHP websites with clean URLs, secure server configuration and crawlable pages.

Talk to a Developer →