MySQL Error 1045 — Access Denied for User

Fix MySQL Error 1045 (Access denied for user). Covers password reset, GRANT privileges, authentication plugin changes, and common connection string issues.

MySQL Error Codes

Detailed Explanation

MySQL Error 1045: Access Denied

Error 1045 means MySQL recognized the connection attempt but rejected the authentication. This is the most common MySQL connection error.

Error Format

ERROR 1045 (28000): Access denied for user 'myuser'@'localhost' (using password: YES)

Understanding user@host

MySQL permissions are tied to both the username and the host the client connects from. 'myuser'@'localhost' and 'myuser'@'%' are different accounts.

-- Check existing users
SELECT user, host, plugin FROM mysql.user;

-- Grant access from any host
CREATE USER 'myuser'@'%' IDENTIFIED BY 'password';
GRANT ALL ON mydb.* TO 'myuser'@'%';
FLUSH PRIVILEGES;

Common Causes

1. Wrong password:

-- Reset password
ALTER USER 'myuser'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;

2. Authentication plugin mismatch: MySQL 8.0+ defaults to caching_sha2_password, which older clients may not support.

-- Switch to mysql_native_password for compatibility
ALTER USER 'myuser'@'localhost'
IDENTIFIED WITH mysql_native_password BY 'password';

3. Connecting from wrong host: If you created 'myuser'@'localhost' but connect from a Docker container, you are connecting from the container's IP, not localhost.

4. Password in connection string issues: Special characters in passwords (like @, #, %) must be URL-encoded in connection strings:

mysql://user:p%40ssw0rd@host:3306/db

Recovery: Reset Root Password

If you are locked out of root:

# 1. Stop MySQL
sudo systemctl stop mysql

# 2. Start with skip-grant-tables
sudo mysqld_safe --skip-grant-tables &

# 3. Connect without password
mysql -u root

# 4. Reset password
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

# 5. Restart normally
sudo systemctl restart mysql

Use Case

MySQL 1045 is the first error most developers encounter when setting up a new database connection. It is especially common during initial project setup, Docker-based development environments, and after MySQL version upgrades that change the default authentication plugin. Understanding the user@host model and authentication plugins prevents hours of frustration.

Try It — Error Code Reference

Open full tool