Resolving MySQL Access Denied for User root@localhost Login Issue

So, you’re trying to log into MySQL using the root user, and bam! You get hit with this:

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

Annoying, right? Don’t worry. You’re not alone, and better yet, this article will help you fix it. Let’s walk through it step by step—in a fun, easy way!

What’s Happening Here?

This message is MySQL’s way of saying “Nope, you’re not getting in!”. But why? Well, there are a few usual suspects:

  • Wrong password
  • MySQL server not running
  • User privileges messed up
  • MySQL not using the correct authentication plugin

Let’s crack the case and get you back inside.

Step 1: Check the Obvious Stuff

Before diving deep, check these first. You’ll be surprised how often they’re the cause:

  • Are you using the correct password? Might be worth a recheck (or maybe even a reset).
  • Is the MySQL service running? Try this command:
sudo systemctl status mysql

If it’s not active, start it with:

sudo systemctl start mysql

Still getting the error? Let’s dig deeper.

Step 2: Try Logging In Without a Password (For Testing)

If you’re on your development machine where security is less of a concern, test this:

sudo mysql -u root

This uses sudo to skip password stuff entirely. If this works, the issue is related to password authentication or user rights.

If not, fear not. We have more tricks up our sleeves.

Step 3: Resetting the Root Password (Safely!)

This is where the real fun begins. 😄

You’re going to start MySQL in safe mode—basically, MySQL with super chill rules.

Ready? Let’s go:

  1. Stop MySQL service:
sudo systemctl stop mysql
  1. Start MySQL in safe mode without password restriction:
sudo mysqld_safe --skip-grant-tables &
  1. Log in to MySQL:
mysql -u root

You should be inside now. No password checks here. Sweet!

Reset the Password

Now run these commands to set a new password:


USE mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourNewPassword';
FLUSH PRIVILEGES;

Replace YourNewPassword with something secure (and easy to remember!).

Then exit MySQL:

exit

And restart MySQL normally:

sudo systemctl restart mysql

Now try logging in again:

mysql -u root -p

It’ll ask for the password. Enter the new one you just set. Fingers crossed!

Step 4: Still Locked Out? Check the Authentication Plugin

MySQL 5.7 and above sometimes uses a plugin called auth_socket. It’s useful… but confusing.

To see which plugin your root account is using, log into MySQL and run:


SELECT user, host, plugin FROM mysql.user;

If you see auth_socket, here’s what that means: MySQL only lets root in if you’re coming from the local system using the system’s root user, not just knowing the password.

You can change it back to password-based login like this:


ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourNewPassword';
FLUSH PRIVILEGES;

Now your root can log in using just a password. Much simpler!

Step 5: Creating a New Admin User (Optional But Handy)

If things keep breaking, you might want a backup plan—a second admin user.

Once in MySQL, do this:


CREATE USER 'newadmin'@'localhost' IDENTIFIED BY 'StrongPassword';
GRANT ALL PRIVILEGES ON *.* TO 'newadmin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Now you’ve got another way in if root gets cranky again. 😎

Step 6: Fixing Broken MySQL Installs

Sometimes the problem isn’t with you—it’s with a messed-up MySQL install.

If nothing works, a clean reinstall might be best:


sudo apt remove --purge mysql-server mysql-client mysql-common
sudo apt autoremove
sudo apt autoclean
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt install mysql-server

Warning: This deletes all your MySQL data. Don’t do it unless you’re sure.

Useful Tips and Gotchas

  • MySQL usernames and hosts matter. root@localhost is different from root@127.0.0.1 or root@%.
  • Use mysql -u root -h 127.0.0.1 -p if localhost fails. Just try it.
  • If using MySQL 8+, authentication plugins changed. Some tools don’t support caching_sha2_password yet.

Tools You Can Use

Want to make managing MySQL easier in the future?

  • MySQL Workbench: A graphical tool to manage databases easily
  • phpMyAdmin: Web-based MySQL interface
  • Sequel Pro / TablePlus: GUI tools (Mac fans rejoice!)

But always get your terminal basics right. You’ll thank yourself later.

Error Still There?! Let’s Do a Checklist:

  1. Correct user and host? ✔️
  2. Password working? ✔️
  3. MySQL server running? ✔️
  4. Plugins and privileges correct? ✔️

If you’ve checked all these and it’s still broken… maybe take a break, have a coffee ☕, and try again. Sometimes that works better than anything else.

Wrapping It Up

MySQL access denied errors can feel like you’re locked out of your own digital house. But with calm, curiosity, and a few handy commands, you’re back in.

You learned how to:

  • Spot the cause of access errors
  • Reset the root password safely
  • Switch authentication methods
  • Even create a backup admin user

Nice job!

MySQL might be a strict gatekeeper, but now you’ve got the keys 🍀