Tutorials  /  Databases

How to Fix Corrupted Tables in MySQL: A Complete Guide

Ccentron Redaktion · December 2024 ·5 min read ·Databases, Tutorial

Occasionally, MySQL tables can become corrupted, meaning that an error has occurred and the data held within them is unreadable. Attempts to read from a corrupted table will usually lead to the server crashing.

Common Causes of Corrupted Tables

  • The MySQL server stops in middle of a write.
  • An external program modifies a table that’s simultaneously being modified by the server.
  • The machine is shut down unexpectedly.
  • The computer hardware fails.
  • There’s a software bug somewhere in the MySQL code.
DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

Backup Before Troubleshooting

If you suspect that one of your tables has been corrupted, you should make a backup of your data directory before troubleshooting or attempting to fix the table. This will help to minimize the risk of data loss.

Stopping the MySQL Service

First, stop the MySQL service:

Console
sudo systemctl stop mysql

Note: On some platforms such as Rocky Linux, the MySQL service may be called mysqld instead.

Creating a Backup

Then copy all of your data into a new backup directory. On Ubuntu systems, the default data directory is /var/lib/mysql/:

Code
cp -r /var/lib/mysql /var/lib/mysql_bkp

Checking for Corruption

After making the backup, you’re ready to begin investigating whether the table is in fact corrupted. If the table uses the MyISAM storage engine, you can check whether it’s corrupted by restarting MySQL and running a CHECK TABLE statement from the MySQL prompt:

Console
sudo systemctl start mysql
CHECK TABLE table_name;

Repairing MyISAM Tables

A message will appear in this statement’s output letting you know whether or not it’s corrupted. If the MyISAM table is indeed corrupted, it can usually be repaired by issuing a REPAIR TABLE statement:

Code
REPAIR TABLE table_name;

Output Example

Assuming the repair was successful, you will see a message like this in your output:

Code
+--------------------------+--------+----------+----------+
| Table                    | Op     | Msg_type | Msg_text |
+--------------------------+--------+----------+----------+
| database_name.table_name | repair | status   | OK       |
+--------------------------+--------+----------+----------+

Handling InnoDB Tables

If the corrupted table uses the InnoDB storage engine, the process for repairing it will be different. InnoDB is the default storage engine in MySQL as of version 8.0 and features automated corruption checking and repair operations. However, if you do encounter a situation where you need to rebuild a corrupted InnoDB table, use the “Dump and Reload” method.

Restarting MySQL

Try restarting the MySQL service:

Console
sudo systemctl restart mysql

Using Force Recovery

If the server remains inaccessible, enable InnoDB’s force_recovery option by editing the mysqld.cnf file:

Console
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

In the [mysqld] section, add the following line:

Code
[mysqld]
innodb_force_recovery=1

Save the file and restart the MySQL service.

Console
sudo systemctl restart mysql

Dumping and Reloading the Table

If you can successfully access the corrupted table, use the mysqldump utility to dump your table data to a new file. You can name this file whatever you like; in this example, we’ll name it out.sql:

Code
mysqldump database_name table_name > out.sql

Then, drop the table from the database. To avoid having to reopen the MySQL prompt, you can use the following syntax:

Code
mysql -u user -p --execute="DROP TABLE database_name.table_name"

Following this, restore the table with the dump file you just created:

Code
mysql -u user -p < out.sql

Conclusion

Note that the InnoDB storage engine is generally more fault-tolerant than the older MyISAM engine. Tables using InnoDB can still be corrupted, but because of its auto-recovery features, the risk of table corruption and crashes is markedly lower.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren