Table of Content
    MySQL Database Repair

    How to Fix MySQL Server Database Corruption?


    Table of Content

      Summary: There are various factors that can lead to corruption in MySQL database. This post outlines the potential reasons behind corruption in the MySQL Server database and mentions some effective methods to fix MySQL database corruption. It also mentions an advanced MySQL repair software that can repair corrupt MySQL database without any data loss.

      Like other databases, MySQL database is also susceptible to corruption and inconsistencies issues. The database can get corrupted due to several internal and external factors. In case of corruption in the database, you may experience random corruption-related errors, like “Table ‘.\mysql\user’ is marked as crashed and should be repaired”, “Index for table ‘.\mysql\user’ is corrupt; try to repair it,” etc. when you try to access the database. Sometimes, you even fail to open the database. In this article, we will discuss the causes behind MySQL database corruption and see how to fix corruption in the database.

      What Causes Corruption in MySQL Database?

      Following are some common reasons behind corruption in MySQL Server database:

      • Issues in the hard disk where the database is saved. 
      • Sudden restart of MySQL server instance.
      • Bugs in MySQL code.
      • MySQL process gets killed in the middle of writing data to the hard disk.
      • System crashes due to sudden power failure.
      • Insufficient storage space on the hard disk.
      • Malware infection in the system hosting the database

      Solutions to Fix MySQL Server Database Corruption

      Before proceeding to the repairing process, you need to ensure whether the database is corrupted or not. You can use the CHECK TABLE command to check corruption in MySQL database. This command supports databases created in both InnoDB and MyISAM engines. If there is corruption in the MySQL database, the first option you can try is to restore the database from the last known good backup. If the database backup is obsolete or not available, then follow the below methods to repair and restore the corrupt MySQL database.

      Method 1 – Use mysqlcheck Utility

      mysqlcheck is a command-line utility that is used to check, repair, optimize, and analyze tables in the MySQL database. To repair the corrupt table using mysqlcheck utility, open the command-line terminal on the system hosting the MySQL Server and execute the following command:

      mysqlcheck --r database_name table_name

      In above command, --r is the repair option. It will try to resolve all the issues in table, except the unique keys.
      Note:
      If you run the above command without table_name, the entire database will be
      repaired.

      Method 2 – Rebuild your Database

      If there is corruption in MySQL database, then you can also rebuild the database. The rebuilding methods vary depending on the search engine in which the database is created.

      Rebuild InnoDB Database

      If you are using InnoDB search engine, then you can use the Dump and Reload method to rebuild the tables in the MySQL database. This can help in rebuilding the InnoDB tables in MySQL database and also the indexes within the tables. Here’s how:

      Step 1: Restart the MySQL Service

      You need to first start the MySQL service by following these steps:

      • In the Run window, type services.msc.
      • In the Services window, search for the MySQL Service, and right-click on it.
      • Click Restart service.

      Step 2: Use Innodb_force_recovery Option

      When you try to access the damaged or corrupt InnoDB tables, MySQL server may crash or fail to start. If you face trouble while restarting the MySQL service, you can use the InnoDB_force_recovery option to start the MySQL service. To enable the Innodb_force_recovery option from the configuration file, follow these steps:

      • Search for the configuration file (my.cnf).

      Note: The my.cnf file’s location varies based on the operating system installed. In Windows system, the configuration file is located in ‘/etc’ directory. The default path is /etc/mysql/my.cnf.

      • Once you found the my.cnf file, go to the [mysqld] section and then add the below statements:
                     [mysqld]
                     Innodb_force_recovery=1
                     service mysql restart
      • Save and close the MySQL configuration file and then try restarting the MySQL service again.

      Note: The innodb_force_recovery option is set to ‘0’ by default. To start InnoDB and dump MySQL tables, you need to set the value to ‘1’ and increase the value incrementally (from 1 to 6). Dumping tables with value of 4 or higher can lead to data loss. So, take the database backup before proceeding.

      Step 3 – Dump and Reload the Table Data

      Once you enable the innodb_force_recovery, you will be able to access the corrupt table. Now, dump the table data by using the mysqldump command as given below:

      mysqldump -u user -p database_name table_name > single_dbtable_dump.sql

      Next, export all the databases to the dump.sql file by executing the below command:

      mysqldump --all-databases --add-drop-database --add-drop-table > dump.sql

      Now, restart the MySQL Server and use the DROP DATABASE command to delete the database. This command requires DROP privileges. So, check and grant them.

      If the above command fails to drop the database, then run the below commands to delete the database manually:

      cd /var/lib/mysql
      rm -rf db_name

      Next, disable the InnoDB recovery mode by commenting on the following line in [mysqld]:

      #innodb_force_recovery=...

      Now, save the applied changes to the configuration (my.cnf) file and then restart the MySQL Server.

      Rebuild MyISAM Database

      If you are using MyISAM search engine, then you can use myisamchk command to repair or rebuild the MySQL database. The ‘myisamchk’ command checks all the MyISAM tables and repairs them. It supports .MYD and .MYI files in tables. It also repairs data and indexes in the tables. To run the myisamchk command, follow these steps:

      • Stop the MySQL Server.
      • Then, use the following command to repair MyISAM table:      
      myisamchk –recover TABLE
      • Start the MySQL server.

      For detailed information, read ‘How to repair MyISAM table by using myisamchk?

      Alternatively, you can use the REPAIR TABLE command to rebuild the MyISAM table. This command supports MyISAM, ARCHIVE, and CSV tables. To repair the corrupt MyISAM table, run the below command:

      REPAIR TABLE table name;

      Method 3 – Use a Third-Party MySQL Database Repair Software

      If the above repair methods fail, then you can use an advanced MySQL database repair software, such as Stellar Repair for MySQL to repair the corrupt database and restore all its objects. The software can repair tables of both InnoDB and MyISAM storage engines and save them in a new database file. It can help you recover all the data, including tables and indexes, from corrupt databases with complete integrity. The tool is compatible with Windows and Linux operating systems.

      Some key features of Stellar Repair for MySQL:

      • Repairs MySQL database tables created using InnoDB (.frm, .ibdata, or .idb) and MyISAM (.frm, .myd, or .myi) storage engines.
      • Batch repairs multiple MySQL databases in a single process.
      • Recovers all the database objects, including keys, tables, table properties, data types, views, and triggers.
      • Previews recoverable database objects.
      • Allows to save the repaired database in multiple file formats, such as MySQL, CSV, HTML, and XLS.
      • Supports MySQL 8.x, 6.x, 5.x, 4.x, and 3.x.

      End Notes

      MySQL database can get corrupted due to several reasons. The repair methods discussed in this post can help you to easily and effectively repair the corrupted database. In case of severe corruption, you can use Stellar Repair for MySQL. It is a purpose-built MySQL repair software that can easily repair both InnoDB and MyISAM tables with complete precision and integrity. It can help you restore all the database objects, such as tables, indexes, log system tables, etc., in the original format. The software supports both Windows and Linux systems.

      You can back up a MySQL database using the mysqldump utility. Here is the command: 'mysqldump -u root -p db_name > dump.sql'
      The InnoDB search engine works with a crash recovery mechanism, which crashes the MySQL Server automatically when it detects corrupt tables. To rebuild the corrupt InnoDB tables, you can restart the server using the InnoDB_force_recovery option. This allows to set the innodb_force_recovery settings to manage MySQL behavior during startup. You can set these settings manually to permit the MySQL Server to start even if it detects corruption in the tables. It helps you access and repair the corrupt InnoDB tables
      To prevent corruption in MySQL database, o Make sure Windows and MySQL Server are updated. o Make sure your system has a powerful and application-aware antivirus program to prevent malicious attacks. o Ensure you have an uninterrupted power supply/UPS to avoid sudden power failure.

      Was this article helpful?

      No NO

      About The Author

      Monika Dadool linkdin

      Monika Dadool is a Senior Content Writer at Stellar with over 5 years of experience in technical writing. She is a tech enthusiast and expert who specializes in writing about SQL Server, MySQL Server, MariaDB Server, Microsoft Access, Active Directory, email recovery, Microsoft 365, pattern recognition, machine learning, data recovery, file repair, and operating systems like Linux, Windows, and Mac. She also writes about accounting software such as QuickBooks and Sage 50, as well as web-scripting languages like HTML, JavaScript, Python, PHP, Visual Basic, ASP.NET, and AJAX. Monika is passionate about researching and exploring new technologies, and she enjoys developing engaging technical blogs that help organizations and database administrators resolve various issues. When she's not creating content, you can find her on social media, watching web series, reading books, or exploring new food recipes.

      Leave a comment

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

      Image Captcha
      Refresh Image Captcha

      Enter Captcha Here :

      Related Posts

      WHY STELLAR® IS GLOBAL LEADER

      Why Choose Stellar?

      • 0M+

        Customers

      • 0+

        Years of Excellence

      • 0+

        R&D Engineers

      • 0+

        Countries

      • 0+

        PARTNERS

      • 0+

        Awards Received