> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Repairing MySQL databases and tables

> Discover how to repair MySQL tables and databases with step-by-step instructions, code snippets, and related links.

This article describes how to repair MySQL tables and databases. As a database's tables grow, errors may occur from time to time. When they do, MySQL includes several tools that you can use to check and repair database tables. To do this, follow the procedures below in the order in which they appear.

<Note>
  You must have root access to the server to follow these procedures.
</Note>

## Step 1: Back up the databases

Before you attempt to repair any database, you should back it up first. To back up all of the files from all of your databases, follow these steps:

1. Log in to your server [using SSH](/docs/using-ssh-secure-shell).

2. Stop the MySQL server using the appropriate command for your Linux distribution:

   * For AlmaLinux and Fedora, type:

     ```shell theme={null}
     service mysqld stop
     ```

   * For Debian and Ubuntu, type:

     ```shell theme={null}
     service mysql stop
     ```

3. Type the following command:

   ```shell theme={null}
   cp -rfv /var/lib/mysql /var/lib/mysql$(date +%s)
   ```

   > 📘 Note
   >
   > This command copies all of the files from all of your databases to a directory name based on the current time (more precisely, the number of seconds elapsed since January 1, 1970). This ensures that each database backup is stored in a directory that has a unique name. For added protection, you can (and should) back up the database files to a remote location not on the server.

4. Restart the MySQL server using the appropriate command for your Linux distribution:

   * For AlmaLinux and Fedora, type:

     ```shell theme={null}
     service mysqld start
     ```

   * For Debian and Ubuntu, type:

     ```shell theme={null}
     service mysql start
     ```

## Step 2: Run mysqlcheck

After you back up your databases, you are ready to start troubleshooting. The *mysqlcheck* program enables you to check and repair databases while MySQL is running. This feature is useful when you want to work on a database without stopping the entire MySQL service.

Additionally, *mysqlcheck* works on tables that use the MyISAM or InnoDB database engines.

<Note>
  For information about how to determine which storage engine a database table is using, please see [this article](/docs/working-with-mysql-database-engines).
</Note>

To use *mysqlcheck*, follow these steps:

1. As the root user, type the following command:

   ```shell theme={null}
   cd /var/lib/mysql
   ```

2. Type the following command, replacing ***database*** with the name of the database that you want to check:

   ```shell theme={null}
   mysqlcheck database
   ```

   > 👍 Tip
   >
   > The previous command checks all of the tables in the specified database. Alternatively, to check a specific table in a database, type the following command. Replace ***database*** with the name of the database, and replace ***table*** with the name of the table that you want to check:
   >
   > ```shell theme={null}
   > mysqlcheck database table
   > ```

3. *Mysqlcheck* checks the specified database and tables. If a table passes the check, *mysqlcheck* displays **OK** for the table. However, if *mysqlcheck* reports an error for a table, type the following command to try to repair it. Replace ***database*** with the database name, and ***table*** with the table name:

   ```shell theme={null}
   mysqlcheck -r database table
   ```

4. If *mysqlcheck* cannot successfully repair the table or tables, go to the following procedure.

## Step 3: Run engine-specific diagnostics

If running *mysqlcheck* does not fix the problem, the next step is to run diagnostics specific to the engine used by the database table or tables. Follow the appropriate procedure below for your table's database storage engine.

<Note>
  For information about how to determine which storage engine your database tables are using, please see [this article](/docs/working-with-mysql-database-engines).
</Note>

### Repairing MyISAM tables with myisamchk

If you are using the MyISAM storage engine for a table, you can run the *myisamchk* program to repair it. To do this, follow these steps:

<Warning>
  **Important**

  The *myisamchk* program only works for tables that use the MyISAM storage engine. It does not work for the InnoDB engine.
</Warning>

1. Stop the MySQL server using the appropriate command for your Linux distribution:

   * For AlmaLinux and Fedora, type:

     ```shell theme={null}
     service mysqld stop
     ```

   * For Debian and Ubuntu, type:

     ```shell theme={null}
     service mysql stop
     ```

2. Type the following command:

   ```shell theme={null}
   cd /var/lib/mysql
   ```

3. Change to the directory where the database is located. For example, if the database is named *customers*, type `cd customers`.

4. Type the following command, replacing ***table*** with the name of the table that you want to check:

   ```shell theme={null}
   myisamchk table
   ```

   > 👍 Tip
   >
   > To check all of the tables in a database, type the following command:
   >
   > ```shell theme={null}
   > myisamchk *.MYI
   > ```

   > 🚧 Important
   >
   > If the previous command does not work, you can try deleting temporary files that may be preventing *myisamchk* from running correctly. To do this, change back to the */var/lib/mysql* directory, and then type the following command:
   >
   > ```shell theme={null}
   > ls */*.TMD
   > ```
   >
   > If there are any .TMD files listed, type the following command to delete them:
   >
   > ```shell theme={null}
   > rm */*.TMD
   > ```
   >
   > Then try to run *myisamchk* again.

5. To try to repair a table, type the following command, replacing ***table*** with the name of the table that you want to repair:

   ```shell theme={null}
   myisamchk --recover table
   ```

6. Restart the MySQL server using the appropriate command for your Linux distribution:

   * For AlmaLinux and Fedora, type:

     ```shell theme={null}
     service mysqld start
     ```

   * For Debian and Ubuntu, type:

     ```shell theme={null}
     service mysql start
     ```

7. Test the repaired table or tables.

### Running the InnoDB recovery process

If you are using the InnoDB storage engine for a database table, you can run the InnoDB recovery process. To do this, follow these steps:

1. Use your preferred text editor to open the *my.cnf* file on your server. The location of the *my.cnf* file depends on your Linux distribution:

   * On AlmaLinux and Fedora, the *my.cnf* file is located in the **/etc** directory.

   * On Debian and Ubuntu, the *my.cnf* file is located in the **/etc/mysql** directory.

2. In the *my.cnf* file, locate the **\[mysqld]** section.

3. Add the following line to the **\[mysqld]** section:

   ```text theme={null}
   innodb_force_recovery=4
   ```

4. Save the changes to the *my.cnf* file, and then restart the MySQL server using the appropriate command for your Linux distribution:

   * For AlmaLinux and Fedora, type:

     ```shell theme={null}
     service mysqld restart
     ```

   * For Debian and Ubuntu, type:

     ```shell theme={null}
     service mysql restart
     ```

5. Type the following command to export all of the databases to the *databases.sql* file:

   ```shell theme={null}
   mysqldump --all-databases --add-drop-database --add-drop-table --routines > databases.sql
   ```

6. Start the *mysql* program, and then try to drop the affected database or databases using the **DROP DATABASE** command.

   > 📘 Note
   >
   > If MySQL is unable to drop a database, you can delete it manually in step 8 below after you stop the MySQL server.

7. Stop the MySQL server using the appropriate command for your Linux distribution:

   * For AlmaLinux and Fedora, type:

     ```shell theme={null}
     service mysqld stop
     ```

   * For Debian and Ubuntu, type:

     ```shell theme={null}
     service mysql stop
     ```

8. If you were unable to drop a database in step 6, type the following commands to delete it manually. Replace ***database*** with the name of the database that you want to delete:

   ```shell theme={null}
   cd /var/lib/mysql
   rm -rf database
   ```

   > ❗️ Warning
   >
   > Make sure you do **not** delete the *mysql* or *performance\_schema* directories!

9. Use your preferred text editor to open the *my.cnf* file on your server, and then comment out the following line in the **\[mysqld]** section as shown:

   ```
   #innodb_force_recovery=4
   ```

   > 📘 Note
   >
   > This disables InnoDB recovery mode.

10. Save the changes to the *my.cnf* file, and then start the MySQL server using the appropriate command for your Linux distribution:

    * For AlmaLinux and Fedora, type:

      ```shell theme={null}
      service mysqld start
      ```

    * For Debian and Ubuntu, type:

      ```shell theme={null}
      service mysql start
      ```

11. Type the following command to restore the databases from the backup file you created in step 5:

    ```shell theme={null}
    mysql < databases.sql
    ```

12. Test the restored database.

## More information

* For more information about *mysqlcheck*, please visit [https://dev.mysql.com/doc/refman/5.5/en/mysqlcheck.html](https://dev.mysql.com/doc/refman/5.5/en/mysqlcheck.html).

* For more information about *myisamchk*, please visit [https://dev.mysql.com/doc/refman/5.5/en/myisamchk.html](https://dev.mysql.com/doc/refman/5.5/en/myisamchk.html).

## Related articles

* [Managing MySQL databases, users, and tables from the command line](/docs/managing-mysql-databases-and-users-from-the-command-line)

* [Working with MySQL database engines](/docs/working-with-mysql-database-engines)
