MySQL单表ibd文件恢复
发布日期:2021-08-16 13:27:48 浏览次数:76 分类:技术文章

本文共 6414 字,大约阅读时间需要 21 分钟。

本博客已经迁移至:

本篇博文已经迁移,如果显示不正常请使用此链接:

 

前言:

随着innodb的普及,innobackup也成为了主流备份方式。物理备份对于新建slave,全库恢复的需求都能从容应对。

但当面临单表数据误删,或者单表误drop的情况,如果使用物理全备进行恢复呢? 

下文将进行详细分析。 

恢复过程中需要用到的工具,percona data recover tool : 

PS:以下所有方案仅支持 innodb-file-per-table = 1 的情况 

注意: 以下操作非文档推荐,切勿在没有测试的情况下直接在production环境使用!!!

情况一:误删部分数据,需要用最近一次备份覆盖

来自同一台机器的ibd恢复覆盖,且备份后table没有被recreate过。

这种情况是最简单的,备份时的ibd文件(后称老ibd)中的space id和index id 与 新ibd的space id 和index id一致。

且和ibdata文件中的space id和index id一致。因此,物理文件可以直接覆盖做恢复。

以下是详细步骤

-------------------------------------------准备阶段-------------------------------------------------

Step -2 : 物理备份

 innobackupex --defaults-file=/usr/local/mysql3321/my.cnf --socket=/xfs/mysql3321/mysql.sock --user=root --password=password /xfs/backup/

Step -1 : 停止数据库对外服务

service mysqld restart (起在另外一个端口上)

或者 停止所有业务连接并且set global innodb_max_dirty_pages_pct  =0

------------------------------------------操作阶段----------------------------------------------------

Step 0 : apply log

innobackupex --apply-log --defaults-file=/usr/local/mysql3321/my.cnf  /xfs/backup/2012-10-17_11-29-20/

Step 1 : 备份现在的ibd文件(可选)

cp -a testibd.ibd testibd.bak

Step 2 : 舍弃现在ibd文件

mysql> alter table testibd discard tablespace

Step 3 : 复制备份ibd文件

shell> cp /xfs/backup/2012-10-17_11-29-20/test/testibd.ibd /xfs/mysql3321/test/ 

shell> chown mysql:mysql /xfs/mysql3321/test/testibd.ibd

Step 4 : 导入ibd文件

mysql> alter table testibd import tablespace

 

情况二:误删 table,表结构已经被drop了

这种情况稍复杂,不过恢复过程还是比较容易操作的。由于table被drop后的space id会留空因此备份文件的space id不会被占用。

我们只需要重建表结构,然后把ibdata中该表的space id还原,物理文件可以直接覆盖做恢复了。

Step 1 : 重建表

mysql> create table testibd (UserID int);

Step 2 : 关闭mysql服务(必须)

shell> service mysqld3321 stop

Step 3: 准备ibd文件  apply log

shell> innobackupex --apply-log --defaults-file=/usr/local/mysql3321/my.cnf  /xfs/backup/2012-10-17_11-29-20/

Step 4 : 备份现在的ibd文件(可选)

cp -a testibd.ibd testibd.bak

Step 5 : 复制备份ibd文件

shell> cp -a /xfs/backup/2012-10-17_11-29-20/test/testibd.ibd /xfs/mysql3321/test/ 

shell> chown mysql:mysql /xfs/mysql3321/test/testibd.ibd

Step 6 : 使用percona recovery tool 修改ibdata 

shell> /root/install/percona-data-recovery-tool-for-innodb-0.5/ibdconnect -o /xfs/mysql3321/ibdata1 -f /xfs/mysql3321/test/testibd.ibd -d test -t testibd

输出结果
Initializing table definitions...Processing table: SYS_TABLES - total fields: 10 - nullable fields: 6 - minimum header size: 5 - minimum rec size: 21 - maximum rec size: 555Processing table: SYS_INDEXES - total fields: 9 - nullable fields: 5 - minimum header size: 5 - minimum rec size: 29 - maximum rec size: 165Setting SPACE=1 in SYS_TABLE for `test`.`testibd`Check if space id 1 is already usedPage_id: 8, next page_id: 4294967295Record position: 65Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 8 50 3 2 0 0 0 0 0 Db/table: infimumSpace id: 1768842857 (0x696E6669)Next record at offset: 8DRecord position: 8DChecking field lengths for a row (SYS_TABLES): OFFSETS: 16 11 17 24 32 36 40 48 52 52 Db/table: SYS_FOREIGNSpace id: 0 (0x0)Next record at offset: D5Record position: D5Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 16 22 29 37 41 45 53 57 57 Db/table: SYS_FOREIGN_COLSSpace id: 0 (0x0)Next record at offset: 122Record position: 122Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 12 18 25 33 37 41 49 53 53 Db/table: test/testibdSpace id: 2 (0x2)Next record at offset: 74Space id 1 is not used in any of the records in SYS_TABLESPage_id: 8, next page_id: 4294967295Record position: 65Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 8 50 3 2 0 0 0 0 0 Db/table: infimumSpace id: 1768842857 (0x696E6669)Next record at offset: 8DRecord position: 8DChecking field lengths for a row (SYS_TABLES): OFFSETS: 16 11 17 24 32 36 40 48 52 52 Db/table: SYS_FOREIGNSpace id: 0 (0x0)Next record at offset: D5Record position: D5Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 16 22 29 37 41 45 53 57 57 Db/table: SYS_FOREIGN_COLSSpace id: 0 (0x0)Next record at offset: 122Record position: 122Checking field lengths for a row (SYS_TABLES): OFFSETS: 16 12 18 25 33 37 41 49 53 53 Db/table: test/testibdSpace id: 2 (0x2)Updating test/testibd (table_id 17) with id 0x01000000SYS_TABLES is updated successfullyInitializing table definitions...Processing table: SYS_TABLES - total fields: 10 - nullable fields: 6 - minimum header size: 5 - minimum rec size: 21 - maximum rec size: 555Processing table: SYS_INDEXES - total fields: 9 - nullable fields: 5 - minimum header size: 5 - minimum rec size: 29 - maximum rec size: 165Setting SPACE=1 in SYS_INDEXES for TABLE_ID = 17Page_id: 11, next page_id: 4294967295Record position: 65Checking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 50 7 2 0 0 0 0 TABLE_ID: 3798561113125514496SPACE: 1768842857Next record at offset: 8CRecord position: 8CChecking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 35 39 43 47 TABLE_ID: 11SPACE: 0Next record at offset: CERecord position: CEChecking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 36 40 44 48 TABLE_ID: 11SPACE: 0Next record at offset: 111Record position: 111Checking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 36 40 44 48 TABLE_ID: 11SPACE: 0Next record at offset: 154Record position: 154Checking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 35 39 43 47 TABLE_ID: 12SPACE: 0Next record at offset: 22CRecord position: 22CChecking field lengths for a row (SYS_INDEXES): OFFSETS: 15 8 16 22 29 44 48 52 56 TABLE_ID: 17SPACE: 2Updating SPACE(0x00000001 , 0x01000000) for TABLE_ID: 17sizeof(s)=4Next record at offset: 74SYS_INDEXES is updated successfully

 

Step 7 : 使用percona recovery tool 重新checksum ibdata

重复执行以下命令,直到程序没有输出为止。

shell> /root/install/percona-data-recovery-tool-for-innodb-0.5/innochecksum -f /xfs/mysql3321/ibdata1

输出结果
page 8 invalid (fails old style checksum)page 8: old style: calculated = 0xF4AD74CB; recorded = 0xEECB309Dfixing old checksum of page 8page 8 invalid (fails new style checksum)page 8: new style: calculated = 0x6F0C29B4; recorded = 0x3D02308Cfixing new checksum of page 8page 11 invalid (fails old style checksum)page 11: old style: calculated = 0x3908087C; recorded = 0xF9E8D30Cfixing old checksum of page 11page 11 invalid (fails new style checksum)page 11: new style: calculated = 0xB26CFD77; recorded = 0xDB25D39Dfixing new checksum of page 11

 

Step 8 : 启动mysql服务

shell> service mysqld3321 start

 

参考文档:

 

 

 

转载于:https://www.cnblogs.com/cenalulu/archive/2012/10/17/2727438.html

转载地址:https://blog.csdn.net/weixin_30807779/article/details/98200910 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:YCOJ【汉诺塔】
下一篇:yii目录解析

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年03月05日 14时21分51秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

mysql 更新数据 字符串_批量替换 MySQL 指定字段中的字符串 2019-04-21
web开发 mysql安装_mysqlinstallerwebcommunity5.7.21.0.msi安装图文教程 2019-04-21
mysql concat 整数型_MySQL 数字类型转换函数(concat/cast) 2019-04-21
mysql单元格函数是_MySQL常用内置函数 2019-04-21
mysql 怎么字段分裂_你可以分裂/爆炸MySQL查询中的字段吗? 2019-04-21
mysql server卸载出错_Mysql卸载问题Start Server卡住报错解决方法 2019-04-21
全国省市区 mysql_2017全国省市区数据库【含三款数据库】 2019-04-21
druid加载MySQL驱动原理_你好,想知道mybatis+druid+jdbc 原理介绍? 2019-04-21
mysql 怎样链接jdbc_jdbc怎么链接mysql数据库 2019-04-21
mysql学生课程表试题_Mysql练习之 学生表、课程表 、教师表、成绩表 50道练习题... 2019-04-21
java exec封装_Java 执行系统命令工具类(commons-exec) 2019-04-21
php sha512解密,PHP加密函数 sha256 sha512 sha256_file() sha512_file() 2019-04-21
mysql里可以用cube吗_sql server的cube操作符使用详解_mysql 2019-04-21
php mysql 图书_使用PHP+MySQL来对图书管理系统进行构建 2019-04-21
单片机c语言 int1,51单片机into、int1中断计数c语言源程序.doc 2019-04-21
c语言课程设计工资管理建库,C语言课程设计工资管理系统参考.doc 2019-04-21
c语言case中途跳出,break语句在switch结构语句中的作用是终止某个case,并跳出switch结构语句。... 2019-04-21
c51写c语言外部ram头文件,C51中访问外部RAM的方法 2019-04-21
51c语言产生随机证书,基于51单片机的随机数产生器设计-LCD1602-KEY-(电路图+程序源码)... 2019-04-21
C语言编写程序计算高考倒计时天数,基于51单片机LCD12864大字符校时万年历带高考倒计时程序... 2019-04-21