1045
Fix MySQL ERROR 1045 Access Denied
MySQL ERROR 1045 occurs when authentication fails due to incorrect credentials, host restrictions, or plugin issues. This guide covers root causes and step-by-step fixes.
Symptoms
When connecting to a MySQL database, you receive the following error:
ERROR 1045 (28000): Access denied for user 'username'@'host' (using password: YES/NO)This error may appear in command-line clients, application logs, or during installation scripts. Common scenarios include:
- Cannot log in as root or any user
- Application fails to connect to the database
- Password reset attempts fail
Root Causes
The error indicates that MySQL's authentication system rejected the connection. Primary causes include:
- Incorrect password: The password supplied does not match the stored hash.
- Host mismatch: The user is restricted to specific hosts (e.g., 'localhost' vs '%').
- Authentication plugin mismatch: Client does not support the server's plugin (e.g., caching_sha2_password vs mysql_native_password).
- Corrupted user table: The mysql.user table is damaged or has inconsistent data.
- Empty password: No password set but client sends one, or vice versa.
Step-by-Step Fix
Prerequisites
- Access to the server where MySQL runs
- Root or sudo privileges
Method 1: Reset Root Password (Skip Grant Tables)
- Stop MySQL service:
sudo systemctl stop mysql - Start MySQL in safe mode without privilege checks:
sudo mysqld_safe --skip-grant-tables &Or on some systems:
sudo mysqld --skip-grant-tables --skip-networking & - Connect without password:
mysql -u root - Flush privileges to reload grant tables:
FLUSH PRIVILEGES; - Reset password (choose one):
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPassword123!';For older MySQL versions:
UPDATE mysql.user SET authentication_string=PASSWORD('NewStrongPassword123!') WHERE User='root'; - Flush and exit:
FLUSH PRIVILEGES;
EXIT; - Stop safe mode and restart normally:
sudo killall mysqld
sudo systemctl start mysql
Method 2: Check Host and User
- Connect with correct host:
mysql -u username -p -h localhostTry '127.0.0.1' or the server IP.
- List users and hosts:
SELECT User, Host FROM mysql.user; - If the user@host combination is missing, create it:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'username'@'host';
FLUSH PRIVILEGES;
Method 3: Update Authentication Plugin
- Connect with root (using skip-grant-tables if needed).
- Change plugin to mysql_native_password:
ALTER USER 'username'@'host' IDENTIFIED WITH mysql_native_password BY 'password'; - Flush privileges.
Alternative Fixes
- Reinstall MySQL (last resort): Backup databases, purge and reinstall MySQL, then restore.
- Use mysql_secure_installation to set root password interactively.
- Check config file (my.cnf) for any skip-grant-tables or bind-address settings that may interfere.
Prevention
- Always use strong passwords and store them securely.
- Use specific hosts instead of wildcard '%' for production users.
- Keep MySQL updated to avoid plugin compatibility issues.
- Regularly audit user accounts and privileges.
- Backup mysql.user table before making changes.
By following these steps, you can resolve MySQL ERROR 1045 and restore database access. For persistent issues, check MySQL error logs for additional details.
Was this solution helpful?