{"id":1193,"date":"2024-07-11T21:42:01","date_gmt":"2024-07-11T19:42:01","guid":{"rendered":"https:\/\/sven-seeberg.de\/wp\/?p=1193"},"modified":"2024-07-11T21:47:38","modified_gmt":"2024-07-11T19:47:38","slug":"recovering-crashed-innodb-indexes","status":"publish","type":"post","link":"https:\/\/sven-seeberg.de\/wp\/?p=1193","title":{"rendered":"Recovering crashed MariaDB InnoDB Tables\/Indexes"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Today I came across a crashed InnoDB MariaDB database. When trying to restart the process, it immediately failed with the following messages in the systemd journal:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Note] Starting MariaDB 10.11.6-MariaDB-0+deb12u1 source revision  as process 661197\n&#91;Note] InnoDB: Compressed tables use zlib 1.2.13\n&#91;Note] InnoDB: Number of transaction pools: 1\n&#91;Note] InnoDB: Using crc32 + pclmulqdq instructions\n&#91;Note] InnoDB: Using liburing\n&#91;Note] InnoDB: Initializing buffer pool, total size = 128.000MiB, chunk size = 2.000MiB\n&#91;Note] InnoDB: Completed initialization of buffer pool\n&#91;Note] InnoDB: File system buffers for log disabled (block size=4096 bytes)\n&#91;Note] InnoDB: Starting crash recovery from checkpoint LSN=10862992357\n&#91;Note] InnoDB: End of log at LSN=10868013717\n&#91;Note] InnoDB: Retry with innodb_force_recovery=5\n&#91;ERROR] InnoDB: Plugin initialization aborted with error Data structure corruption\n&#91;Note] InnoDB: Starting shutdown...\n&#91;ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.\n&#91;Note] Plugin 'FEEDBACK' is disabled.\n&#91;ERROR] Unknown\/unsupported storage engine: InnoDB\n&#91;ERROR] Aborting\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Before continuing with any recovery process, I created a backup of \/var\/lib\/mysql:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cp -rp \/var\/lib\/mysql \/var\/lib\/mysql.bak<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Some posts recommended to start MariaDB with <a href=\"https:\/\/mariadb.com\/kb\/en\/innodb-recovery-modes\/\">innodb_force_recovery<\/a> = 4. However, as you can see in the messages, MariaDB automatically tried to recover with innodb_force_recovery = 5 and failed. I manually set the recovery mode to 6 in the \/etc\/mysql\/mariadb.cnf:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;mysqld]<br>innodb_force_recovery = 6<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With that, MariaDB finally started. Sadly, this did not yet fully resolve the issue. In mode 6, MariaDB starts with all tables in read only mode. That means I was able to access the data, but the database was not really usable or fixed. The obvious next step was to do a mysqldump, clean the database and then insert all data again. Running mysqldump failed with the next error:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mysqldump: Error 1034: Index for table 'mytable' is corrupt; try to repair it when dumping table mytable at row: 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Interestingly, dumping a single table did work. If anyone knows why, please let me know. I dumped a list of all table names in the database into a file called \/root\/tables, then with bash iterated over all tables and dumped each one into a dedicated file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while read TABLE; do<br>   mysqldump -f mydatabase $TABLE &gt; \/root\/mydatabase\/$TABLE-$(date -I).sql;<br>done &lt; \/root\/tables<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">All files can then just be concatenated into one large dump file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat \/root\/mydatabase\/* &gt; \/root\/mydatabase.sql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After the database content was safely stored in a text sql dump file, I bootstrapped a new \/var\/lib\/mysql directory:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rm -rf \/var\/lib\/mysql<br>apt install --reinstall mariadb-server<br>sudo -u mysql mariadb-install-db<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then I created the database again and inserted the dump:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MariaDB &#91;(none)]&gt; CREATE DATABASE mydatabase;\nMariaDB &#91;(none)]&gt; use mydatabase;\nMariaDB &#91;mydatabase]&gt; source \/root\/mydatabase.sql<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After this, the database was healthy again.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I came across a crashed InnoDB MariaDB database. When trying to restart the process, it immediately failed with the following messages in the systemd journal: Before continuing with any recovery process, I created a backup of \/var\/lib\/mysql: Some posts &hellip; <a href=\"https:\/\/sven-seeberg.de\/wp\/?p=1193\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1193","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1193"}],"version-history":[{"count":8,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1193\/revisions"}],"predecessor-version":[{"id":1202,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1193\/revisions\/1202"}],"wp:attachment":[{"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sven-seeberg.de\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}