How to Repair MySQL Databases and Tables

Try this guide with our instant dedicated server for as low as 40 Euros

repair mysql databases

Ensuring the seamless functioning of your apps largely depends on maintaining the integrity of your MySQL databases. 

However, several factors can lead to database corruption, including configuration file mistakes, hardware malfunctions, and software defects. When faced with corrupted MySQL tables or databases, it’s essential to know how to repair them effectively to prevent data loss and restore the database to its optimal state.

In this tutorial, we will discuss how to repair MySQL databases and tables. We will start with an introduction to MySQL errors and then go into the details of checking and rectifying these errors.

Table Of Contents

  1. What are MySQL Errors?
    1. The Prerequisites to Checking & Repairing MySQL Tables and Databases
  2. How to Check MySQL Tables for Errors
    1. Method #1: Run the MySQL CHECK TABLE Query
    2. Method #2: Use mysqlcheck
  3. How to Repair MySQL Database
    1. Method #1: Use REPAIR TABLE Query (Fastest, MyISAM-Specific)
    2. Method #2: Use mysqlcheck
    3. Method #3: Use the ALTER TABLE Statement
    4. Method #4: Engine-Specific Diagnostics and Repairs
  4. Conclusion
  5. FAQs

What are MySQL Errors?

MySQL errors are messages generated by the MySQL database server indicating something went wrong during an operation. These errors can provide valuable information for troubleshooting and resolving issues with your database.

Some of the common MySQL errors are:

Access Denied Errors

These errors indicate users don’t have the necessary permissions to perform the requested operation on the database. This could be due to incorrect credentials, insufficient privileges, or connection issues.

Syntax Errors

These errors occur when the MySQL server encounters a problem with the structure or syntax of the SQL query. This might involve typos, incorrect keywords, or missing parentheses.

Connection Errors 

These errors signify problems in establishing or maintaining a connection between the application and the MySQL server. This could be due to network issues, server downtime, or firewall restrictions.

Table or Data Issues

These errors arise when there are issues with the structure or contents of the database tables. This includes tables reaching their storage capacity, data type mismatches, or foreign key constraint violations.

Maintaining a healthy database environment requires knowing how to repair and mend corrupted tables, whether working with a single MySQL table or the entire MySQL server. To accomplish this, you should know how to check MySQL tables for errors.

The following section discusses how to check MySQL tables for errors. However, before that, let us take a quick look at the prerequisites. 

The Prerequisites to Checking & Repairing MySQL Tables and Databases

Before moving in, ensure you have the following: 

  • You have MySQL version 8.0 or better installed on your server.
  • You have access to a corrupted database or database table.
  • A user account with sudo or administrative privilege.
  • A user account with root privileges on the MySQL server.

How to Check MySQL Tables for Errors

Troubleshooting MySQL problems starts with running diagnostics processes. There are two primary methods to check a MySQL table for errors:

  • Run a MySQL CHECK TABLE query.
  • Use the mysqlcheck terminal program.

Method #1: Run the MySQL CHECK TABLE Query

The CHECK TABLE statement is a powerful tool for inspecting MySQL tables for inconsistencies and potential corruption. It allows users to diagnose issues while the MySQL service is running without interrupting database operations.

The basic syntax of the CHECK TABLE statement is as follows:

mysql> CHECK TABLE <table name> [, table name, table name] [option] [option];

You can check one or more tables at a time by separating their names with commas. For instance, execute the following statement without specifying any options to check a table for errors:

mysql> CHECK TABLE your_table_name;

This statement checks the specified table, providing a detailed report on the results.

The CHECK TABLE statement in MySQL supports several options to control the depth and types of checks performed on the tables. These options allow for flexibility in how thorough the table inspection should be and what aspects of the table should be checked. 

Here are some popular checks that you can add to the CHECK TABLE statement: 


Identifies version inconsistencies. 

When preparing to upgrade MySQL to a newer version, this option helps identify tables that might not be compatible with the new version. 

For instance, a table containing two-digit years will be flagged for an upgrade as MySQL 8.0 does not handle them.


Avoids checking row links. 

This prevents timeouts due to the longer time windows of the indepth checks. It is useful for large tables where a full check might be time-consuming and result in server timeout.


Check tables that were not closed properly. 

When a table has not been closed properly due to a server crash or other issues, this option quickly checks the table for problems without performing a full check.


Checks only tables that have changed since the last check or were not closed properly. 

Efficient for periodic checks where the user only wants to check tables that have had modifications or issues since the last time they were checked, saving time and resources.

MEDIUM (Default for MyISAM, views)

Verifies the validity of deleted links and performs a checksum for the rows. 

This is the default level of checking. It performs a more thorough check than QUICK or FAST, ensuring the integrity of the table structure and data.


Performs a thorough consistency check. 

When you need to perform the most comprehensive check possible, this option verifies all aspects of the table, ensuring there are no hidden issues. It is more time-consuming but provides the highest level of assurance about the table’s integrity.

Combining multiple options with CHECK TABLE tailors the verification process to the user’s needs. 

For instance, to perform a quick check to see if a table was closed correctly, run:

mysql> CHECK TABLE <table name> FAST QUICK;

This statement provides a balance between speed and error detection.

By understanding the different options and using the CHECK TABLE effectively, users can proactively identify potential problems with the MySQL tables and take appropriate actions to maintain data integrity and database health.

Method #2: Use mysqlcheck 

mysqlcheck is another valuable tool for checking MySQL tables for errors. 

It is a versatile command-line program that provides a powerful alternative to the CHECK TABLE query for verifying table integrity. This utility runs in the terminal while the MySQL service is active.

Note: To use mysqlcheck effectively, log in as the MySQL root user or a user with appropriate privileges.

To check tables using mysqlcheck, launch the terminal and navigate to the directory where the databases are stored. 

# sudo su

# cd /path/to/databases

Check the entire database with the following command:

# mysqlcheck -u root -p --databases database_name

Replace <database_name> with the actual name of the database you want to inspect. 


-u: Specifies the username (root in this case).

-p: Prompts to enter the password. 

–databases: Indicates the user wants to check all tables within the named database.

Alternatively, use the following command to check a specific table within the database:

# mysqlcheck <database name> <table name>

Replace <database_name> with the database containing the table, and <table_name> with the specific table required to verify.

mysqlcheck also supports various options to enhance table checks. Some of them are:

  • –repair: This option attempts to repair any errors encountered during the check. Use it with caution, as repairs might lead to data loss in some cases.
  • –optimize: This option optimizes the table structure for better performance.
  • –all-databases: This option checks all databases on the server.
  • –all-tables: This option checks all tables within the specified database(s).
  • –force: This option bypasses certain checks that might prevent the operation from completing, but use it cautiously as it might mask underlying issues.

By combining mysqlcheck with these options, users can create customized commands to suit specific needs, such as checking and repairing all tables in a database or optimizing specific tables for improved performance.

Now that you have an understanding of how to check tables for errors, let us see how to repair the MySQL database.

How to Repair MySQL Database

Fortunately, you can opt for several methods of repairing a MySQL database. 

However, it’s important to note that these methods are not guaranteed quick fixes and may result in data loss. If your database tables frequently become corrupt, it’s crucial to identify and address the underlying causes.

Before making any changes or starting repairs, use the cp command to create a backup copy of the directory to prevent any potential data loss.

# cp -r /var/lib/mysql /var/lib/mysql_backup

Alternatively, make a backup of your database before attempting any repair solutions. 

You can follow our guide How to Back Up & Restore a MySQL Database to ensure you have a rollback copy. 

Now that you have found the error tables, you can use one of these methods to fix MySQL errors.

Method #1: Use REPAIR TABLE Query (Fastest, MyISAM-Specific)

This is one of the fastest ways to resolve issues.

Start by identifying the problematic database and tables and execute the REPAIR TABLE query. 

mysql> REPAIR TABLE your_table_name;

Note: If the server shuts down during the repair process, it is crucial to rerun the REPAIR TABLE immediately before performing any further operations on the table. Typically, repairing a MyISAM table using this method does not lead to data loss under normal conditions.

Method #2: Use mysqlcheck

The mysqlcheck command offers an alternative method for repairing database tables directly from the terminal.

Navigate to the MySQL database directory as the root user:

# cd /var/lib/mysql

Use the -r option with mysqlcheck to repair database tables.

# mysqlcheck -r database_name table_name

This command offers a terminal alternative to REPAIR TABLE, allowing users to repair database tables directly from the command line.

Method #3: Use the ALTER TABLE Statement

If both the original and desired storage engines are the same, use ALTER TABLE to rebuild the table.

To find the storage engine your table uses, execute the following query: 

mysql> SHOW CREATE TABLE <table name>;

Now that you know which storage engine your table uses, execute ALTER TABLE

to fix the error. 

Next, execute the following statement to fix an InnoDB table:

mysql> ALTER TABLE table_name ENGINE=InnoDB;

Similarly, use the following statement to fix the error in the MyISAM table:

mysql> ALTER TABLE <table name> ENGINE = MyISAM;

Method #4: Engine-Specific Diagnostics and Repairs 

Based on the engine, MySQL provides specific methods for analyzing and repairing tables. Understanding each engine’s specific requirements can help effectively manage database integrity.

The following methods demonstrate how to repair MySQL table errors based on the storage engine.


myisamchk is a specialized tool provided by MySQL for examining and fixing MyISAM tables. Follow these steps to repair the MySQL table.

We recommend shutting down the MySQL server by executing the following systemctl command:

# sudo systemctl stop mysql

Navigate to the database directory containing the database as the root user.

# cd /var/lib/mysql/your_database_name

Check a specific table by executing myisamchk.

# myisamchk <table name>

If errors are found, repair the table by executing the following repair command:

# myisamchk --recover <table name>

Once the repair is complete, restart the MySQL server:

# sudo systemctl start mysql

Next, log in to MySQL to verify if the error is resolved.

# sudo mysql -u <user> -p <password>

mysql> USE <database name>;

mysql> SELECT * FROM <table name> LIMIT 4;

InnoDB Recovery Process

MySQL documentation recommends executing the InnoDB recovery process for InnoDB databases. 

Locate and open the global configuration file using a text editor like Vim or Nano.

For Ubuntu and Debian systems, the default location of this file is /etc/mysql/my.cnf.

Next, add the following option to the [mysqld] section:



The innodb_force_recovery option takes values ranging from 0 (off) to 6, with each level including the recovery features of the previous levels. Here is short description of these levels:

1: Ignore corrupt pages.

2: Prevent the background thread from running.

3: Do not run transaction rollbacks.

4: Prevents insert buffer merge.

5: Do not look at the undo log.

6: Prevent redo log application.

We recommend adjusting the level based on your recovery needs.

Next, save and close the file.

Restart the MySQL service to apply the new configuration.

# systemctl restart mysql

Export all databases to a backup file using the mysqldump command:

# sudo mysqldump --all-databases --add-drop-database --add-drop-table --routines > <file name>.sql

Next, drop all the affected database tables.

mysql> DROP TABLE IF EXISTS <table name>;

If the table does not drop through normal SQL commands, you can try removing the database table manually (stop MySQL server, navigate to /var/lib/mysql, locate the table file, and delete it as root).

Next, disable the recovery mode by commenting out the innodb_force_recovery option in my.cnf file. 

Save and close the file.

Next, restart the MySQL server.

# sudo systemctl start mysql

Restore the database from the backup .sql file by logging into the MySQL client:

# mysql -u root -p

Import the databases.

mysql> SOURCE /path/to/backup.sql;

Thoroughly test the database to ensure proper functionality. If issues persist, restore from the backup and consider alternative recovery methods.


Repairing a MySQL database is an essential skill for maintaining the health and performance of your applications. Whether you’re dealing with corrupt database tables, table structure issues, or overall table corruption, understanding the various methods to repair a corrupt database is crucial. 

Running appropriate commands in the command prompt, and leveraging the default storage engine’s capabilities can help you effectively manage and repair your databases.

By following the steps outlined in this guide, you can confidently execute the necessary commands to repair your database, use dump files for backups and restoration, and address issues with different database engines. 


Q. How do I know if my MySQL database or tables are corrupt?

You may experience errors such as missing data, unexpected crashes, or slow performance. You can also use the MySQL command CHECK TABLE to identify any corruption.

Q. What are the common causes of corruption in a MySQL database?

Corruption in a MySQL database can be caused by hardware issues, software bugs, power outages, or improper shutdown of the database server.

Q. What is the most effective method for fixing MySQL database corruption?

You can use software tools like Stellar Repair for MySQL, which can help repair and recover corrupted MySQL databases efficiently.

Q. Can I repair MySQL tables individually?

Yes, you can repair MySQL tables individually using the REPAIR TABLE statement or through database management tools.

Q. Is there a specific MySQL command to repair corrupt tables?

Yes, you can use the MySQL command REPAIR TABLE to repair corrupt tables within the database.

Q. How can I prevent database corruption in MySQL?

Regularly backup your database, ensure reliable hardware and power supply and avoid unexpected interruptions during database operations.

Q. How do I repair multiple corrupted tables in MySQL?

You can use software tools like Stellar Repair for MySQL to repair multiple corrupted tables in the database simultaneously.

Try this guide with our instant dedicated server for as low as 40 Euros