Error
Error Code: 1045

MySQL Error 1045: Access Denied

📦 MySQL
📋

Description

MySQL Error 1045 indicates a failure to authenticate or connect to the MySQL server. This error typically occurs when the provided username, password, or connecting host does not match the server's security configuration, preventing the user from establishing a session.
💬

Error Message

Access denied for user '%s'@'%s' (using password: %s)
🔍

Known Causes

3 known causes
⚠️
Incorrect Credentials
The username or password supplied in the connection attempt is incorrect or does not match any active user account on the MySQL server.
⚠️
Unauthorized Host
The MySQL user account is not configured to allow connections from the specific host or IP address from which the connection is being initiated.
⚠️
Non-Existent User
The specified username does not correspond to any defined user account within the MySQL server's user management system.
🛠️

Solutions

4 solutions available

1. Verify Username and Password easy

Double-check your credentials are correct

1
Test connection with mysql client
mysql -u your_username -p -h localhost
2
Check if user exists in MySQL
SELECT User, Host FROM mysql.user WHERE User = 'your_username';
3
Reset password if forgotten
ALTER USER 'your_username'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;

2. Fix Host-Based Access medium

Configure correct host permissions for the user

1
Check which hosts are allowed
SELECT User, Host FROM mysql.user WHERE User = 'your_username';
2
Create user with wildcard host access
CREATE USER 'your_username'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'your_username'@'%';
FLUSH PRIVILEGES;
3
Or create user for specific IP range
CREATE USER 'your_username'@'192.168.1.%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database_name.* TO 'your_username'@'192.168.1.%';
FLUSH PRIVILEGES;

3. Fix Authentication Plugin (MySQL 8.0+) medium

Switch from caching_sha2_password to mysql_native_password

1
Check current authentication plugin
SELECT User, Host, plugin FROM mysql.user WHERE User = 'your_username';
2
Change to mysql_native_password for compatibility
ALTER USER 'your_username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
3
Set default auth plugin for new users (my.cnf)
[mysqld]
default_authentication_plugin=mysql_native_password

4. Reset Root Password (Emergency) advanced

Recover access when root password is lost

1
Stop MySQL service
sudo systemctl stop mysql
2
Start MySQL in safe mode
sudo mysqld_safe --skip-grant-tables &
3
Connect without password and reset
mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;
exit;
4
Restart MySQL normally
sudo systemctl restart mysql
🔗

Related Errors

5 related errors