Skip to content

Instantly share code, notes, and snippets.

@josemmo
Created August 28, 2020 18:48
Show Gist options
  • Save josemmo/24e35f2b4984a4370ce2c164f5956437 to your computer and use it in GitHub Desktop.
Save josemmo/24e35f2b4984a4370ce2c164f5956437 to your computer and use it in GitHub Desktop.
Repair MySQL data directory (for XAMPP)
# Based on this answer: https://stackoverflow.com/a/61859561/1956278
# Backup old data
Rename-Item -Path "./data" -NewName "./data_old"
# Create new data directory
Copy-Item -Path "./backup" -Destination "./data" -Recurse
Remove-Item "./data/test" -Recurse
$dbPaths = Get-ChildItem -Path "./data_old" -Exclude ('mysql', 'performance_schema', 'phpmyadmin') -Recurse -Directory
Copy-Item -Path $dbPaths.FullName -Destination "./data" -Recurse
Copy-Item -Path "./data_old/ibdata1" -Destination "./data/ibdata1"
# Notify user
Write-Host "Finished repairing MySQL data"
Write-Host "Previous data is located at ./data_old"
@safirbltr
Copy link

how to use this bloc of code

@bmrafiq
Copy link

bmrafiq commented Jan 11, 2022

Awesome

@SannanOfficial
Copy link

@safirbltr Go to xampp directly, then go to mysql and then open PowerShell there and simply paste this code.

@AdrianGS8E
Copy link

Thanks!!!!

@sarah-browning
Copy link

Amazing! Thanks so much.

@DinithKumudika
Copy link

thanks man you saved my precious time

@ashenthiwanka99
Copy link

thank you saved my time

@prathu9
Copy link

prathu9 commented May 15, 2022

thank you.

@Pseudoman21
Copy link

Still helpful even now. Thank you so much!

@irkha
Copy link

irkha commented Jun 22, 2022

The best solution ever. it solves the problem in seconds
Thanks a lot

@theweirdgenius
Copy link

This works! But for time being. MySQL started crashing again. Not a permanent solution.

@vasquezdavidd
Copy link

Thank you very much!!

@Rettend
Copy link

Rettend commented Sep 29, 2022

Ty!

@anasriaz786
Copy link

how do we exactly run this script?

@akashsinghtkd
Copy link

thank you.

@To-heeb
Copy link

To-heeb commented Dec 4, 2022

How to run this script

  1. Create a file with the extension ".ps1" in your mysql directory in your xampp folder e.g(repair-mysql-data.ps1).

  2. Open the file and paste the above commands in it.

  3. Then open the mysql folder in your powershell terminal( left click then you see ) this ⬇️
    image

  4. Then you see this ⬇️
    image

  5. After that run "PowerShell.exe -ExecutionPolicy UnRestricted -File repair-mysql-data.ps1" (your can always replace the name with whatever you have named the script)⬇️
    image

  6. Another way to get it done

@Waheire
Copy link

Waheire commented Feb 2, 2023

Great! worked!, thank you!

@cartzedan
Copy link

cartzedan commented Apr 24, 2023

Great work. This works perfectly without errors or stress.

@MarioSwitch
Copy link

Perfect!

@DKVI
Copy link

DKVI commented Aug 2, 2023

I confirm it still works!

@ucheohams
Copy link

This still works, thank you

@rbtarthur
Copy link

thank you very much it still works!

@akimi0308
Copy link

Saved me! Thank you sooooooo much!

@spring8904
Copy link

Thank you!

@Chanaka-J
Copy link

Chanaka-J commented Jan 17, 2024

you can add a line at the beginning of the script to delete the existing "data_old" directory before creating a new backup.

# Delete existing data_old directory
Remove-Item -Path "./data_old" -Recurse -Force

@ItsSalman99
Copy link

Amazing ...

@Arman016
Copy link

Arman016 commented Feb 7, 2024

It's just awesome! Thanks very much!

@KipchirchirIan
Copy link

Marvelous! Works beautifully.

@aagirre92
Copy link

thanks! as @Chanaka-J suggests adding that line helps in case you have to run it more than once

@bismark-joe
Copy link

Thank you a thousand times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment