How to repair MySQL databases and tables

2022-09-23 By Nathan 48967 Views linux mysql
8 reviews

MySQL is a relational database management system based on the Structured Query Language, which is the popular language for accessing and managing the records in the database. MySQL is open-source and free software under the GNU license. It is supported by Oracle Company.


Database corruption is not always as simple as a "database corrupted" error message. We can only act based on the symptoms that suggest database corruption. The corruption may be limited to a single table, a data block, or a single entry.


Common causes of database corruption are:

  • Server hardware failure.

  • Unexpected machine shutdown.

  • The MySQL server stopped in the middle of a write.

  • Using 3rd party software to access the database

  • Software bugs within the MySQL code.

  • Wrong data schema.


Backing up MySQL Data


  1. Stop MySQL database using the following command.

    # systemctl stop mysqld
    

    repairmysqldb1

  2. Create a backup now.

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

    repairmysqldb2

  3. Restart the MySQL database.

    # systemctl start mysqld
    

    repairmysqldb3


Finding MySQL Engine Type


  1. Log in to MySQL using username and password.

    # mysql -u root -p
    

    repairmysqldb4

  2. List the databases in MySQL.

    SHOW DATABASES;
    

    repairmysqldb5

  3. Use the following command to know the database table engine. If it is InnoDB or MyISAM.

    SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = 'layerstack_innodb';
    
    SELECT TABLE_NAME, ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = 'layerstack_myisam';
    

    repairmysqldb6


Repairing MyISAM Tables


  1. Select layerstack_myisam database using the following command.

    USE layerstack_myisam;
    

    repairmysqldb7

  2. Check for corruption on tables running the MyISAM storage engine, such as the customer's table above, using the following MySQL prompt.

    CHECK TABLE  layerstack_myisam;
    

    repairmysqldb8


    Use the following command to repair myisam table.

    REPAIR TABLE  layerstack_myisam;
    

    repairmysqldb9


Repairing MySQL Database with mysqlcheck

The mysqlcheck command is an alternative to repairing database tables in the terminal.


  1. Change directory to /var/lib/mysql.

    # cd /var/lib/mysql
    

    repairmysqldb10

  2. Use mysqlcheck command.

    #  mysqlcheck -u root -p --safe --recover <database> <tablename>
    

    repairmysqldb11


Repairing InnoDB Tables

  1. Start InnoDB recovery, use the following command to edit mysqld.cnf file.

    # vim /etc/my.cnf
    

    repairmysqldb12


    And add the following

    innodb_force_recovery=1
    

    repairmysqldb13


    By default, the forced recovery option is set to 0 (off) and goes up to 6. Every new level includes the recovery features of the lower numbers.

  2. Restart the MySQL service.

    # systemctl restart mysqld
    

    repairmysqldb14

  3. Create mysqldump of layerstack_innodb database.

    # mysqldump -u root -p --routines --triggers --events layerstack_innodb > backup_db.sql
    

    repairmysqldb15

  4. The next step is to drop the table from the database.

    # mysql -u root -p --execute="DROP TABLE layerstack_innodb.layerstack_innodb"
    

    repairmysqldb16

  5. Restore the table.

    # mysql -u root -p  layerstack_innodb < backup_db.sql
    

    repairmysqldb17


This tutorial demonstrated how to fix a corrupted MySQL table running the MyISAM or InnoDB storage engines.


Related Tutorials

What do you think about this article?

Rate this article
LayerStack Promotion
Need assistance?

Try this guide to receive free bundled services at signup on a new free account.

Sign Up

Your Feedback Is Important

We hope you’ll give the new products and updates a try. If you have an idea for improving our products or want to vote on other user ideas so they get prioritized, please submit your feedback on our Community platform. And if you have any questions, please feel free to ask in the Community or contact our Technical Support team.