Quick answer: Stop MySQL service, start it with --skip-grant-tables, connect without password, flush privileges, set new password with ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass';, then restart normally.
This error tripped me up the first time too. You're trying to log into MySQL, you type your password carefully, and boom — “Access denied for user 'root'@'localhost' (using password: YES)”. It's not just you. This happens a lot when MySQL uses a different authentication plugin than what your client expects. For example, MySQL 8.0 changed to caching_sha2_password, but older tools like PHP 5.6 or some phpMyAdmin versions still use mysql_native_password. Also, if you're on Ubuntu and used sudo apt install mysql-server, the root account often uses auth_socket — meaning it only logs in via sudo, not with a password at all.
Fix Steps — The Main Fix
- Stop the MySQL service. On Ubuntu or Debian:
sudo systemctl stop mysql. On Windows (as admin):net stop MySQL80(adjust version). - Start MySQL without grant tables. This bypasses authentication. Run:
sudo mysqld_safe --skip-grant-tables &(Linux) ormysqld --skip-grant-tables(Windows command prompt as admin). - Connect without a password. Open another terminal and type:
mysql -u root. No password needed now. You should see the MySQL prompt. - Flush privileges. Inside MySQL, run:
FLUSH PRIVILEGES;. This tells the server to reload the grant tables. - Reset the password. For MySQL 8.0+, use:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewStrongPassword';. For older MySQL (5.x), use:SET PASSWORD FOR 'root'@'localhost' = PASSWORD('YourNewStrongPassword');. - Flush again. Run
FLUSH PRIVILEGES;one more time. - Exit MySQL with
EXIT;. - Restart MySQL normally. Stop the skip-grant instance:
sudo killall mysqld(Linux) or find the process in Task Manager (Windows). Then start:sudo systemctl start mysql(Linux) ornet start MySQL80(Windows). - Test. Now connect:
mysql -u root -p, then type your new password. It should work.
Alternative Fix — Auth Plugin Change
If the main fix didn't help, or you keep getting the error even with the right password, the problem is likely the authentication plugin. Your MySQL user might be set to auth_socket or caching_sha2_password, but your client expects the old mysql_native_password. Here's how to change it:
- Repeat steps 1-4 from the main fix to get into MySQL without password.
- Check what plugin your user uses:
SELECT user, plugin FROM mysql.user WHERE user = 'root';. If it saysauth_socketorcaching_sha2_password, you need to switch it. - Change the plugin and password together:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourNewStrongPassword';. This forces MySQL to use the old native password system. - Flush and restart as before.
Special case for Ubuntu: If the user shows auth_socket, you can keep it but only log in with sudo. That means: sudo mysql -u root (no password). It's annoying but some Linux distros do this by design.
Prevention Tips
- Use the right auth plugin for your apps. If you're building a PHP app that runs on PHP 5.x or 7.0-7.2, always set your MySQL user to
mysql_native_password. PHP's old mysqlnd library doesn't supportcaching_sha2_password. - Don't use root for apps. Create a dedicated user:
CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'mypass';then grant specific privileges. Less risk of locking yourself out. - Keep passwords in a manager. I've seen admins type the wrong password for an hour. Use a password manager or store them in a secure file (with proper permissions).
- Backup grant tables. Before making big changes, run:
mysqldump --all-databases --flush-privileges > backup.sql. If you mess up, you can restore.
I know this error is infuriating, but trust me — the skip-grant-tables trick is like a master key for MySQL. Once you've done it a couple times, it becomes second nature. Good luck, and feel free to drop a comment if something's still broken.