> ## Content Index
> Fetch the complete content index at: https://www.ggorantala.dev/llms.txt
> Use this file to discover other available public pages before exploring further.

# ERROR 1045 (28000): Access Denied for User — Causes and Fixes
- URL: https://www.ggorantala.dev/mysql-error-1045-access-denied/
- Published: 2026-12-01T19:13:13.000Z
- Updated: 2026-12-01T19:13:13.000Z
- Description: ERROR 1045 with a password you know is right usually means the server matched a different account, or never got the password you typed. How to find out which in two queries, then fix it without handing anyone root.
- Author: Gopi Gorantala
- Tags: mysql-error-1045, access-denied, user-host-accounts, mysql-auth, mysql-connections, mysql, mysql-errors

You've retyped the password four times. You've copied it out of the password manager, pasted it, and got the same thing. Somebody in the channel has already suggested `GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'`, and you're tempted.

Don't. **ERROR 1045, "Access denied for user"**, with a password you know is right almost never means the password is wrong. It means the server compared *something* against *some account*, and one of those two isn't what you think it is.

## The error, and the password you swear is right

```log
ERROR 1045 (28000): Access denied for user 'points_svc'@'localhost' (using password: YES)

```

That's the `mysql` CLI. The format comes from the [MySQL 8.4 server error reference](https://dev.mysql.com/doc/mysql-errors/8.4/en/server-error-reference.html?ref=ggorantala.dev): error 1045, symbol `ER_ACCESS_DENIED_ERROR`, SQLSTATE `28000`, text `Access denied for user '%s'@'%s' (using password: %s)`. No MySQL server would install in my workspace this run (the package mirrors and Docker Hub were both off limits), so that line is the reference format filled in, not my terminal. The lab further down is real; it runs the client side.

Same error, other clients:

```log
pymysql.err.OperationalError: (1045, "Access denied for user 'points_svc'@'172.18.0.5' (using password: YES)")
SQLSTATE[HY000] [1045] Access denied for user 'points_svc'@'localhost' (using password: NO)
Error: Access denied for user 'points_svc'@'10.0.4.31' (using password: YES)   code: 'ER_ACCESS_DENIED_ERROR', errno: 1045

```

PyMySQL, PHP's PDO, Node's `mysql2`. Connector/J hands you an `SQLException` whose `getErrorCode()` is 1045 and `getSQLState()` is `28000`, usually wrapped in a HikariCP "Failed to initialize pool" stack. Reading those three parts of any MySQL error is its own post: [how to read a MySQL error](https://ggorantala.dev/read-mysql-error-message/?ref=ggorantala.dev).

Three facts sit in that one line, and people read one of them. The user name is the one your client *sent*. The host is where the server thinks *you* are, not where the server is. And `using password: YES` or `NO` tells you whether a password arrived at all.

It happens at connect time. The good news hiding in there: TCP worked, the server answered, it read your user name back to you. You're past [ERROR 2002](https://ggorantala.dev/mysql-error-2002-cant-connect-through-socket/?ref=ggorantala.dev) and [ERROR 2003](https://ggorantala.dev/mysql-error-2003-cant-connect-to-server/?ref=ggorantala.dev) territory. If you're not sure which connection error you've got, the [2002 vs 2003 vs 1045 vs 1130 comparison](https://ggorantala.dev/mysql-connection-errors-compared/?ref=ggorantala.dev) maps how far each one gets.

## Which account did the server think you were?

MySQL doesn't log in a user. It logs in a user *at a host*, and `'points_svc'@'localhost'` and `'points_svc'@'%'` are two separate accounts with two separate passwords. Before anything else, from any session that does work on that server:

```sql
SELECT user, host, plugin, account_locked, password_expired
FROM mysql.user
WHERE user IN ('points_svc', '')
ORDER BY user, host;

```

You need `SELECT` on `mysql.user` for that, so run it as an admin account. Then, from a session that logs in fine as the same user from the same place (if you have one), compare what you asked for with what you got:

```sql
SELECT USER(), CURRENT_USER();

```

`USER()` is the name you sent and the host you came from. `CURRENT_USER()` is the row the server actually matched. When those disagree, you've found your bug, and the [user@host accounts post](https://ggorantala.dev/mysql-user-host-accounts/?ref=ggorantala.dev) explains every way they can.

Three quick discriminators while you're here:

- `using password: NO` means your client sent an empty password. The server never saw the one you think you configured. Go straight to the client config.
- `ERROR 1130 ... is not allowed to connect` instead of 1045 means no account matched your host at all. That's [ERROR 1130](https://ggorantala.dev/mysql-error-1130-host-not-allowed/?ref=ggorantala.dev).
- `ERROR 1698` for `root` on Ubuntu or Debian means root uses `auth_socket`, and no password will ever work over TCP. That's [ERROR 1698](https://ggorantala.dev/mysql-error-1698-access-denied-root/?ref=ggorantala.dev).

## A lab that watches what the client actually sends

You can't reproduce a mismatched account without a server, and I didn't have one. What I could do is watch the half of this error that people get wrong most: what the client puts on the wire. So I wrote a 50-line stub. It isn't MySQL. It speaks enough of the protocol to send a MySQL 10 handshake greeting advertising `caching_sha2_password` (the default authentication plugin since 8.0), reads the client's login packet, prints what arrived, and checks the scramble against a password I "stored" the same way the plugin's fast path does: `SHA256(pw) XOR SHA256(SHA256(SHA256(pw)) + nonce)`. Then it hangs up, which is why PHP below reports a gone-away error rather than a 1045\. Ignore PHP's line; the stub's line is the point.

Setup: Ubuntu 24.04, kernel 6.18, Python 3.13.18, PHP 8.3.6 with mysqlnd 8.3.6\. The example is a loyalty-points service logging into a `loyalty` schema as `points_svc`, stored password `Pa$$w0rd!`. Yes, with dollar signs. That's the trap.

The probe:

```php
<?php
// probe.php HOST USER PASSWORD [SOCKET]
mysqli_report(MYSQLI_REPORT_OFF);
[$_, $host, $user, $pass] = $argv; $sock = $argv[4] ?? null;
$m = @mysqli_connect($host, $user, $pass, 'loyalty', 3307, $sock);
printf("php  host=%s pass_bytes=%d -> [%d] %s\n", $host, strlen($pass), mysqli_connect_errno(), mysqli_connect_error());

```

1. Start the stub (`python3 stub.py &`); it listens on `127.0.0.1:3307` and on a Unix socket in the lab directory.
2. Run five logins:

```bash
php probe.php 127.0.0.1 points_svc 'Pa$$w0rd!'        # single quotes: what you meant
php probe.php 127.0.0.1 points_svc ''                 # empty password
php probe.php 127.0.0.1 points_svc "Pa$$w0rd!"        # double quotes: bash gets there first
php probe.php 127.0.0.1 points_svc 'Pa$$w0rd! '       # one trailing space, as from a pasted .env value
echo "double-quoted expands to: Pa$$w0rd!"

```

```log
[tcp 127.0.0.1:3307] user='points_svc' db='loyalty' plugin=caching_sha2_password auth_len=32 -> server would say 'using password: YES', scramble matches stored password: True
[tcp 127.0.0.1:3307] user='points_svc' db='loyalty' plugin=caching_sha2_password auth_len=0 -> server would say 'using password: NO', scramble matches stored password: False
[tcp 127.0.0.1:3307] user='points_svc' db='loyalty' plugin=caching_sha2_password auth_len=32 -> server would say 'using password: YES', scramble matches stored password: False
[tcp 127.0.0.1:3307] user='points_svc' db='loyalty' plugin=caching_sha2_password auth_len=32 -> server would say 'using password: YES', scramble matches stored password: False
double-quoted expands to: Pa341w0rd!

```

Look at the third line. `YES`, a 32-byte scramble, and wrong. In double quotes, bash replaced `$$` with its own process ID, so the client sent `Pa341w0rd!`. Every one of those would have been ERROR 1045 with `using password: YES`, and only one of them involves a mistyped password. The "YES or NO" in the message is decided by one thing: whether the auth response the client sent was empty.

1. The account-matching half, as a model. This is Python, not the server, and it encodes the sort from the [9.7 manual's connection verification page](https://dev.mysql.com/doc/refman/9.7/en/connection-access.html?ref=ggorantala.dev): specific hosts first, `'%'` after them, `''` last, and for the same host a named user before the anonymous one.

```log
sorted: 'root'@'localhost', ''@'localhost', 'points_svc'@'10.0.%', 'points_svc'@'%'
points_svc from localhost  -> CURRENT_USER() = ''@'localhost'
points_svc from 10.0.4.31  -> CURRENT_USER() = 'points_svc'@'10.0.%'
points_svc from 172.18.0.5 -> CURRENT_USER() = 'points_svc'@'%'

```

`points_svc` logging in from the box itself matches the anonymous `''@'localhost'` row, because a literal `localhost` beats `%` before the user name is even considered. The server checks your password against *that* account. It fails. You get 1045 with your own user name in it.

Teardown: kill the stub, delete the socket file and the `.env` you made. Nothing else to undo.

## Start here if you're in a hurry

The server found an account for your user and host, checked the password you sent against that account's, and they didn't match. Either you sent a different password than you think, or the server picked a different account than you think, or you reached a different server than you think. Those three cover nearly every 1045 I've seen.

## How MySQL picks the account, and where I went wrong

When a connection arrives, the server has already sorted every row of `mysql.user` in memory. It walks them in order and takes the first row whose host matches where you came from and whose user is either your name exactly or blank. Blank means "anyone", the anonymous account. The manual's own example is the uncomfortable one: a `jeffrey` connecting from `localhost` matches `''@'localhost'` ahead of `'jeffrey'@'%'`, and `CURRENT_USER()` comes back as `@localhost`.

Only after that does authentication happen, with that row's plugin and that row's stored password. So the account you created and gave a lovely password to was never consulted.

My dead end, from the first time this bit me: I'd created `'app'@'%'`, tested it from my laptop, and it worked. On the database host itself it failed. I reset the password twice. Resetting felt right because the error said "Access denied" and the one thing I'd changed recently was the password. It was a 5.6-era server with the anonymous accounts the old `mysql_install_db` used to create, and `''@'localhost'` was eating every local login. Current `mysqld --initialize` doesn't create anonymous accounts, but servers that were upgraded in place for a decade, and some MariaDB installs, still carry them. Check: `SELECT user, host FROM mysql.user WHERE user = '';`.

The second thing that surprised me, and the reason the host part in the message deserves attention: it's the client's address as the server saw it. Inside Docker, that's the bridge address (`172.18.0.5` above), so `'points_svc'@'localhost'` will never match an app container, however right the password is. You'd get 1130 if no row matched that host; you get 1045 when a `%` row matched and its password is different from the `localhost` row's you've been resetting.

And "a different server than you think" is more common than it sounds. With the `mysql` CLI and PHP, `localhost` means the Unix socket, so `mysql -h localhost -P 3307` ignores the port and lands on whatever MySQL owns the default socket, perhaps a Homebrew install with its own root password, not the container you published on 3307\. My lab showed the transport switch: the same PHP call with `localhost` arrived on the stub's Unix socket, not its TCP port. Run `SELECT @@hostname, @@port, @@version;` on whatever you do get into.

Where's the evidence on the server side? Not in the error log by default. In the [8.4 error log filtering docs](https://dev.mysql.com/doc/refman/8.4/en/error-log-priority-based-filtering.html?ref=ggorantala.dev), `log_error_verbosity` defaults to 2 (errors and warnings), and access-denied messages for new connection attempts are informational, logged only at 3\. That's a dynamic global variable, so you can raise it for an hour without a restart.

What I checked for this post: the 1045 entry in the 8.4 server error reference, the 9.7 connection verification and 8.4 troubleshooting pages, the error log filtering page, the connection control component docs, and every client-side variant I could run through mysqlnd. I didn't run a real server, and I've said where.

## Fixing ERROR 1045 Access denied for user, cause by cause

Turn on the logging first if you're on a server you control, so the server tells you who failed and from where:

```sql
SET GLOBAL log_error_verbosity = 3;   -- needs SYSTEM_VARIABLES_ADMIN; set it back to 2 afterwards

```

**`using password: NO`.** Your client sent nothing. Check the variable your app reads is actually set in that environment (`printenv | grep -i pass` inside the container), and for the `mysql` CLI, that you passed `-p`. Option files can also supply an empty password: `mysql --print-defaults` shows exactly what your option files add, and `mysql --no-defaults -u points_svc -p` takes them out of the picture.

**Wrong password reaching the server.** Shell quoting (`$`, `!`, backticks inside double quotes), trailing whitespace or a CRLF from a Windows-edited `.env`, and Compose interpolation, which treats `$` in `docker-compose.yml` values as a variable unless you write `$$`. A `[client]` section in `~/.my.cnf` or a login path in `~/.mylogin.cnf` quietly overriding what you typed is the CLI version of the same mistake; so is `MYSQL_PWD` left in the environment.

**Anonymous account winning.** Confirm with the `mysql.user` query above, then remove it:

```sql
DROP USER ''@'localhost';   -- removes the anonymous account; check nothing relies on it first (nothing should)

```

`mysql_secure_installation` offers to do the same.

**Your account exists for the wrong host.** Create the one for where the app really runs, scoped to its schema:

```sql
CREATE USER 'points_svc'@'172.18.%' IDENTIFIED BY 'a-real-secret';
GRANT SELECT, INSERT, UPDATE, DELETE ON loyalty.* TO 'points_svc'@'172.18.%';

```

Or reset the one you meant, by its full name: `ALTER USER 'points_svc'@'%' IDENTIFIED BY 'a-real-secret';`. An `ALTER USER 'points_svc'` without a host means `'points_svc'@'%'`, which may not be the account you've been failing against.

**Docker changed nothing.** `MYSQL_ROOT_PASSWORD` and friends are read only when the data directory is empty. Change the env var on an existing volume and the old password stays. That's [its own article](https://ggorantala.dev/mysql-docker-env-vars-ignored/?ref=ggorantala.dev).

**Plugin trouble.** If the client is old or the account uses a plugin the client can't speak, you usually get a different error; [authentication plugin cannot be loaded](https://ggorantala.dev/mysql-authentication-plugin-cannot-be-loaded/?ref=ggorantala.dev) and [Public Key Retrieval is not allowed](https://ggorantala.dev/mysql-public-key-retrieval-not-allowed/?ref=ggorantala.dev) cover those.

**Lost root entirely.** Restarting with `--skip-grant-tables` turns off all authentication. Do it only on a server nobody else can reach, for the few minutes it takes to `ALTER USER`, and restart normally straight after.

What isn't a fix: `GRANT ALL ON *.* TO 'root'@'%'`, or creating any `%` account with every privilege "to rule it out". You'll rule out nothing (privileges aren't checked until after authentication) and you'll keep the hole forever.

## Designing around it

One account per service, created for the network that service actually connects from, with grants on its own schema and nothing else:

```sql
CREATE USER 'points_svc'@'10.0.4.%' IDENTIFIED BY 'from-the-secret-store'
  FAILED_LOGIN_ATTEMPTS 5 PASSWORD_LOCK_TIME 1;
GRANT SELECT, INSERT, UPDATE, DELETE ON loyalty.* TO 'points_svc'@'10.0.4.%';

```

Passwords come from one secret store, mounted as a file rather than interpolated through two layers of shell and YAML. No anonymous accounts, no app ever logging in as root, no `localhost` in an app's connection string. And schema changes run under a separate migration account, so the app account never needs `ALTER` at all.

## Catching it before production does

- Run a login smoke test from the app's own network as part of the deploy (`mysql --no-defaults -h db.internal -u points_svc -p -e 'SELECT CURRENT_USER()'`), and assert the returned account, not only that it connected.
- In CI, create accounts with the same `CREATE USER` scripts you use in production, in a Testcontainers MySQL at the production version.
- Alert on a jump in `Aborted_connects`, which counts failed connection attempts including bad logins. On 9.2 and later, the [connection control component](https://dev.mysql.com/doc/refman/9.7/en/connection-control-component.html?ref=ggorantala.dev) (`INSTALL COMPONENT 'file://component_connection_control';`) adds growing delays after repeated failures; the older connection control plugins are deprecated.
- Keep an audit query that fails the build if `mysql.user` contains a blank user or any app account with `%` and global privileges.

On versions: the 1045 number, symbol, SQLSTATE and message format are the same in 8.0, 8.4 and 9.7\. What moves between versions is around it: `caching_sha2_password` as the default plugin since 8.0, `mysql_native_password` disabled in 8.4 and gone in 9.0, connection control turning into a component in 9.2\. After an upgrade, re-run the `mysql.user` query and look at the `plugin` column before anything else.

## Screenshot this bit

- 1045 means the server answered and rejected you. Your network is fine; stop restarting containers.
- `using password: NO` is a client problem: nothing was sent.
- `SELECT USER(), CURRENT_USER();` shows the account you asked for and the one you got. Different means wrong account.
- An anonymous `''@'localhost'` beats `'you'@'%'` for every local login.
- Never fix it with `'root'@'%'`. Privileges aren't even checked yet.

## Questions that come up about 1045

### How do I fix ERROR 1045 in a Docker MySQL container?

Check whether the volume was initialised with an older password: the image only reads `MYSQL_ROOT_PASSWORD` on an empty data directory. Then check the account's host part, because from another container you arrive from a bridge address like `172.18.0.5`, not `localhost`. Use `ALTER USER` to change the password on an existing volume rather than editing the env var.

### What does "using password: NO" mean?

The client sent an empty password. The server didn't compare anything you typed, because nothing arrived. Look for a missing `-p` flag, an unset environment variable, or an option file that sets an empty password, and test with `mysql --no-defaults`.

### Why can I log in from the MySQL CLI but not from my app?

They're usually not reaching the same account, or not the same server. The CLI on the database host comes in as `localhost` over the Unix socket; the app comes in over TCP from its own IP and may match a `%` account with a different password. Compare `CURRENT_USER()` and `@@port` from each.

### Is it safe to use --skip-grant-tables to fix Access denied?

Only on a server no one else can reach, and only for the minutes it takes to reset the password (run `FLUSH PRIVILEGES` first, or `ALTER USER` refuses to work in that mode). It disables authentication for every connection. Restart without it straight afterwards, and in 8.0 or later use `ALTER USER`, not direct `UPDATE`s on `mysql.user`.