Understanding the Error
ERROR 1045 (28000): Access denied for user 'username'@'localhost' (using password: YES)
Fix 1: Reset Root Password
Stop MySQL:
sudo systemctl stop mysql
Start in safe mode (skip grants):
sudo mysqld_safe --skip-grant-tables &
Connect without password:
mysql -u root
Reset password:
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPassword';
# For MySQL 5.7 and older:
UPDATE mysql.user SET authentication_string=PASSWORD('NewStrongPassword') WHERE User='root';
Exit and restart normally:
sudo systemctl restart mysql
Fix 2: Check Plugin Authentication
For MySQL 8.0+, some clients don't support caching_sha2_password:
ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Fix 3: Verify User Exists and Host
SELECT User, Host FROM mysql.user;
Connect as user@host properly:
# If user exists for localhost only
mysql -u username -p -h localhost
# If user exists for specific IP
mysql -u username -p -h 127.0.0.1
Fix 4: Grant Missing Privileges
GRANT ALL PRIVILEGES ON database.* TO 'username'@'localhost';
FLUSH PRIVILEGES;
Fix 5: Check Password Expiration
SELECT User, password_expired FROM mysql.user;
ALTER USER 'user'@'host' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;