mysql卸载后怎么样 mysql卸载后数据还在吗

mysql卸载后怎么找回原来建的数据库?每个 DBA 是不是都有过删库的经历?删库了没有备份怎么办?备份恢复后无法启动服务什么情况?表定义损坏数据无法读取怎么办?
我曾遇到某初创互联网企业 , 因维护人员不规范的备份恢复操作,导致系统表空间文件被初始化,上万张表无法读?。耸∈辈徘谰然乩?。
当你发现数据无法读取时,也许并非数据丢失了,可能是 DBMS 找不到描述数据的信息 。
背景
先来了解下几张关键的 InnoDB 数据字典表 , 它们保存了部分表定义信息,在我们恢复表结构时需要用到 。
SYS_TABLES 描述 InnoDB 表信息CREATE TABLE `SYS_TABLES` (`NAME` varchar(255) NOT NULL DEFAULT '',表名`ID` bigint(20) unsigned NOT NULL DEFAULT '0',表id`N_COLS` int(10) DEFAULT NULL,`TYPE` int(10) unsigned DEFAULT NULL,`MIX_ID` bigint(20) unsigned DEFAULT NULL,`MIX_LEN` int(10) unsigned DEFAULT NULL,`CLUSTER_NAME` varchar(255) DEFAULT NULL,`SPACE` int(10) unsigned DEFAULT NULL,表空间idPRIMARY KEY (`NAME`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;SYS_INDEXES 描述 InnoDB 索引信息CREATE TABLE `SYS_INDEXES` (`TABLE_ID` bigint(20) unsigned NOT NULL DEFAULT '0', 与sys_tables的id对应`ID` bigint(20) unsigned NOT NULL DEFAULT '0',索引id`NAME` varchar(120) DEFAULT NULL,索引名称`N_FIELDS` int(10) unsigned DEFAULT NULL, 索引包含字段的个数`TYPE` int(10) unsigned DEFAULT NULL,`SPACE` int(10) unsigned DEFAULT NULL,存储索引的表空间id`PAGE_NO` int(10) unsigned DEFAULT NULL,索引的root page idPRIMARY KEY (`TABLE_ID`,`ID`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;SYS_COLUMNS 描述 InnoDB 表的字段信息CREATE TABLE `SYS_COLUMNS` (`TABLE_ID` bigint(20) unsigned NOT NULL, 与sys_tables的id对应`POS` int(10) unsigned NOT NULL,字段相对位置`NAME` varchar(255) DEFAULT NULL,字段名称`MTYPE` int(10) unsigned DEFAULT NULL,字段编码`PRTYPE` int(10) unsigned DEFAULT NULL, 字段校验类型`LEN` int(10) unsigned DEFAULT NULL,字段字节长度`PREC` int(10) unsigned DEFAULT NULL, 字段精度PRIMARY KEY (`TABLE_ID`,`POS`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;SYS_FIELDS 描述全部索引的字段列CREATE TABLE `SYS_FIELDS` (`INDEX_ID` bigint(20) unsigned NOT NULL,`POS` int(10) unsigned NOT NULL,`COL_NAME` varchar(255) DEFAULT NULL,PRIMARY KEY (`INDEX_ID`,`POS`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;./storage/innobase/include/dict0boot.h 文件定义了每个字典表的 index id,对应 id 的 page 中存储着字典表的数据 。
这里我们需要借助 undrop-for-innodb 工具恢复数据,它能读取表空间信息得到 page , 将数据从 page 中提取出来 。
# wgetyum install -y gcc flex bison# make# make sys_parser
# ./sys_parser 读取表结构信息
sys_parser [-h] [-u] [-p] [-d] databases/table
stream_parser 读取 InnoDB page 从 ibdata1 或 ibd 或分区表
# ./stream_parserYou must specify file with -f optionUsage: ./stream_parser -f innodb_datafile [-T N:M] [-s size] [-t size] [-V|-g]Where:-h- Print this help-V or -g- Print debug information-s size- Amount of memory used for disk cache (allowed examples 1G 10M). Default 100M-T- retrieves only pages with index id = NM (N - high word, M - low word of id)-t size- Size of InnoDB tablespace to scan. Use it only if the parser can't determine it by himself.
c_parser 从 innodb page 中读取记录保存到文件
# ./c_parserError: Usage: ./c_parser -4|-5|-6 [-dDV] -f InnoDB page or dir -t table.sql [-T N:M] [-b external pages directory]Where-f InnoDB page(s) -- InnoDB page or directory with pages(all pages should have same index_id)-t table.sql -- CREATE statement of a table-o file -- Save dump in this file. Otherwise print to stdout-l file -- Save SQL statements in this file. Otherwise print to stderr-h-- Print this help-d-- Process only those pages which potentially could have deleted records (default = NO)-D-- Recover deleted rows only (default = NO)-U-- Recover UNdeleted rows only (default = YES)-V-- Verbose mode (lots of debug information)-4-- innodb_datafile is in REDUNDANT format-5-- innodb_datafile is in COMPACT format-6-- innodb_datafile is in MySQL 5.6 format-T-- retrieves only pages with index id = NM (N - high word, M - low word of id)-b dir -- Directory where external pages can be found. Usually it is pages-XXX/FIL_PAGE_TYPE_BLOB/-i file -- Read external pages at their offsets from file.-p prefix -- Use prefix for a directory name in LOAD DATA INFILE command

推荐阅读