🔐 How I Recovered My MySQL Root Password on Windows (MySQL 8.0) — Step by Step
Source: Dev.to

Overview
Losing your MySQL root password can feel like the end of the world — especially when you’re a student or a fresher and MySQL just refuses to connect. I recently faced this exact issue on Windows + MySQL 8.0, spent hours getting errors like:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306'
After a lot of trial and error, I finally fixed it without losing any data. Here’s the exact method that worked 100 % for me.
Problem Summary
- OS: Windows 10/11
- MySQL Version: 8.0.x
- Issue: Forgot MySQL root password; MySQL service wouldn’t allow login.
- Tried:
--skip-grant-tablesmethod kept failing.
The Solution That Actually Worked (Init File Method)
This method is safe, official, and works even when other methods fail.
Step 1: Stop MySQL Service
- Press Win + R, type
services.mscand hit Enter. - Locate MySQL80 (or the service name for your installation).
- Right‑click → Stop.
Step 2: Create a Password Reset File
- Open Notepad.
- Paste the following SQL:
ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password
BY 'TempPass@123';
- Save the file as
reset.sql(make sure the extension is.sql, not.txt) in:
C:\Users\<your‑username>\Documents\
Step 3: Start MySQL Using the Init File
Open Command Prompt as Administrator and run:
cd "C:\Program Files\MySQL\MySQL Server 8.0\bin"
Then start MySQL with the init file:
mysqld --init-file="C:\Users\<your‑username>\Documents\reset.sql" --datadir="C:\ProgramData\MySQL\MySQL Server 8.0\Data"
What Should Happen
- The terminal appears “stuck” (no prompt).
- No errors are shown.
- MySQL server is running, meaning the password reset command was executed successfully.
Step 4: Stop the Temporary Server
Press Ctrl + C in the terminal to terminate the process, then close the window.
Step 5: Start MySQL Normally
- Open
services.mscagain. - Start MySQL80.
Step 6: Login with the New Password
mysql -u root -p
Enter the password you set in reset.sql (TempPass@123). You should see:
Welcome to the MySQL monitor
Step 7 (Important): Set Your Own Secure Password
Inside the MySQL monitor, run:
ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password
BY 'YourOwnStrongPassword@123';
FLUSH PRIVILEGES;
Cleanup (Optional but Recommended)
Delete the reset file you created:
del "C:\Users\<your‑username>\Documents\reset.sql"
Common Mistakes I Made (So You Don’t)
- Trying to connect when the MySQL server wasn’t running.
- Using the wrong
datadir. - Forgetting to adjust Windows file permissions.
- Assuming a “stuck” terminal meant an error (it actually indicates success).
Final Thoughts
- If you’re stuck with MySQL root access on Windows: don’t panic, don’t reinstall MySQL, and don’t delete data.
- The init‑file method is the most reliable fix I’ve found.
If this helped you, feel free to share it—it might save someone hours of frustration.