What the message looks like
ERROR 1045 (28000): Access denied for user 'app'@'localhost' (using password: YES)Common causes
- The password is wrong or was changed, or it contains characters the shell or a config file mangles.
- The user exists only for another host:
'app'@'%'is not the same as'app'@'localhost'. - On MariaDB the
rootaccount uses theunix_socketplugin: it works withsudo mysqlbut not with a password. - The application config still holds the old credentials after a migration or restore.
- Reverse DNS or
skip-name-resolvechanges which host entry matches.
Check
- Which users and hosts exist, and with which plugin?
sudo mysql -e "SELECT user, host, plugin FROM mysql.user;" - What can this user do?
sudo mysql -e "SHOW GRANTS FOR 'app'@'localhost';" - Test the login over TCP with a prompt for the password
mysql -u app -p -h 127.0.0.1 -P 3306
Fix
- Set a new password:
ALTER USER 'app'@'localhost' IDENTIFIED BY 'new-password';and use it in the application. - Create the user for the host that actually connects:
CREATE USER 'app'@'192.0.2.10' IDENTIFIED BY '…';followed byGRANT … ON db.* TO …. - For
rooton MariaDB, log in withsudo mysqlinstead of a password. - Update the credentials in the application's configuration and restart the application.
- Store passwords with special characters in quotes, or choose one without
$,!and#.
Typical scenario
You moved a WordPress site to a new server and restored the database from a dump. The site shows "Error establishing a database connection", and the log has error 1045 for the user in wp-config.php. The dump contains tables only, not users, because those live in the system database mysql. First test the same credentials with mysql -u USER -p to separate the server from the application.
FAQ
I forgot the root password. What now?
On MariaDB, sudo mysql usually works without a password. Otherwise restart the server briefly with --skip-grant-tables while it is not reachable from the network, set a new password and restart normally.
Is the error the same as 2002?
No. With 1045 the server answered and said no. With 2002 the client did not reach any server.
What does "using password: NO" in the message mean?
The client did not send any password at all. Typical causes are an empty value in the application config, an unset environment variable, or an option file the program never read. The fault is then not on the server but wherever the credentials are supplied.
Why does the password work in the console but not in the application?
Often the application connects from a different address or over TCP, and no user exists for that host. Special characters that get altered in config files or URLs are another candidate. Compare the user and host in the error with SELECT user, host FROM mysql.user.
Can a missing privilege on a database cause error 1045?
No. 1045 is about the login itself. If the login works but the user may not use a database, you get a different message, such as 1044 "Access denied … to database". For 1045 check password, user name, host and authentication plugin, not the GRANTs.