Mazzy
Mazzy

Reputation: 14179

Trying to repair a corrupted InnoDB table on MariaDB

I have a corrupted MariaDB database. The corruption was due to a power outage.

I can't boot the MariaDB without putting innodb_force_recovery. I've set it up to 3.

It's InnoDB so I can't repair it. It should do it on your own.

When I try to dump the table, I got the following error:

mysqldump: Error 1034: Index for table 'statistics' is corrupt; try to repair it when dumping table `statistics` at row: 36157

At this point I'm hitting the wall because I can't figure out how to fix the index.

Tried to run as recommended by Georg mariadb-check command and I got the following error:

2022-06-03 15:08:15 4 [ERROR] InnoDB: Space id and page no stored in the page, read in are [page id: space=201, page number=30781], should be [page id: space=201, page number=32829]
2022-06-03 15:08:15 4 [ERROR] InnoDB: Space id and page no stored in the page, read in are [page id: space=201, page number=30721], should be [page id: space=201, page number=32769]
2022-06-03 15:08:15 4 [ERROR] [FATAL] InnoDB: Trying to read page number 2188681215 in space 201, space name homeassistant/states, which is outside the tablespace bounds. Byte offset 0, len 16384
220603 15:08:15 [ERROR] mysqld got signal 6 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
To report this bug, see https://mariadb.com/kb/en/reporting-bugs
We will try our best to scrape up some info that will hopefully help
diagnose the problem, but since we have already crashed,
something is definitely wrong and this may fail.
Server version: 10.4.19-MariaDB-1:10.4.19+maria~focal
key_buffer_size=16777216
read_buffer_size=262144
max_used_connections=1
max_threads=66
thread_count=2
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 68686 K  bytes of memory
Hope that's ok; if not, decrease some variables in the equation.
Thread pointer: 0x7f3c000c48
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
stack_bottom = 0x7f843028b8 thread_stack 0x30000
mysqld(my_print_stacktrace+0x30)[0x55886ff4c0]
mysqld(handle_fatal_signal+0x49c)[0x55882085b4]
linux-vdso.so.1(__kernel_rt_sigreturn+0x0)[0x7f87a4378c]
/lib/aarch64-linux-gnu/libc.so.6(gsignal+0xe0)[0x7f870fa138]
/lib/aarch64-linux-gnu/libc.so.6(abort+0x110)[0x7f870e6d68]
mysqld(+0xb241a0)[0x55884d41a0]
mysqld(+0x5a1724)[0x5587f51724]
mysqld(+0xbc3728)[0x5588573728]
mysqld(+0xb81184)[0x5588531184]
mysqld(+0xb5e44c)[0x558850e44c]
mysqld(+0xb39644)[0x55884e9644]
mysqld(+0x59d0d8)[0x5587f4d0d8]
mysqld(+0xb9fa84)[0x558854fa84]
mysqld(+0xba0008)[0x5588550008]
mysqld(+0xb913e8)[0x55885413e8]
mysqld(+0x9ffcb4)[0x55883afcb4]
mysqld(_ZN7handler7ha_openEP5TABLEPKcijP11st_mem_rootP4ListI6StringE+0x58)[0x558820e0b8]
mysqld(_Z21open_table_from_shareP3THDP11TABLE_SHAREPK25st_mysql_const_lex_stringjjjP5TABLEbP4ListI6StringE+0x628)[0x55880d5f30]
mysqld(_Z10open_tableP3THDP10TABLE_LISTP18Open_table_context+0x6dc)[0x5587fcb1f4]
mysqld(_Z11open_tablesP3THDRK14DDL_options_stPP10TABLE_LISTPjjP19Prelocking_strategy+0x9f8)[0x5587fce378]
mysqld(_Z20open_and_lock_tablesP3THDRK14DDL_options_stP10TABLE_LISTbjP19Prelocking_strategy+0x38)[0x5587fceb30]
mysqld(+0x758608)[0x5588108608]
mysqld(+0x7595dc)[0x55881095dc]
mysqld(_ZN19Sql_cmd_check_table7executeEP3THD+0x8c)[0x558810b24c]
mysqld(_Z21mysql_execute_commandP3THD+0xd80)[0x5588024370]
mysqld(_Z11mysql_parseP3THDPcjP12Parser_statebb+0x1f8)[0x558802a200]
mysqld(_Z16dispatch_command19enum_server_commandP3THDPcjbb+0xfb0)[0x558802c008]
mysqld(_Z10do_commandP3THD+0xfc)[0x558802de34]
mysqld(_Z24do_handle_one_connectionP7CONNECT+0x290)[0x55880feaf8]
mysqld(handle_one_connection+0x44)[0x55880fec1c]
/lib/aarch64-linux-gnu/libpthread.so.0(+0x84fc)[0x7f874e24fc]
/lib/aarch64-linux-gnu/libc.so.6(+0xd467c)[0x7f8719767c]
Trying to get some variables.
Some pointers may be invalid and cause the dump to abort.
Query (0x7f3c0118f0): CHECK TABLE `events`
Connection ID (thread ID): 4
Status: NOT_KILLED
Optimizer switch: index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=on,table_elimination=on,extended_keys=on,exists_to_in=on,orderby_uses_equalities=on,condition_pushdown_for_derived=on,split_materialized=on,condition_pushdown_for_subquery=on,rowid_filter=on,condition_pushdown_from_having=on
The manual page at https://mariadb.com/kb/en/how-to-produce-a-full-stack-trace-for-mysqld/ contains
information that should help you find out what is causing the crash.
Writing a core file...
Working directory at /var/lib/mysql
Resource Limits:
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            8388608              unlimited            bytes
Max core file size        unlimited            unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             unlimited            unlimited            processes
Max open files            1048576              1048576              files
Max locked memory         65536                65536                bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       29948                29948                signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us
Core pattern: |/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h

Upvotes: 2

Views: 16646

Answers (3)

Jesse
Jesse

Reputation: 986

Many of my errors were very similar to this situation. But for me, any innodb_force_recovery level would result in a recovery that occupied all CPU and it would never be able to start. Removing the ib_logfile MariaDB file wouldn't work as did work with others.

So, alternatively, it could help some situations to simply disable InnoDB, which worked in my case.

Per this answer, under:

[mysqld]

Add this, instead of innodb_force_recovery=:

innodb=OFF
default_storage_engine=MyISAM

I was running Arch, and grep revealed that there was already a [mysqld] statement in /etc/my.cnf.d/server.cnf, so I put it there.

systemctl start mysql worked like a charm, I entered from the CLI, and I got my work done.

Upvotes: 0

akuzminsky
akuzminsky

Reputation: 2258

Trying to read page number 2188681215 in space 201, space name homeassistant/states, which is outside the tablespace bounds. Byte offset 0, len 16384

Here one of pages in the primary index of the homeassistant/states table stored incorrect values, so InnoDB crashes when it accesses the page.

If InnoDB starts with any innodb_force_recovery > 0 but crashes during a mysqldump operation you can try to skip corrupted pages:

#!/bin/bash

# Adjust your values here
host=localhost
user=root
db=test
table=test
PK=id
newtable=newtest2
# end of user defined values

read -p "Password: " -s password
echo ""
if [ "xxx$password" = "xxx" ]
then
        pass_cmd=""
else
        pass_cmd="-p$password"
fi
mysql -h $host -u $user $pass_cmd -e "show databases" $db
if [ $? -eq 0 ]
then
        echo "Credentials are ok!"
else
        echo "Can't run query SHOW DATABASES"
        echo "Check what's wrong"
        exit
fi
minPK=`mysql -sN -h $host -u $user $pass_cmd -e "SELECT MIN($PK) FROM $table" $db`
maxPK=`mysql -sN -h $host -u $user $pass_cmd -e "SELECT MAX($PK) FROM $table" $db`
a=$minPK
b=$maxPK
let chunk=$b-$a

function insert_range {
q="INSERT IGNORE INTO \`$newtable\` SELECT * FROM \`$table\` WHERE \`$PK\` >= $1 and \`$PK\` <= $2"
mysql -h $host -u $user $pass_cmd -e "$q" $db 2>/dev/null
}
successful_tries=0
echo "Primary key ($PK) range: $minPK .. $maxPK"
while true
do
        insert_range $a $b
        if [ $? -eq 0 ]
        then
                echo "Good, primary key range: $a .. $b"
                let a=$b+1
                let b=$b+$chunk
                let successful_tries=$successful_tries+1
        else
                echo "Bad luck, primary key range: $a .. $b"
                let chunk=$chunk/2
                let b=$a+$chunk
                mysqladmin -h $host -u $user $pass_cmd ping > /dev/null 2>&1
                while [ $? -ne 0 ]
                do
                        mysqladmin -h $host -u $user $pass_cmd ping > /dev/null 2>&1
                done
                successful_tries=0
        fi
        if [ $a -gt $maxPK ]
        then
                exit
        fi
        if [ $chunk -eq 0 ]
        then
                let a=$a+1
                let b=$a+1
                let chunk=1
        fi
        # If things are going well, increase pace
        if [ $successful_tries -gt 5 ]
        then
                let chunk=$chunk*2
        fi
done

P.S.

I wonder if it's really the primary index. If it's actually the secondary index you can drop it, reload the table and then create the secondary indexes again.

[1] https://github.com/twindb/undrop-for-innodb/blob/develop/fetch_data.sh

Upvotes: 1

Georg Richter
Georg Richter

Reputation: 7476

  1. Make a backup

  2. Check which tables might have corrupted indexes: mariadb-check -c database

  3. Force a rebuild with OPTIMIZE TABLE corrupted_table

Upvotes: 2

Related Questions