{"id":605,"date":"2019-02-12T22:19:35","date_gmt":"2019-02-13T03:19:35","guid":{"rendered":"http:\/\/www.buddroyce.com\/?p=605"},"modified":"2022-04-19T03:03:40","modified_gmt":"2022-04-19T03:03:40","slug":"recovering-from-a-mysql-innodb-crash","status":"publish","type":"post","link":"https:\/\/www.buddroyce.com\/?p=605","title":{"rendered":"Recovering from a MySQL InnoDB crash"},"content":{"rendered":"\n<p class=\"has-drop-cap\">On Feb 9th 2019, the datacenter where the Virtual Private Server that I use for this site(and many others) experienced a bit of a hardware failure which required some emergency maintenance.<\/p>\n\n\n\n<p>This happened overnight and when I woke up, I found my inbox with 100+ emails alerts from the various sites that I manage indicating an error with connecting to the database.<\/p>\n\n\n\n<p>Normally this isn&#8217;t a huge issue but this time things were different. The MySQL server not only wouldn&#8217;t start, it also didn&#8217;t provide any logs in either the MySQL error log or the system error log. I didn&#8217;t have any recent backups and neither did the datacenter, so this meant that had to do some serious guess work.<\/p>\n\n\n\n<p>If that sounds similar to what you have to deal with, don&#8217;t worry. It&#8217;s fixable.<\/p>\n\n\n\n<p class=\"has-huge-font-size\">The Fix<\/p>\n\n\n\n<p>Before you start screwing around with a live production database, unless you have daily or weekly backups that you would be ok with, the first thing you&#8217;re going to want to do is extract whatever data you can.<\/p>\n\n\n\n<p>To do that, we&#8217;re going to boot the MySQL in forced recovery mode. There&#8217;s a line in your mysql my.cnf that says &#8220;innodb_forced_recovery = 0&#8221;<\/p>\n\n\n\n<p>Change that 0 value to 1 or just add that line if you don&#8217;t have it.<\/p>\n\n\n\n<p>This may or may not work depending on the issue. If it doesn&#8217;t work, try a value of 2 or 3. If recovery levels 1-3 won&#8217;t work for you, there&#8217;s still hope as there&#8217;s also levels 4-6, however, do understand that recovery levels 4-6 may cause data loss with level 6 resulting in guaranteed data loss. So work your way up from level 1 and pray to god that your database boots up in the earlier levels.<\/p>\n\n\n\n<p>In my case, I had to use recovery level 4 which puts me in a risky position but at least it booted.<\/p>\n\n\n\n<p>Once you find a level that boots, it&#8217;s time to backup and restore your databases\/tables<br><\/p>\n\n\n\n<p class=\"has-large-font-size\">Backup and restore MySQL<\/p>\n\n\n\n<p>For this you have two options. Either export the databases using phpmyadmin or use mysqldump.<\/p>\n\n\n\n<p>Both tools will fail once it reaches a table that&#8217;s corrupted so your mileage may vary.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">To dump ALL databases run<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">mysqldump &#8211;all-databases &#8211;single-transaction &#8211;quick &#8211;lock-tables=false &gt; someName-full-backup-$(date +%F).sql -u root -p<\/p>\n\n\n\n<p class=\"has-medium-font-size\">To dump one specific database run<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">mysqldump -u username -p databaseName &#8211;single-transaction &#8211;quick &#8211;lock-tables=false &gt; databaseName-backup-$(date +%F).sql<\/p>\n\n\n\n<p class=\"has-medium-font-size\">To dump one specific table run<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">mysqldump -u username -p &#8211;single-transaction &#8211;quick &#8211;lock-tables=false databaseName tableName &gt; databaseName-tableName-$(date +%F).sql<\/p>\n\n\n\n<p>Once you have everything backed up, this is where it may be easy or not as it depends on what the issue is and what innodb recovery level you had to use. <\/p>\n\n\n\n<p class=\"has-large-font-size\">Finding the faulty table<\/p>\n\n\n\n<p>Finding the faulty table<\/p>\n\n\n\n<p class=\"has-large-font-size\">Completely remove MySQL server and do a fresh re-install<\/p>\n\n\n\n<p>This step<\/p>\n\n\n\n<p>Remove MySQL completely (instructions for a Debian based system)<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">sudo apt-get remove &#8211;purge mysql\\*<br>sudo apt-get clean<br>sudo updatedb<\/p>\n\n\n\n<p>Reinstall MySQL (in my case it was 5.6 or 5.7) <br><br>Download the mysql repository configuration tool<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">wget https:\/\/dev.mysql.com\/get\/mysql-apt-config_0.8.12-1_all.deb<\/p>\n\n\n\n<p>(you can get whatever the latest version is at <br>http:\/\/dev.mysql.com\/downloads\/repo\/apt\/ )<br><br>Install the tool using<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">dpkg -i mysql-apt-config_0.8.9-1_all.deb<\/p>\n\n\n\n<p>You get the option of selecting versions 5.6 or 5.7. I went with 5.6 because that&#8217;s what was running before.<br><br>Update the apt repo list and install the damn server<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">apt-get update<br>apt-get install mysql-community-server<\/p>\n\n\n\n<p>Re-install the mysqli connector (in my case I needed it for php5.6 and php7)<br><\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">apt-get install php5-mysqlnd<br>apt-get install php-mysql<\/p>\n\n\n\n<p>Note: you can get away with php5-mysql but for MySQL 5.6 or higher you&#8217;re going to want mysqlnd. (the ND stand for Native Driver).<\/p>\n\n\n\n<p>While it php5-mysql and MySQL 5.6+ will most likely work, depending on the level of error reporting, you may get a LOT of notices about incorrect minor versions. It&#8217;s a problem I ran into before and it&#8217;s more annoying than anything.<\/p>\n\n\n\n<p class=\"has-medium-font-size\">To restore a backup:<\/p>\n\n\n\n<p>For full backups<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">mysql -u root -p &lt; full-backup.sql<\/p>\n\n\n\n<p>For a specific database<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">mysql -u [username] -p databaseName &lt; databaseName-backup.sql<\/p>\n\n\n\n<p>For a specific table<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-very-light-gray-background-color has-text-color has-background\">mysql -u [username] -p databaseName &lt; databaseName-tableName.sql<br><\/p>\n\n\n\n<p>Understanding the InnoDB recovery levels<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>On Feb 9th 2019, the datacenter where the Virtual Private Server that I use for this site(and many others) experienced a bit of a hardware failure which required some emergency maintenance. This happened overnight and when I woke up, I found my inbox with 100+ emails alerts from the various sites that I manage indicating [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-605","post","type-post","status-publish","format-standard","hentry","category-it-and-tech"],"_links":{"self":[{"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=\/wp\/v2\/posts\/605","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=605"}],"version-history":[{"count":7,"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions"}],"predecessor-version":[{"id":680,"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=\/wp\/v2\/posts\/605\/revisions\/680"}],"wp:attachment":[{"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buddroyce.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}