This is the second part of a guide on recovering MySQL data from a broken Raspberry Pi.
Part 1: Rebuild the Raspberry Pi
Part 2: Recover the MySQL data and restore to the new environment.
With a newly functional Raspberry Pi, it was now time to try and recover MySQL data. Clearly this will only work if the SD card in the old Pi itself is actually functional and readable.
The good news is that MySQL stores its data on the file system in a reasonably compact and accessible format. There are a few differences in structure based on whether your database tables are in MyISAM or InnoDB format. This post assumes your tables are in InnoDB format (since that’s what mine were) but as far as I know, the only difference is that if your tables are in MyISAM format, then there’s no need to copy the ibdata* files over.
In order to do this, you’ll need:
- Access to a functional Linux environment. You could use your new Pi, but I opted for an Ubuntu VM.
- The old SD card and a card reader
- A FAT32 USB stick or similar means to get the files off your old card and on to the new SD card (assuming you’re not using your Pi for this.)
As I write this, I realise using the new Pi environment would probably have been the better route as it would save some jimmying around. The benefit with the Ubuntu route was it saved some effort in terms of mounting volumes and being able to use a GUI for copying.
But maybe I’ll do that if I have to do this again… but I’ll assume you’re following along with an Ubuntu VM but obviously ignore any unnecessary steps as appropriate!
- Boot your Linux environment
- Attach your card reader and SD card
- Attach your USB stick
- By default, the Ubuntu file explorer does not run as root so you will need to launch it as root in order to access the filesystem of your old SD card. Do this with:
- This should open a file explorer with root permissions. Find your SD card volumes in the side – it will either be ‘rootfs’ or a bunch of numbers or similar. It’s not the ‘boot’ partition.
- Navigate through to /var/lib/mysql. You should see some files and then folders for all your databases. In my case, I only wanted one specific database. Copy that whole folder to your USB stick.
- Bear in mind that if you use stored procedures in your MySQL database, then the definitions of these are actually stored in mysql.proc. You can either just copy the whole mysql database folder, or just copy the proc* files
- If your database is InnoDB, also copy the ibdata1, ib_logfile0 and ib_logfile1 files to your USB stick. If you don’t copy these files, then you will likely see an error message when accessing MySQL such as ‘table database.table does not exist in the engine’
- Unmount / eject your SD card
- Insert / mount the new SD card
- You essentially now repeat the above steps… but in this case, you copy the database folder(s) and ibdata* files to /var/lib/mysql to the SD card
- Unmount / eject your SD card
- Put the SD card in the Pi and boot
- Important: at this point, the mysql daemon probably won’t start. This is fine.
- Either login or SSH in to the Pi. Issue the command:
sudo chown mysql:mysql /var/lib/mysql/ -R
- Either restart mysql:
sudo /etc/init.d/mysql restart
or if you’re feeling lazy just reboot the Pi:
And this *should* be everything you need to do. For my purposes this was it. Your mileage may vary, especially if you had more than InnoDB database stored on the old MySQL server.