Why do we need to recover InnoDB dictionary
c_parser is
a tool from TwinDB
recovery toolkit that can read InnoDB page and fetch records out of it. Although it can scan any stream of bytes recovery quality is higher when you feed c_parser with
pages that belong to the PRIMARY index of the table. All InnoDB indexes have their identifiers a.k.a. index_id. The InnoDB dictionary stores correspondence between table name and index_id. That would be reason number one.
Another reason – it is possible to recover table structure from the InnoDB dictionary. When a table is dropped MySQL deletes respective .frm file. If you had neither backups nor table schema it becomes quite a challenge to recover the table structure. This
topic however deserves a separate post which I write some other day.
Let’s assume you’re convinced enough and we can proceed with InnoDB dictionary recovery.
Compiling TwinDB recovery toolkit
The source code of the toolkit is hosted on GitHub.
You will need git to get the latest revision, so make sure you have it:
Get the latest revision of the toolkit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#
git clone https://github.com/twindb/undrop-for-innodb.git
#
cd undrop-for-innodb/
[undrop-for-innodb]#
ll
total136
-rw-r--r--1rootroot 6271Jun2400:41check_data.c
-rw-r--r--1rootroot27516Jun2400:41c_parser.c
drwxr-xr-x2rootroot 4096Jun2400:41dictionary
drwxr-xr-x2rootroot 4096Jun2400:41include
-rw-r--r--1rootroot 1203Jun2400:41Makefile
-rw-r--r--1rootroot15495Jun2400:41print_data.c
drwxr-xr-x2rootroot 4096Jun2400:41sakila
-rw-r--r--1rootroot 5223Jun2400:41sql_parser.l
-rw-r--r--1rootroot21137Jun2400:41sql_parser.y
-rw-r--r--1rootroot22236Jun2400:41stream_parser.c
-rw-r--r--1rootroot 2237Jun2400:41tables_dict.c
-rwxr-xr-x1rootroot 6069Jun2400:41test.sh
[undrop-for-innodb]#
|
As prerequisites we would need gcc, flex and bison. Check that you have them:
|
|
[undrop-for-innodb]#
which gcc
/usr/bin/gcc
[undrop-for-innodb]#
which bison
/usr/bin/bison
[undrop-for-innodb]#
which flex
/usr/bin/flex
|
Good. Now let’s compile the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[undrop-for-innodb]#
make
gcc-g-O3 -I./include-cstream_parser.c
gcc-g-O3 -I./include -pthread-lmstream_parser.o-ostream_parser
#flex
-d sql_parser.l
flexsql_parser.l
#bison
-r all -o sql_parser.c sql_parser.y
bison-osql_parser.csql_parser.y
sql_parser.y:conflicts:5shift/reduce
gcc-g-O3 -I./include-csql_parser.c
gcc-g-O3 -I./include-cc_parser.c
gcc-g-O3 -I./include-ctables_dict.c
gcc-g-O3 -I./include-cprint_data.c
gcc-g-O3 -I./include-ccheck_data.c
gcc-g-O3 -I./include -pthread-lmsql_parser.oc_parser.otables_dict.oprint_data.ocheck_data.o-oc_parser
|
If there are no errors we are ready to proceed.
Splitting ibdata1
The InnoDB dictionary is stored in ibdata1. So we need to parse it and get pages that store records of the dictionary. stream_parser does
it.
|
|
[undrop-for-innodb]#
./stream_parser -f /var/lib/mysql/ibdata1
...
Sizetoprocess: 79691776(76.000MiB)
Allworkersfinishedin1sec
|
stream_parser finds
InnoDB pages in ibdata1 and stores them sorted by page type(FIL_PAGE_INDEX or FIL_PAGE_TYPE_BLOB)
by index_id.
Here’s the indexes:
SYS_TABLES
|
|
[undrop-for-innodb]#
ll pages-ibdata1/FIL_PAGE_INDEX/0000000000000001.page
-rw-r--r--1rootroot16384Jun2400:50pages-ibdata1/FIL_PAGE_INDEX/0000000000000001.page
|
SYS_INDEXES
|
|
[undrop-for-innodb]#
ll pages-ibdata1/FIL_PAGE_INDEX/0000000000000003.page
-rw-r--r--1rootroot16384Jun2400:50pages-ibdata1/FIL_PAGE_INDEX/0000000000000003.page
|
SYS_COLUMNS
|
|
[undrop-for-innodb]#
ll pages-ibdata1/FIL_PAGE_INDEX/0000000000000002.page
-rw-r--r--1rootroot49152Jun2400:50pages-ibdata1/FIL_PAGE_INDEX/0000000000000002.page
|
and SYS_FIELDS
|
|
[undrop-for-innodb]#
ll pages-ibdata1/FIL_PAGE_INDEX/0000000000000004.page
-rw-r--r--1rootroot16384Jun2400:50pages-ibdata1/FIL_PAGE_INDEX/0000000000000004.page
|
As you can see the dictionary is pretty small, just one page per index.
Dumping records from SYS_TABLES and SYS_INDEXES
To fetch records out of the index pages you need c_parser.
But first, let’s create directory for dumps
|
|
[undrop-for-innodb]#
mkdir -p dumps/default
|
InnoDB dictionary is always in REDUNDANT format,
so options -4 is
mandatory:
|
|
[undrop-for-innodb]#
./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/0000000000000001.page -t dictionary/SYS_TABLES.sql > dumps/default/SYS_TABLES 2> dumps/default/SYS_TABLES.sql
|
Here’s our sakila tables:
|
|
[undrop-for-innodb]#
grep sakila dumps/default/SYS_TABLES | head -5
0000000052D5 D9000002380110 SYS_TABLES "sakila/actor" 7534 1 0 80 "" 739
0000000052D8 DC0000014F0110 SYS_TABLES "sakila/address" 7548 1 0 80 "" 740
0000000052DB DF000002CA0110 SYS_TABLES "sakila/category" 7553 1 0 80 "" 741
0000000052DE E2000002F80110 SYS_TABLES "sakila/city" 7564 1 0 80 "" 742
0000000052E1 E5000002C50110 SYS_TABLES "sakila/country" 7573 1 0 80 "" 743
|
dumps/default/SYS_TABLES is
a dump of the table eligible for LOAD
DATA INFILE command. The exact command c_parsers prints
to standard error output. I saved it in dumps/default/SYS_TABLES.sql
|
|
[undrop-for-innodb]#
cat dumps/default/SYS_TABLES.sql
SETFOREIGN_KEY_CHECKS=0;
LOADDATAINFILE'/root/tmp/undrop-for-innodb/dumps/default/SYS_TABLES'REPLACEINTOTABLE`SYS_TABLES`FIELDSTERMINATEDBY'\t'OPTIONALLYENCLOSEDBY'"'LINESSTARTINGBY'SYS_TABLES\t'(`NAME`,`ID`,`N_COLS`,`TYPE`,`MIX_ID`,`MIX_LEN`,`CLUSTER_NAME`,`SPACE`);
|
The same way let’s dump SYS_INDEXES:
|
|
[undrop-for-innodb]#
./c_parser -4f pages-ibdata1/FIL_PAGE_INDEX/0000000000000003.page -t dictionary/SYS_INDEXES.sql > dumps/default/SYS_INDEXES 2> dumps/default/SYS_INDEXES.sql
|
Make sure we have sane result in the dumps
1
2
3
4
5
6
7
8
9
10
11
12
|
[undrop-for-innodb]#
head -5 dumps/default/SYS_INDEXES
--Pageid:11,Format:REDUNDANT,Recordslist:Valid,Expectedrecords:(153153)
000000000300 800000012D0177 SYS_INDEXES11 11 "ID\_IND" 1 3 0 302
000000000300 800000012D01A5 SYS_INDEXES11 12 "FOR\_IND" 1 0 0 303
000000000300 800000012D01D3 SYS_INDEXES11 13 "REF\_IND" 1 0 0 304
000000000300 800000012D026D SYS_INDEXES12 14 "ID\_IND" 2 3 0 305
[undrop-for-innodb]#
head -5 dumps/default/SYS_INDEXES.sql
SETFOREIGN_KEY_CHECKS=0;
LOADDATAINFILE'/root/tmp/undrop-for-innodb/dumps/default/SYS_INDEXES'REPLACEINTOTABLE`SYS_INDEXES`FIELDSTERMINATEDBY'\t'OPTIONALLYENCLOSEDBY'"'LINESSTARTINGBY'SYS_INDEXES\t'(`TABLE_ID`,`ID`,`NAME`,`N_FIELDS`,`TYPE`,`SPACE`,`PAGE_NO`);
|
Now we can work with the dictionary, but it’s more convenient if the tables are in MySQL.
Loading dictionary tables into MySQL
The main usage of SYS_TABLES and SYS_INDEXES is
to get index_id by table name. It’s possible to run two greps. Having SYS_TABLES and SYS_INDEXES in
MySQL makes job easier.
Before we can process let’s make sure mysql user can read from the root’s home directory. Maybe it’s not wise from security standpoint. If it’s your concern create whole recovery environment somewhere in /tmp.
|
|
[undrop-for-innodb]#
chmod 711 /root/
|
Create empty dictionary tables in some database(e.g. test)
|
|
[undrop-for-innodb]#
mysql test < dictionary/SYS_TABLES.sql
[undrop-for-innodb]#
mysql test < dictionary/SYS_INDEXES.sql
|
And load the dumps:
|
|
[undrop-for-innodb]#
mysql test < dumps/default/SYS_TABLES.sql
[undrop-for-innodb]#
mysql test < dumps/default/SYS_INDEXES.sql
|
Now we have the InnoDB dictionary in MySQL and we can query it as any other MySQL table:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
mysql>SELECT*FROMSYS_TABLESWHERENAME='sakila/actor';
+--------------+-----+--------+------+--------+---------+--------------+-------+
|NAME |ID |N_COLS|TYPE|MIX_ID|MIX_LEN|CLUSTER_NAME|SPACE|
+--------------+-----+--------+------+--------+---------+--------------+-------+
|sakila/actor|753| 4| 1| 0| 80| | 739|
+--------------+-----+--------+------+--------+---------+--------------+-------+
1rowinset(0.00sec)
mysql>SELECT*FROMSYS_INDEXESWHERETABLE_ID=753;
+----------+------+---------------------+----------+------+-------+---------+
|TABLE_ID|ID |NAME |N_FIELDS|TYPE|SPACE|PAGE_NO|
+----------+------+---------------------+----------+------+-------+---------+
| 753|1828|PRIMARY | 1| 3| 739| 3|
| 753|1829|idx_actor_last_name| 1| 0| 739| 4|
+----------+------+---------------------+----------+------+-------+---------+
2rowsinset(0.00sec)
|
Here we can see that sakila.actor has
two indexes: PRIMARY and idx_actor_last_name.
Respective index_id are
1828 and 1829.
Stay tuned to learn what to do with them and how to recover sakila.actor