DEV Community

Cover image for 🔐 How I Recovered My MySQL Root Password on Windows (MySQL 8.0) — Step by Step
SoftwareDev
SoftwareDev

Posted on

🔐 How I Recovered My MySQL Root Password on Windows (MySQL 8.0) — Step by Step

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'

Enter fullscreen mode Exit fullscreen mode

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
  • --skip-grant-tables method 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

  1. Press Win + R → type services.msc
  2. Find MySQL80
  3. Right-click → Stop

📝 Step 2: Create a Password Reset File

  1. Open Notepad
  2. Paste this:
ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password
BY 'TempPass@123';

Enter fullscreen mode Exit fullscreen mode
  1. Save the file as:
reset.sql
Enter fullscreen mode Exit fullscreen mode
  1. Save it in:
C:\Users\<your-username>\Documents\

Enter fullscreen mode Exit fullscreen mode

⚠️ Make sure it’s reset.sql, not reset.sql.txt

▶️ 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"

Enter fullscreen mode Exit fullscreen mode

Then:

mysqld --init-file="C:\Users\<your-username>\Documents\reset.sql" --datadir="C:\ProgramData\MySQL\MySQL Server 8.0\Data"

Enter fullscreen mode Exit fullscreen mode

✅ What should happen

  • Terminal looks “stuck”
  • No errors
  • MySQL server is running

👉 This means the password reset command was executed successfully.

⛔ Step 4: Stop Temporary Server

Press:

CTRL + C

Enter fullscreen mode Exit fullscreen mode

Close the terminal.

▶️ Step 5: Start MySQL Normally

  • Open services.msc
  • Start MySQL80

🔓 Step 6: Login with New Password

Open CMD:

mysql -u root -p

Enter fullscreen mode Exit fullscreen mode

Enter the password you set in reset.sql.

If you see:

Welcome to the MySQL monitor

Enter fullscreen mode Exit fullscreen mode

🎉 You’re back in.

🔐 Step 7 (Important): Set Your Own Secure Password

Inside MySQL:

ALTER USER 'root'@'localhost'
IDENTIFIED WITH mysql_native_password
BY 'YourOwnStrongPassword@123';

FLUSH PRIVILEGES;

Enter fullscreen mode Exit fullscreen mode

🧹 Cleanup (Optional but Recommended)

Delete the file:

reset.sql

Enter fullscreen mode Exit fullscreen mode

You don’t need it anymore.

🧠 Common Mistakes I Made (So You Don’t)

❌ Trying to connect when MySQL server wasn’t running
❌ Using wrong datadir
❌ Forgetting Windows permissions
❌ Assuming “terminal stuck” = error (it’s actually success)

✅ Final Thoughts

  • If you’re stuck with MySQL root access on Windows:
  • Don’t panic
  • Don’t reinstall MySQL
  • 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.

Top comments (0)