Methods to Recover Corrupted MySQL Database

MySQL is the Relational Database Management System that is developed by Oracle and used by corporations whether small or big extensively. It is especially used by web developers to handle their web page content and e-commerce websites to manage the traffic on their pages.

It is open-source in nature, and its feature-rich infrastructure is available for creating responsive web applications and complex B2B data-driven services. Many tech giants like Twitter, Facebook, Wikipedia, and YouTube take the services of MySQL database to manage their applications.

Due to its capability to remain compatible with all platforms, it is used extensively to create a large database and saves data in several file formats like .sql, .cnf, .arm, .qbquery, .ddl, etc.

There are different chances that a database file may become corrupt due to malware or spyware corruption. So, Oracle has given a detailed description of how the corrupt database can be repaired and useful to save new data again. First, you should check that how the database starts to show the anomaly-

  1. The database is downgraded automatically.
  2. There are unwanted changes to the indexes of data.
  3. A database table is showing inconsistencies.

Depending on the database state, there can be multiple troubleshooting methods for repairing it-

Check the table for errors

When MySQL is running, then you can run a query inside the table –

CHECK TABLE <table-name>;

Note: Depending on the type of table, there CHECK TABLE command will run separate checks on them.

  • For MyISAM tables, the CHECK TABLE will run a MEDIUM CHECK.
  • For InnoDB tables, the CHECK TABLE will run a FOR UPGRADE and QUICK scan.

After running the query, the status of the table will be shown in several columns like Table, Op, Msg_type, and Msg_text
If Msg_type shows the status as ‘error’ and Msg_text shows ‘corrupt’, then you should run the next query to repair the table.

REPAIR TABLE <table-name>;

After repairing the table, the Msg_type will show an OK status.

Note: If the MySQL server is shut down during the repair process, then do not worry. It will resume after restarting the server. But, you should make sure that you are not running any additional queries on the same table.

Check using mysqlcheck

For users in the Linux terminal, they can run mysqlcheck to repair the database. First, change the directory level to the location of the database file.

  1. The root user should change the directory to the location where the database is saved.
    Sudo su
    Cd /var/lib/mysql
  2. Check the entire database file
    Mysqlcheck <database-name>

You can also check a table inside the database file

Mysqlcheck <database-name> <database-table-name>

After checking the database for corruption, you can start the repair process for the database. But, first, you should take the backup of the database.

“cp -rfv /var/lib/mysql /var/lib/mysql_copy””

The above command will create the same file with a different name. So, you should save the new file in a different drive or folder.

Repair the database

In an active database, you can start the repair process by writing a query.

REPAIR TABLE <table-name>

The query will reset the database and repair the complete database. Then, its status will be OK.

Repair using mysqlcheck
  1. In the Linux terminal, you should change the directory to the location of the database file.
    cd /var/lib/mysql
  2. Run mysqlcheck scan with –r parameter. It will run the repair process for the database.
    mysqlcheck -r <database-name> <database-table-name>

The command will repair the table of database cleanly.

Conclusion

The tutorial has given the simple methods to scan corrupt Mysql databases like MyISAM and InnoDB. You can scan and later repair the database file, but the methods have already asked you to take a backup of the database as it may become more corrupt or lose already present data.

The backup file taken from a corrupt database may contain traces of corruption from malware or spyware. So, the manual recovery method is not a suitable choice for a thorough scan and recovery of the Mysql database.

A professional Mysql database recovery software is the powerful alternate that can scan a corrupt database and recover all tables. The relationships and dependencies between the tables will be maintained. Some important properties like primary keys, foreign keys, indexes, triggers, and other parts will recover from corruption and become useful again. After recovering the database, you can connect it with the server and run further queries.

About Elias Macclure

Elias has a sleek writing style, and the reader may find that the extensive nuances of the material are covered in the article. He changes the writing style according to the platform and writes fine for Office 365 and SharePoint. He is an active reader and reads books by renowned writers including Charles Dickens, Sidney Sheldon and Agatha Christie.

Leave a Reply

Your email address will not be published. Required fields are marked *

6  +  2  =