MySQL Mayhem: How to Reset Your Root Password for MySQL in No Time!

Purvesh Panchal - Sep 5 - - Dev Community

Hey there, fellow MySQL wrangler! Ever been locked out of your MySQL database because the root password decided to pull a vanishing act? Or maybe it’s just not playing nice? Don’t sweat it! you’re in good company. This happens to pretty much everyone at some point. The good news? It’s an easy fix!

By the time we’re done here, you’ll be resetting that pesky root password like a seasoned pro. No more pulling your hair out — just pure MySQL mastery.

Step 1: Knock, Knock! Getting Into MySQL as Root

First thing’s first, let’s get through the front door. Fire up your terminal and hit MySQL as the root user with this command:

sudo mysql -u root
Enter fullscreen mode Exit fullscreen mode

Boom! You’re in! (Hopefully... 😬 If not, we’ll save that mess for another day.) But let’s assume we’re in—because we're optimistic like that.

Step 2: Let’s Spy on the Authentication Setup

Now that you’ve got your foot in the door, it’s time to see how MySQL is handling the root user’s login. We need to know what authentication method is being used. Just run this command:

SELECT User, Host, plugin FROM mysql.user;
Enter fullscreen mode Exit fullscreen mode

This will spit out some handy details—who’s logging in, from where, and, most importantly, how. It’s like a little behind-the-scenes peek at the database’s security setup. Now, based on that, we’ll choose the right path forward. Ready?

Step 3: The Password Fix—Choose Your Adventure!

Alright, now the fun begins. Depending on the authentication method you found in Step 2, here are a few different ways you can reset the root password.

Option 1: Old-School Password Authentication

If you see mysql_native_password listed as the plugin, or if you just want to stick with the classic method of login, this is the command for you:

UPDATE mysql.user SET Password = PASSWORD('your_new_password') WHERE User = 'root';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

Just like that, new password in place! (And don't forget to make it something you’ll actually remember this time... or use a password manager—seriously.)

Option 2: Welcome to the Future with caching_sha2_password

If you’re rolling with MySQL 8.0+ (or just like keeping things secure), your root user might be using caching_sha2_password. Here’s how to reset the password with the new-age encryption:

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'your_new_password';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

Nice, right? Fancy encryption, modern security—everything you need to keep hackers and snoops at bay.

Option 3: A Blast from the Past—Switching Back to mysql_native_password

If, for some reason, you need to revert back to the old-school method (maybe an app you’re using requires it), you can switch the root user back to mysql_native_password with this command:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

Nothing wrong with a little nostalgia, right? Sometimes the old ways still work best!

Step 4: Make Sure MySQL Actually Listens (Apply the Changes)

Now, after you’ve run the right command to reset the password, don’t forget to lock it in! Run this:

FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode

This makes sure that MySQL actually takes those changes seriously and updates its privileges. It’s like telling MySQL, "Hey, pay attention!"

Step 5: Exit Stage Left

You’ve done the hard work—now it’s time to exit the MySQL shell. Just type:

exit;
Enter fullscreen mode Exit fullscreen mode

And you’re outta there. How smooth was that?

Final Thoughts

Congrats! You’ve just wrestled MySQL into submission and reset that elusive root password. Next time, maybe note the password down somewhere safe... or, you know, use a password manager! 😏

Troubleshooting Tips: When MySQL Still Plays Hard to Get

Okay, so you’ve followed the steps, and MySQL’s still giving you the cold shoulder? Don’t panic! Here’s a little trick up your sleeve.

Problem: “Access Denied” Errors?

Sometimes, MySQL can act stubborn when you’re trying to log in, even after resetting the password. If you’re getting an “Access Denied” error, you may need to restart MySQL in safe mode.

Here’s what to do:

  1. Stop the MySQL service by running this command in your terminal:
   sudo systemctl stop mysql
Enter fullscreen mode Exit fullscreen mode
  1. Restart MySQL in safe mode with the --skip-grant-tables option, which lets you bypass the normal login process:
   sudo mysqld_safe --skip-grant-tables &
Enter fullscreen mode Exit fullscreen mode
  1. Log in to MySQL without a password:
   mysql -u root
Enter fullscreen mode Exit fullscreen mode
  1. Reset the password using the same commands from earlier, depending on your authentication method.

  2. Exit MySQL, and restart the service normally:

   sudo systemctl start mysql
Enter fullscreen mode Exit fullscreen mode

Now, try logging in again with your new password. It should work like a charm!

Now go forth and manage your databases like the MySQL genius you are! 🎉

. . . .