MySQL and MariaDB return error 1045 when the server was reached but refused the login. The network path works, so this is an account problem: a wrong password, a user that does not exist for your client's host, or an authentication plugin your client cannot use.
ERROR 1045 (28000): Access denied for user 'app'@'203.0.113.7' (using password: YES)
Read the message closely. The part in quotes is the user and the host the server saw, and using password: YES or NO tells you whether the client sent a password at all. NO usually means your app config or environment variable is empty.
How MySQL identifies an account
An account is a pair: 'user'@'host'. The accounts 'app'@'localhost' and 'app'@'%' are different rows with different passwords and privileges. The server picks the most specific host match for the address your client connects from, so a connection from 203.0.113.7 can be matched against a different row than you expect. If the host in the error message is not one you created, that is your answer.
Fix, in this order
1. Confirm the credentials and where they come from
Test with the plain client, bypassing your application:
mysql -h 127.0.0.1 -P 3306 -u app -p
If this works but the app fails, the app has a different password, a stale .env, or special characters that are mangled. Passwords containing @, #, / or % must be percent-encoded inside a URL such as DATABASE_URL.
Also note that -h localhost uses the Unix socket while -h 127.0.0.1 uses TCP, and these can match different accounts.
2. Look at the accounts that exist
Log in as an admin and list them:
SELECT user, host, plugin FROM mysql.user WHERE user = 'app';
SHOW GRANTS FOR 'app'@'203.0.113.7';
If there is no row for the client's host, create one:
CREATE USER 'app'@'203.0.113.7' IDENTIFIED BY 'a-strong-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'app'@'203.0.113.7';
GRANT takes effect immediately; FLUSH PRIVILEGES is only needed if you edited the mysql.* tables directly. Use the What Is My IP tool to learn the public address your app server connects from, since NAT or a cloud gateway may change it.
3. Reset a password you no longer know
ALTER USER 'app'@'%' IDENTIFIED BY 'new-strong-password';
On MariaDB older than 10.2 use SET PASSWORD FOR ... = PASSWORD('...'). Then update the application configuration and restart it, since pooled connections may hold the old credentials.
4. Check the authentication plugin
MySQL 8.0 defaults to caching_sha2_password. Old client libraries, including some PHP builds, old connectors and tools, fail against it. The error may be 1045 or a message about an unknown authentication method. Upgrade the client, or for one legacy account only:
ALTER USER 'app'@'%' IDENTIFIED WITH mysql_native_password BY 'new-strong-password';
Recent MySQL releases deprecate mysql_native_password and newer versions may disable it by default, so check the release notes for your version before relying on it.
MariaDB's root account on many Linux packages uses unix_socket authentication. That means sudo mysql works while mysql -u root -p is denied, regardless of the password. Create a dedicated admin user with a password for remote and app use rather than changing root.
5. Recover a lost root password
Stop the server, then start it without grant checks and only on the local machine:
sudo systemctl stop mysql
sudo mysqld --skip-grant-tables --skip-networking &
mysql -u root
Inside the shell, run FLUSH PRIVILEGES; first (so account management statements are allowed), then ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password';. Stop that instance and start the service normally. Never leave --skip-grant-tables in a config file, because it lets anyone in.
Common traps
- A trailing newline or space in a password file or secret.
- Docker:
MYSQL_ROOT_PASSWORDandMYSQL_USERonly apply when the data directory is empty on the first start. Changing them later does nothing to an existing volume. - Managed databases: your user may exist only for specific hosts, or require TLS. Check the provider's console.
- Using
rootfrom an app. Create a least-privilege user per application.
अगले कदम
- If you get a different message about the host, read MySQL error 1130: host not allowed to connect.
- If the server cannot be reached at all, see MySQL error 2002.
- Check that port 3306 is reachable with the पोर्ट चेकर.
- For a WordPress site, follow Error establishing a database connection.
- Run the वेबसाइट हेल्थ चेक to see whether the site is failing for visitors.
सामान्य प्रश्न
What is the difference between error 1045 and error 1130?
1045 means a matching account was found but the credentials or plugin failed. 1130 means no account matches the client's host at all.
Why does it work in the terminal but not in my app?
The app is probably using a different host, password, or socket. Compare the exact user and host in the error message with the account you tested.
Is FLUSH PRIVILEGES required after CREATE USER?
No. CREATE USER, ALTER USER and GRANT reload the privilege tables themselves. It is only needed after direct edits to the mysql schema tables.
Can a wrong password lock me out?
Only if the account is configured with a failed-login lock, which is optional in MySQL 8.0. By default you can retry, but fail2ban or a firewall may block repeated attempts.
Get notified when this fix updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.