Fix MySQL ERROR 1045 Access denied for user (using password: YES)
MySQL ERROR 1045 (using password: YES) is not always a wrong password. Match 'user'@'host' to mysql.user and rule out the shell or an option file.
ERROR 1045 (28000): Access denied for user 'app'@'localhost' (using password: YES)No account on the server matches the user name, client host and password you sent. A wrong password is the usual cause, but the same message appears when the account exists only for another host or when the shell mangled the password. Compare the 'user'@'host' in the error with mysql.user and the cause shows.
[1] Read three things from the message. Per the official docs it tells you who you tried to log in as, from which client host, and whether a password was sent (Troubleshooting Problems Connecting to MySQL).
| Part | Seen in testing | Meaning |
|---|---|---|
'app' | User name sent | Typos, letter case |
@'localhost' · @'127.0.0.1' | Client host as the server saw it | Socket gives localhost, TCP gives the address |
using password: YES / NO | Was a password sent | NO against an account that needs one is still 1045 |
If you get NO but think you gave a password, check for a space between -p and the value. The docs state there is no space between -p and the password.
[2] Check that an account row exists for that host. Log in as an admin and list user and host together:
$ mysql -uroot -p'<password>' -e "SELECT user, host, plugin FROM mysql.user WHERE user IN ('app','') ORDER BY host"mysql: [Warning] Using a password on the command line interface can be insecure.user host pluginapp localhost caching_sha2_passwordapp exists only for localhost. With the same password, the socket login works and a TCP login with -h127.0.0.1 is refused:
$ mysql -uapp -p'<password>' -e 'SELECT USER(), CURRENT_USER()'mysql: [Warning] Using a password on the command line interface can be insecure.USER() CURRENT_USER()app@localhost app@localhost$ mysql -uapp -p'<password>' -h127.0.0.1 -e 'SELECT 1'mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user 'app'@'127.0.0.1' (using password: YES)This server runs with skip_name_resolve on (@@skip_name_resolve is 1, set in the official Docker image's /etc/my.cnf), so 127.0.0.1 is not turned into localhost. From another container or app server, the error shows that client's address:
$ mysql -h db -uapp -p'<password>' -e 'SELECT 1'mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user 'app'@'192.0.2.10' (using password: YES)[3] Create the account for that host. In MySQL a different host part is a different account, with its own password and grants. '%' means any host, so narrow it to the app server address or subnet ('192.0.2.%') where you can.
mysql> CREATE USER 'app'@'%' IDENTIFIED BY '<password>';mysql> GRANT ALL ON appdb.* TO 'app'@'%';$ mysql -uapp -p'<password>' -h127.0.0.1 -e 'SELECT USER(), CURRENT_USER()'mysql: [Warning] Using a password on the command line interface can be insecure.USER() CURRENT_USER()app@127.0.0.1 app@%USER() is the name and host you sent; CURRENT_USER() is the account row the server picked. When they differ, this is where you see which row won.
[4] If the account is right and the password still fails, suspect the shell. A $ in an unquoted password gets expanded as a variable. The test password below is Ab$cd12:
$ mysql -uapp -pAb$cd12 -e "SELECT 1"mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user 'app'@'localhost' (using password: YES)$ echo -pAb$cd12-pAb$ mysql -uapp -p'Ab$cd12' -e 'SELECT 1 AS ok'mysql: [Warning] Using a password on the command line interface can be insecure.ok1Wrap it in single quotes, or pass only -p and type it at the prompt. !, spaces and \ are also shell-dependent, so scripts should use an option file.
[5] If you passed no password and still see YES, an option file supplied one. The docs point to a wrong password in an option file for this case and suggest testing with --no-defaults. Look first with --print-defaults:
$ mysql -e 'SELECT 1'ERROR 1045 (28000): Access denied for user 'app'@'localhost' (using password: YES)$ mysql --print-defaultsmysql would have been started with the following arguments:--socket=/var/run/mysqld/mysqld.sock --user=app --password=*****$ mysql --no-defaults -uapp -p'<password>' -S /var/run/mysqld/mysqld.sock -e 'SELECT 1 AS ok'mysql: [Warning] Using a password on the command line interface can be insecure.ok1If --no-defaults works, fix password= in ~/.my.cnf or whichever option file sets it.
[6] Check for an anonymous account. With ''@'localhost' (empty user name) present, a localhost login picks that row over 'app2'@'%' and refuses even the correct password:
$ mysql -uroot -p'<password>' -e "SELECT user, host FROM mysql.user WHERE user IN ('app2','') ORDER BY host"mysql: [Warning] Using a password on the command line interface can be insecure.user hostapp2 % localhost$ mysql -uapp2 -p'<password>' -e 'SELECT 1'mysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user 'app2'@'localhost' (using password: YES)The docs explain that the row with the more specific Host is used first, and the fix is a separate localhost row for the user or deleting the anonymous row. In the test, after DROP USER ''@'localhost'; the same login came in as app2@%.
[7] If you still cannot see the cause, have the server log failures. At the default log_error_verbosity of 2 nothing was logged; at 3 each failure appeared as a [Note]:
mysql> SET GLOBAL log_error_verbosity = 3;2026-10-27T08:04:24.078549Z 19 [Note] [MY-010926] [Server] Access denied for user 'app'@'localhost' (using password: YES)2026-10-27T08:04:24.201399Z 20 [Note] [MY-010926] [Server] Access denied for user 'app'@'127.0.0.1' (using password: YES)2026-10-27T08:04:24.303171Z 21 [Note] [MY-010926] [Server] Access denied for user 'app'@'localhost' (using password: NO)The host the server saw is more reliable than what the app reports. Set it back with SET GLOBAL log_error_verbosity = 2; when done; level 3 also logs informational messages and the log grows fast.
To set a new password, run ALTER USER 'app'@'%' IDENTIFIED BY '<new password>'; as an admin, with the exact host part so the right row changes. For a forgotten root password, follow the official How to Reset the Root Password.
If only old accounts fail after moving to 8.4 with Plugin 'mysql_native_password' is not loaded, see MySQL 8.4 mysql_native_password is not loaded. For 1698 with no password suffix, see MySQL ERROR 1698 Access denied for root. If you cannot reach the server at all, see MySQL ERROR 2002 Can't connect through socket.
Analyze your MySQL slow log log in the browser ↗ — nothing is uploaded.