MySQL error 1045: Access denied for user
MySQL rejected the user name, host and password combination. Usually a wrong password or no 'user'@'host' account matching the host you connect from.
- Error number
1045- Name
ER_ACCESS_DENIED_ERROR- SQLSTATE
28000- Category
- Access and privileges
What it means
A MySQL account is a user name plus the host it connects from: 'app'@'localhost' and 'app'@'%' are two different accounts with their own passwords. When you connect, the server picks the most specific account matching your user name and client host, then checks the password. If no account matches, or the password is wrong, you get 1045.
(using password: YES) means a password was sent and was wrong or the account doesn't exist; (using password: NO) means the client sent no password at all.
Common causes
- Wrong password, or a password with special characters mangled by the shell or a connection string.
- The account exists for a different host.
'app'@'localhost'doesn't match a connection from another machine, and a more specific anonymous account (''@'localhost') can win over'app'@'%'. (using password: NO): the client didn't send the password, for example-pwas missing or an option file overrode it.- On Ubuntu and Debian,
rootuses socket authentication (auth_socket) and can only log in as the operating-system root user, without a password. - The account was created with an authentication plugin the client library doesn't support.
How to fix it
-
Connect as an administrator and list the accounts for that user name and their hosts.
SELECT user, host, plugin FROM mysql.user WHERE user = 'app';
-
Reset the password for the exact account (user and host) you connect as.
ALTER USER 'app'@'%' IDENTIFIED BY 'new-strong-password';
-
If no account covers your client host, create one and grant it what it needs.
CREATE USER 'app'@'10.0.0.%' IDENTIFIED BY 'strong-password'; GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'app'@'10.0.0.%';
-
Check which account the server matched you to. After you can connect,
CURRENT_USER()shows the account used andUSER()the name and host you sent.SELECT USER(), CURRENT_USER();
-
If you lost the root password entirely, restart the server with
--skip-grant-tables --skip-networking, connect, runFLUSH PRIVILEGES;thenALTER USERto set a new password, and restart normally.
Questions
What does using password: NO mean?
The client didn't send any password. Pass one (mysql -u app -p), or check that your option file or connection string actually includes it.
Why can I log in locally but not from another machine?
MySQL accounts are per host. You probably have 'user'@'localhost' but no account matching the remote host, such as 'user'@'%'. Also check that the server listens on a network interface (bind_address).
Related errors
1044Access denied for user to database1130Host is not allowed to connect1251Client doesn't support auth protocol1698Access denied for user (no password)2059Authentication plugin cannot be loaded18456SQL Server: Login failed for user28P01PostgreSQL: password authentication failedORA-01017Oracle: invalid username/password; logon denied
More access and privileges errors
1142Command denied to user for table1143Command denied to user for column1227Access denied; you need a privilege1396Operation CREATE USER failed1419SUPER privilege needed with binary logging1449Definer user does not exist
The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.
Work on MySQL in SQLly
A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for MySQL and the other databases you use.