.frm和.ibd恢复数据
昨日晚上开发告诉我不小心truncate两个表的数据,要求还原。结果在阿里云上找到了备份内容,结果是物理备份文件.frm、.ibd。心中一万个草泥马啊。。没办法,开始还原吧。
1、查看测试机Mysql配置文件位置
[root@localhost mysql]# which mysqld /usr/sbin/mysqld [root@localhost mysql]# /usr/sbin/mysqld --verbose --help|grep -A 1 'Default options' Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf
可以看到Mysql首先调用的是/etc/my.cnf。查看该文件
[root@localhost mysql]# cat /etc/my.cnf # # The Percona Server 5.7 configuration file. # # # * IMPORTANT: Additional settings that can override those from this file! # The files must end with '.cnf', otherwise they'll be ignored. # Please make any edits and changes to the appropriate sectional files # included below. # !includedir /etc/my.cnf.d/ !includedir /etc/percona-server.conf.d/
发现加载的是/etc/my.cnf.d/和/etc/percona-server.conf.d/目录,/etc/my.cnf.d/为空,/etc/percona-server.conf.d/有两个文件。
终于找到了配置文件/etc/percona-server.conf.d/mysqld.cnf。
2、进入测试机mysql,创建一个表(表名需要与恢复的表名一致,表结构一致)
CREATE TABLE `goms_hangar` ( `hangar_id` int(11) unsigned NOT NULL AUTO_INCREMENT, `airport_iata` char(3) NOT NULL DEFAULT 'KMG' COMMENT '所属机场', `hangar_category` varchar(16) NOT NULL DEFAULT '' COMMENT '所属机库分类', `hangar_num` varchar(16) NOT NULL DEFAULT '' COMMENT '机库具体编号', `arr_fid` char(32) NOT NULL DEFAULT '' COMMENT '进港航班', `aircraft_num` varchar(16) NOT NULL DEFAULT '' COMMENT '飞机编号', `from_parking_num` varchar(8) NOT NULL DEFAULT '' COMMENT '原机位', `in_time` int(11) unsigned NOT NULL COMMENT '进库时间', `go_parking_num` varchar(8) NOT NULL DEFAULT '' COMMENT '出库机位', `out_time` int(11) unsigned NOT NULL COMMENT '出库时间', `uid` int(11) unsigned NOT NULL COMMENT '操作人', `is_exists` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '是否还存在', `update_time` int(11) unsigned NOT NULL, `create_time` int(11) NOT NULL COMMENT '数据创建时间', PRIMARY KEY (`hangar_id`), KEY `IDEX_H_CATEGORY` (`hangar_category`), KEY `IDX_H_NUM` (`hangar_num`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3、
alter table goms_hangar discard tablespace;
4、将备份的.ibd拷贝到datadir目录下,记得更改权限
[root@localhost hello]# cp ~/source/goms_hangar.ibd . [root@localhost hello]# chown -R mysql:mysql .
5、在Mysql数据库中输入
mysql> alter table goms_hangar import tablespace;
完成恢复。
6、查看效果
mysql> select count(*) from goms_hangar; +----------+ | count(*) | +----------+ | 341 | +----------+ 1 row in set (0.09 sec)
附:alter table table_name discard tablespace;与alter table goms_hangar import tablespace;
ALTER TABLE tbl_name DISCARD TABLESPACE;会将当前.ibd文件删除掉
ALTER TABLE tbl_name IMPORT TABLESPACE;会将文件内容导入到表空间中
注:如果在ALTER TABLE tbl_name IMPORT TABLESPACE;报如下错误
则需要在创建表的时候指定表的行格式。即加上ROW_FORMAT=COMPACT