<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Guguweb - innodb</title><link href="https://www.guguweb.com/" rel="alternate"></link><link href="https://www.guguweb.com/feeds/innodb.atom.xml" rel="self"></link><id>https://www.guguweb.com/</id><updated>2022-01-14T20:57:45+01:00</updated><subtitle>Freelance developer and sysadmin</subtitle><entry><title>How to repair a corrupted MySQL database</title><link href="https://www.guguweb.com/2020/02/06/how-to-repair-a-corrupted-mysql-database/" rel="alternate"></link><published>2020-02-06T12:11:13+00:00</published><updated>2022-01-14T20:57:45+01:00</updated><author><name>Augusto Destrero</name></author><id>tag:www.guguweb.com,2020-02-06:/2020/02/06/how-to-repair-a-corrupted-mysql-database/</id><summary type="html">&lt;p&gt;In this post I&amp;#8217;ll explain how to repair a corrupted MySQL database. Especially on large traffic database servers, data corruption can occur, for instance due to hard disk or network failures.&lt;/p&gt;
&lt;p&gt;In a previous post I described how to perform a &lt;a href="https://www.guguweb.com/2020/01/30/mysql-incremental-backup-with-mysqldump-and-rdiff-backup/"&gt;MySQL incremental backup&lt;/a&gt; that will keep you safe …&lt;/p&gt;</summary><content type="html">&lt;p&gt;In this post I&amp;#8217;ll explain how to repair a corrupted MySQL database. Especially on large traffic database servers, data corruption can occur, for instance due to hard disk or network failures.&lt;/p&gt;
&lt;p&gt;In a previous post I described how to perform a &lt;a href="https://www.guguweb.com/2020/01/30/mysql-incremental-backup-with-mysqldump-and-rdiff-backup/"&gt;MySQL incremental backup&lt;/a&gt; that will keep you safe from data losses, but sometimes a backup is not enough.&lt;/p&gt;
&lt;p&gt;Consider this scenario: your web application ran smoothly on MySQL until today. Running a simple query to read data from tables is crashing MySQL instance. The latest backup was taken yesterday, so you propose your client to perform a full restore of the database from yesterday&amp;#8217;s backup. Backups are made exactly for that, right?&lt;/p&gt;
&lt;p&gt;Nope! Your client says that today they worked many hours on the data, before the crash occurred. They cannot accept to repeat all the work and ask you to solve the problem quickly. The only solution here is trying to retrieve data from the corrupt tables.&lt;/p&gt;
&lt;div class="toc"&gt;&lt;span class="toctitle"&gt;Table of Contents&lt;/span&gt;&lt;ul&gt;
&lt;li&gt;&lt;a href="#1-how-to-repair-a-corrupted-mysql-database-using-mysqlcheck"&gt;1. How to repair a corrupted MySQL database using mysqlcheck&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#2-how-to-repair-a-corrupted-mysql-database-using-innodb-recovery-mode"&gt;2. How to repair a corrupted MySQL database using InnoDB recovery mode&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#3-how-to-repair-a-corrupted-mysql-database-using-stellar-repair-for-mysql"&gt;3. How to repair a corrupted MySQL database using Stellar Repair for MySQL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#4-conclusion"&gt;4. Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;h2 id="1-how-to-repair-a-corrupted-mysql-database-using-mysqlcheck"&gt;1. How to repair a corrupted MySQL database using mysqlcheck&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://dev.mysql.com/doc/refman/8.0/en/mysqlcheck.html"&gt;mysqlcheck&lt;/a&gt; is a tool distributed together with MySQL. As the documentation says:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;the mysqlcheck client performs table maintenance: It checks, repairs, optimizes, or analyzes tables.
&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You can try to issue the following command to repair corrupted tables:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;mysqlcheck --auto-repair -o --all-databases -uroot -p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Executing the above command started checking the tables (line-by-line), displaying OK at the end. Something like this:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;user@example.com:/$ mysqlcheck --auto-repair -o --all-databases -uroot -p
Enter password:
mydb.accounting_table1                     OK
mydb.accounting_table2                     OK
mydb.accounting_table3                     OK
mydb.accounting_table4                     OK
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Unfortunately mysqlcheck may hang when checking large databases for corrupt tables.&lt;/p&gt;
&lt;h2 id="2-how-to-repair-a-corrupted-mysql-database-using-innodb-recovery-mode"&gt;2. How to repair a corrupted MySQL database using InnoDB recovery mode&lt;/h2&gt;
&lt;p&gt;If your database uses InnoDB tables you can try to restart it in recovery mode, by stopping the mysql service and restarting it after adding the following options in my.cnf:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;innodb_force_recovery=3
innodb_purge_threads=0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Please read the manual on &lt;a href="https://dev.mysql.com/doc/refman/8.0/en/forcing-innodb-recovery.html"&gt;starting the InnoDB engine in recovery mode&lt;/a&gt; to have more details on the meaning of these options. Using those options can help you in restarting the crashed database server and if this is the case you will be able to perform a dump of the data:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;mysqldump --single-transaction --quick --skip-extended-insert \
    --routines -umyuser -pmysecret dbname &amp;amp;gt; /path/to/dumps/dir/dbname.dump;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you cannot restart your MySQL server in recovery mode, the last resort is using a commercial MySQL repair tool.&lt;/p&gt;
&lt;h2 id="3-how-to-repair-a-corrupted-mysql-database-using-stellar-repair-for-mysql"&gt;3. How to repair a corrupted MySQL database using Stellar Repair for MySQL&lt;/h2&gt;
&lt;p&gt;&lt;a class="aff" href="/r/stellar" target="_blank" rel="noopener noreferrer"&gt;Stellar Repair for MySQL&lt;/a&gt; is a commercial tool specifically designed to repair corrupted MySQL databases. As you can see from the product website it has the following benefits:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Easy-to-use user interface (UI), with detailed instructions, makes the repair process straightforward.&lt;/li&gt;
&lt;li&gt;Repairs corrupt MySQL InnoDB and MyISAM tables.&lt;/li&gt;
&lt;li&gt;Recovers all major database components including tables, table properties, data types, keys, triggers, etc.&lt;/li&gt;
&lt;li&gt;Repairs and recovers corrupt database files created in MySQL version 8.x, 6.x, 5.x, and older versions.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The software is produced by &lt;a href="https://www.stellarinfo.com/"&gt;Stellar Data Recovery Inc.&lt;/a&gt;, which is specialized in data recovery solutions and has &lt;a href="https://www.stellarinfo.com/company/about/data-restore-reviews.php"&gt;positive reviews from renowned sites&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The current price for the tool is &lt;strong&gt;$199&lt;/strong&gt;, but I think that those money are well spent for a tool like this because you&amp;#8217;ll save a lot of time in the long run. &lt;a class="aff" href="/r/stellar" target="_blank" rel="noopener noreferrer"&gt;Click here to download the tool&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Unfortunately the software currently doesn&amp;#8217;t run on Linux based Operating Systems, but you can indeed repair MySQL databases running on Linux servers by making a copying the data directory, usually located in &lt;strong&gt;/var/lib/mysql&lt;/strong&gt;. In my case I used a Windows Server virtual machine to run the tool.&lt;/p&gt;
&lt;p&gt;After purchasing a license of Stellar Repair and obtaining a copy of the software you can run it and it will present itself with a simple interface. On first run an &lt;b&gt;Instruction &lt;/b&gt;window will give you some info on how to prepare the data directory for repair. Then a &lt;b&gt;Select Data Folder&lt;/b&gt; window will appear:&lt;/p&gt;
&lt;div class="d-flex"&gt;
    &lt;figure class="figure mx-auto"&gt;
        &lt;img src="/images/2020/02/thumbnails/800x_/stellar_repair_select_datadir-e1580989549591.png" class="figure-img img-fluid rounded" alt="Select data folder window"&gt;
        &lt;figcaption class="figure-caption text-center"&gt;Select data folder window&lt;/figcaption&gt;
    &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;here you have to choose the version of MySQL that produced the data directory you want to repair. In my case it was MySQL 5.7. Click Browse to select the data directory you copied from the MySQL server (this should be a copy of &lt;strong&gt;/var/lib/mysql&lt;/strong&gt; if your DB server was running on Linux OS).&lt;/p&gt;
&lt;p&gt;Then a list of your databases will be presented. Here you have to choose only the database(s) that you want to repair, then click &amp;#8220;Repair&amp;#8221;:&lt;/p&gt;
&lt;div class="d-flex"&gt;
    &lt;figure class="figure mx-auto"&gt;
        &lt;img src="/images/2020/02/thumbnails/800x_/stellar_repair_repair-e1580989567670.png" class="figure-img img-fluid rounded" alt="Select the database(s) to repair"&gt;
        &lt;figcaption class="figure-caption text-center"&gt;Select the database(s) to repair&lt;/figcaption&gt;
    &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;On the left pane of the window you&amp;#8217;ll see all the databases together with the tables that have been repaired. You can click on each item to see the table data and properties on the right pane:&lt;/p&gt;
&lt;div class="d-flex"&gt;
    &lt;figure class="figure mx-auto"&gt;
        &lt;img src="/images/2020/02/thumbnails/800x_/stellar_repair_table_details.png" class="figure-img img-fluid rounded" alt="See the repaired table details"&gt;
        &lt;figcaption class="figure-caption text-center"&gt;See the repaired table details&lt;/figcaption&gt;
    &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;You can optionally filter the data you want to export by selecting the checkboxes appropriately. Then click on &amp;#8220;Save&amp;#8221; button in the toolbar:&lt;/p&gt;
&lt;div class="d-flex"&gt;
    &lt;figure class="figure mx-auto"&gt;
        &lt;img src="/images/2020/02/thumbnails/800x_/stellar_repair_save.png" class="figure-img img-fluid rounded" alt="Export the repaired data"&gt;
        &lt;figcaption class="figure-caption text-center"&gt;Export the repaired data&lt;/figcaption&gt;
    &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;You&amp;#8217;ll be presented with a window to choose the destination of the restored data. Here I choose to export in CSV format:&lt;/p&gt;
&lt;div class="d-flex"&gt;
    &lt;figure class="figure mx-auto"&gt;
        &lt;img src="/images/2020/02/thumbnails/800x_/stellar_repair_save_csv-e1580989498662.png" class="figure-img img-fluid rounded" alt="Export the repaired data in CSV format"&gt;
        &lt;figcaption class="figure-caption text-center"&gt;Export the repaired data in CSV format&lt;/figcaption&gt;
    &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;You can also export the restored data directly on a running instance of MySQL by providing the necessary connection parameters:&lt;/p&gt;
&lt;div class="d-flex"&gt;
    &lt;figure class="figure mx-auto"&gt;
        &lt;img src="/images/2020/02/thumbnails/800x_/stellar_repair_save_mysql-e1580989525481.png" class="figure-img img-fluid rounded" alt="Copy the repaired data on another MySQL running instance"&gt;
        &lt;figcaption class="figure-caption text-center"&gt;Copy the repaired data on another MySQL running instance&lt;/figcaption&gt;
    &lt;/figure&gt;
&lt;/div&gt;

&lt;p&gt;In order to save MySQL database version 8, MySQL 8.0 ODBC driver 32-bit must be pre-installed on your system. To do this you can download the &lt;a href="https://dev.mysql.com/downloads/installer/"&gt;Windows version of MySQL community edition&lt;/a&gt; (GPL licensed).&lt;/p&gt;
&lt;h2 id="4-conclusion"&gt;4. Conclusion&lt;/h2&gt;
&lt;p&gt;While MySQL InnoDB storage engine is more resistant to corruption compared to other storage engines, InnoDB tables can still become corrupt and lead to server crash.&lt;/p&gt;
&lt;p&gt;In this post I&amp;#8217;ve discussed some methods to repair a corrupt MySQL table:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;mysqlcheck&lt;/strong&gt; command can identify and repair corrupt tables;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;innodb_force_recovery&lt;/strong&gt; option can be used to restart the server in recovery mode and bring database up for retrieving the data;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stellar’s&lt;/strong&gt; &lt;a href="/r/stellar"&gt;MySQL repair tool&lt;/a&gt; can come in handy if all other methods failed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;Disclaimer: this fair review was kindly sponsored by Stellar Data Recovery Inc. who also gave me a license key for testing and reviewing their MySQL repair tool.&lt;/em&gt;&lt;/p&gt;</content><category term="sysadmin"></category><category term="innodb"></category><category term="mysql"></category><category term="repair"></category></entry></feed>