Method 1: If You Know the Current Root Password
Step-by-Step
-
Log in to MySQL as root:
-
Change the root password:
Make sure to replace 'NewSecurePassword!'
with a strong, secure password.
Method 2: If You Forgot the Root Password
Step-by-Step
-
Stop the MySQL Service:
-
Start MySQL in Safe Mode (no password check):
Wait a few seconds after this command.
-
Login without password:
-
Set a new root password:
-
Stop the MySQL Safe Mode and Restart the Service:
-
Test login with new password:
Tips for Security
-
Use a strong password with upper/lowercase letters, numbers, and symbols.
-
Never leave the root account with a blank or weak password.
-
Restrict remote root access for added security.