MySQL Database Recovery from Physical Data Files: A Step-by-Step Guide for System Reinstallation Scenarios


3 views

When you have MySQL's physical data files but no dump backups, you're essentially working with the raw database storage. The key files you'll typically find in a MySQL data directory include:

ibdata1          # InnoDB system tablespace
ib_logfile0      # InnoDB log files
ib_logfile1
database_name/   # Directory for each database
  table_name.frm # Table structure
  table_name.ibd # InnoDB table data

Before proceeding, ensure you have:

  • MySQL server installed (same or compatible version)
  • File system permissions to access the old data files
  • Enough disk space for the recovery process
  • Stopped the MySQL service during the operation

1. Prepare the Environment

First, locate your current MySQL data directory. You can find it by running:

SHOW VARIABLES LIKE 'datadir';

Create a backup of your current data directory before proceeding:

sudo systemctl stop mysql
sudo cp -R /var/lib/mysql /var/lib/mysql_backup

2. Transfer the Old Database Files

Copy the files from your old installation to the new data directory:

sudo cp -R /path/to/old/mysql/data /var/lib/mysql
sudo chown -R mysql:mysql /var/lib/mysql

3. Handle InnoDB Recovery

If you're using InnoDB, add these lines to your my.cnf/my.ini file:

[mysqld]
innodb_force_recovery = 6
innodb_purge_threads = 0

Then start MySQL in recovery mode:

sudo systemctl start mysql

4. Verify and Repair Tables

After MySQL starts, check for corrupted tables:

mysqlcheck --all-databases --check --extended

Repair any corrupted tables:

mysqlcheck --all-databases --repair --extended

If the direct file copy method fails, you can try extracting data using mysqlfrm:

# Install mysql-utilities
sudo apt-get install mysql-utilities

# Recreate CREATE TABLE statements from .frm files
mysqlfrm --server=user:pass@localhost:3306 /path/to/old/data/database/table.frm

Then import the generated SQL to recreate tables and use ALTER TABLE to attach the .ibd files:

ALTER TABLE table_name IMPORT TABLESPACE;
  • Version compatibility is crucial - MySQL 5.7 files won't work with MySQL 8.0
  • Mixed storage engines (MyISAM and InnoDB) require different handling
  • Always test recovered databases thoroughly before putting them into production
  • Consider setting up proper backup routines after recovery

When you have MySQL's physical data files but no SQL dump, you're working with the raw database structure. MySQL stores data in several critical files:

/var/lib/mysql/
├── ibdata1          # Shared tablespace data
├── ib_logfile0      # Transaction logs
├── ib_logfile1
├── database_name/   # Per-database directory (for file-per-table)
│   ├── table_name.frm
│   ├── table_name.ibd
│   └── db.opt

Here's how to restore from physical files after system reinstallation:

1. Stop MySQL Service

sudo systemctl stop mysql

2. Backup Current Data Directory

sudo mv /var/lib/mysql /var/lib/mysql_backup

3. Copy Old Data Files

sudo cp -R /path/to/old/mysql/data /var/lib/mysql
sudo chown -R mysql:mysql /var/lib/mysql

4. Handle InnoDB Configuration

Add these to your my.cnf if using InnoDB files from different MySQL version:

[mysqld]
innodb_force_recovery = 1
innodb_file_per_table = 1

When Table Files Are Corrupted

Try extracting data using mysqlfrm (for .frm files):

mysqlfrm --diagnostic /path/to/table.frm > table_definition.sql

Recovering Specific Tables Only

For file-per-table storage:

CREATE DATABASE recovered_db;
USE recovered_db;
# Create empty tables first
# Then discard tablespace and copy .ibd files
ALTER TABLE table_name DISCARD TABLESPACE;
cp old_table.ibd /var/lib/mysql/recovered_db/
chown mysql:mysql /var/lib/mysql/recovered_db/old_table.ibd
ALTER TABLE table_name IMPORT TABLESPACE;
  • Run mysql_upgrade if moving between major versions
  • Check for errors in MySQL error log
  • Consider running REPAIR TABLE on problematic tables