Read the Whole Message
ERROR 1045 (28000): Access denied for user 'app_user'@'localhost' (using password: YES)
- The user: the name the client sent. A typo in the configuration shows up here.
- The host: where the server thinks the connection came from. This is what it matches against the host part of the accounts.
- Using password:
YESmeans a password was sent and did not match;NOmeans none was sent, often because the application's configuration is empty or the client was started without-p.
Then look at the accounts, as an administrator in sudo mysql or sudo mariadb:
SELECT user, host, plugin FROM mysql.user WHERE user = 'app_user';
1. The Password Is Wrong
The most common cause. Test it by hand and type the password at the prompt:
mysql -u app_user -p
If it fails here too, set a new one as an administrator and update the application in the same step:
ALTER USER 'app_user'@'localhost' IDENTIFIED BY 'STRONG_PASSWORD';
Passwords with quotes or a dollar sign are often broken by the way a configuration file or shell reads them. Check that the application loads the value you think it does.
2. The Account Exists for Another Host
The query may show 'app_user'@'localhost' while the message says 'app_user'@'10.0.0.5'. These are separate accounts. Create the one for the host the server saw, with its own grants, as in how to create a MySQL user. Do not change the host to % to make the error go away: that lets the account be tried from anywhere.
3. localhost and 127.0.0.1 Are Not the Same
With -h localhost, or no -h at all, the client uses the Unix socket. With -h 127.0.0.1 it uses TCP. The server can match the two against different accounts, and an application may use one while your test uses the other. Try both forms to see which one fails, and use the same one in the application's configuration as in your test. The address itself is explained in what is localhost.
4. Root Logs In Through the Socket
On Ubuntu's MySQL, root uses auth_socket; on MariaDB, unix_socket. Running mysql -u root as a normal user is then refused, on MySQL often with error 1698. That is the intended setup: run sudo mysql or sudo mariadb. Applications and database tools should never log in as root; give them their own account. If root really has lost its password, see how to reset the MySQL root password.
5. The Login Works but the Database Is Denied
ERROR 1044 (42000): Access denied for user 'app_user'@'localhost' to database 'app'
Error 1044 is not a login failure: the account logged in but has no rights on that database. Check and grant on that database only:
SHOW GRANTS FOR 'app_user'@'localhost';
GRANT SELECT, INSERT, UPDATE, DELETE ON app.* TO 'app_user'@'localhost';
Never fix it with GRANT ALL ON *.*; that gives an application account control of every database on the server.
6. The Client Does Not Support the Login Method
MySQL's default plugin is caching_sha2_password. An old client library may not support it and reports an error about an authentication plugin that cannot be loaded. The fix is a current client library or driver. Switching the account to mysql_native_password is not a long-term answer: it is disabled by default in MySQL 8.4 and removed in 9.0.
Other Checks
- Anonymous accounts: an account with an empty user name can match first and deny you. The security script removes them; check with the full list in how to list users in MySQL.
- Locked account: a locked account is refused even with the right password.
- Wrong server: after a move, the application may connect to the old one.