MySQL error 1045 access denied: read the message, fix the cause
MySQL error 1045 (Access denied for user 'x'@'y') looks like a password problem, but the message itself carries three clues that point to the real cause. This guide reads the message the way the official MySQL reference manual does.
Read the message first
Per the MySQL manual, the error tells you: who you are trying to log in as, the client host you are connecting from, and whether a password was used. using password: NO means you tried to log in without a password at all. Normally the user table on the server should have one row that exactly matches the host name and user name in the error message.
Checklist, in order
- Are you passing a password? If the message says
using password: NOand the account has one, you forgot-por your config file is not being read. - Wrong password? With
using password: YESand a matching account, the password is the first suspect. - Does a matching account exist? Check what the server knows:
SELECT User, Host FROM mysql.user;
The result should include a row whose Host and User match your client host and MySQL user name. - Connecting from another machine? An error like
Host ... is not allowed to connect to this MySQL servermeans no user table row has a Host value matching your client host. Fix it by creating an account for the client-host plus user-name combination you actually use. - Localhost quirks:
localhostis special; using--host=127.0.0.1forces a TCP/IP connection to the local server, and the host name must then appear in a user table row even though you are on the same machine.
Fixes that follow from the diagnosis
- No matching row: create the account with the exact
'user'@'host'pair the error reported. - Unknown or changing client host: the manual notes you can create a row with
'%'as the Host value, which matches any host. Treat that as a last resort and protect it with a strong password, since it opens the account to every machine. - After an upgrade: run the MySQL upgrade procedure; the grant table structure changes between versions and stale tables cause login failures.
Common mistakes
- Resetting the password when the real problem is a missing
'user'@'host'row. - Testing only from the server itself, then being surprised when the app server's host is not granted.
- Granting
'user'@'%'to silence the error without understanding what it opens.
Running MySQL behind nginx? See fixing nginx 502 Bad Gateway for the layer in front. If you would rather hand database and server care to us, see hosting and maintenance.
