MySQL异常恢复之无主键情况下innodb数据恢复的方法

 更新时间:2016年04月26日 11:45:18   作者:OurMySQL  
这篇文章主要介绍了MySQL异常恢复之无主键情况下innodb数据恢复的方法,结合实例形式分析了针对innodb引擎无主键的情况下恢复数据的步骤与相关技巧,需要的朋友可以参考下

本文讲述了MySQL异常恢复之无主键情况下innodb数据恢复的方法。分享给大家供大家参考,具体如下:

在mysql的innodb引擎的数据库异常恢复中,一般都要求有主键或者唯一index,其实这个不是必须的,当没有index信息之时,可以在整个表级别的index_id进行恢复

创建模拟表—无主键

mysql> CREATE TABLE `t1` (
  ->  `messageId` varchar(30) character set utf8 NOT NULL,
  ->  `tokenId` varchar(20) character set utf8 NOT NULL,
  ->  `mobile` varchar(14) character set utf8 default NULL,
  ->  `msgFormat` int(1) NOT NULL,
  ->  `msgContent` varchar(1000) character set utf8 default NULL,
  ->  `scheduleDate` timestamp NOT NULL default '0000-00-00 00:00:00',
  ->  `deliverState` int(1) default NULL,
  ->  `deliverdTime` timestamp NOT NULL default '0000-00-00 00:00:00'
  -> ) ENGINE=INnodb DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t1 select * from sms_service.sms_send_record;
Query OK, 11 rows affected (0.00 sec)
Records: 11 Duplicates: 0 Warnings: 0
…………
mysql> insert into t1 select * from t1;
Query OK, 81664 rows affected (2.86 sec)
Records: 81664 Duplicates: 0 Warnings: 0
mysql> insert into t1 select * from t1;
Query OK, 163328 rows affected (2.74 sec)
Records: 163328 Duplicates: 0 Warnings: 0
mysql> select count(*) from t1;
+----------+
| count(*) |
+----------+
|  326656 | 
+----------+
1 row in set (0.15 sec)

解析innodb文件

[root@web103 mysql_recovery]# rm -rf pages-ibdata1/
[root@web103 mysql_recovery]# ./stream_parser -f /var/lib/mysql/ibdata1 
Opening file: /var/lib/mysql/ibdata1
File information:
ID of device containing file:     2049
inode number:           1344553
protection:             100660 (regular file)
number of hard links:          1
user ID of owner:            27
group ID of owner:           27
device ID (if special file):       0
blocksize for filesystem I/O:     4096
number of blocks allocated:     463312
time of last access:      1440819443 Sat Aug 29 11:37:23 2015
time of last modification:   1440819463 Sat Aug 29 11:37:43 2015
time of last status change:   1440819463 Sat Aug 29 11:37:43 2015
total size, in bytes:      236978176 (226.000 MiB)
Size to process:         236978176 (226.000 MiB)
Opening file: /var/lib/mysql/ibdata1
File information:
ID of device containing file:     2049
inode number:           1344553
protection:             100660 (regular file)
number of hard links:          1
user ID of owner:            27
group ID of owner:           27
device ID (if special file):       0
blocksize for filesystem I/O:     4096
number of blocks allocated:     463312
Opening file: /var/lib/mysql/ibdata1
File information:
time of last access:      1440819443 Sat Aug 29 11:37:23 2015
time of last modification:   1440819463 Sat Aug 29 11:37:43 2015
ID of device containing file:     2049
inode number:           1344553
protection:             100660 time of last status change:   1440819463 Sat Aug 29 11:37:43 2015
total size, in bytes:      236978176 (226.000 MiB)
Size to process:         236978176 (226.000 MiB)
Opening file: /var/lib/mysql/ibdata1
File information:
ID of device containing file:     2049
inode number:           1344553
protection:             100660 (regular file)
number of hard links:          1
user ID of owner:            27
group ID of owner:           27
device ID (if special file):       0
blocksize for filesystem I/O:     4096
number of blocks allocated:     463312
time of last access:      1440819443 Sat Aug 29 11:37:23 2015
time of last modification:   1440819463 Sat Aug 29 11:37:43 2015
time of last status change:   1440819463 Sat Aug 29 11:37:43 2015
total size, in bytes:      236978176 (226.000 MiB)
Size to process:         236978176 (226.000 MiB)
(regular file)
number of hard links:          1
user ID of owner:            27
group ID of owner:           27
device ID (if special file):       0
blocksize for filesystem I/O:     4096
number of blocks allocated:     463312
time of last access:      1440819443 Sat Aug 29 11:37:23 2015
time of last modification:   1440819463 Sat Aug 29 11:37:43 2015
time of last status change:   1440819463 Sat Aug 29 11:37:43 2015
total size, in bytes:      236978176 (226.000 MiB)
Size to process:         236978176 (226.000 MiB)
Opening file: /var/lib/mysql/ibdata1
File information:
ID of device containing file:     2049
inode number:           1344553
protection:             100660 (regular file)
number of hard links:          1
user ID of owner:            27
group ID of owner:           27
device ID (if special file):       0
blocksize for filesystem I/O:     4096
number of blocks allocated:     463312
time of last access:      1440819443 Sat Aug 29 11:37:23 2015
time of last modification:   1440819463 Sat Aug 29 11:37:43 2015
time of last status change:   1440819463 Sat Aug 29 11:37:43 2015
total size, in bytes:      236978176 (226.000 MiB)
Size to process:         236978176 (226.000 MiB)
Opening file: /var/lib/mysql/ibdata1
File information:
ID of device containing file:     2049
inode number:           1344553
protection:             100660 (regular file)
number of hard links:          1
user ID of owner:            27
group ID of owner:           27
device ID (if special file):       0
blocksize for filesystem I/O:     4096
number of blocks allocated:     463312
time of last access:      1440819443 Sat Aug 29 11:37:23 2015
time of last modification:   1440819463 Sat Aug 29 11:37:43 2015
time of last status change:   1440819463 Sat Aug 29 11:37:43 2015
Opening file: /var/lib/mysql/ibdata1
File information:
ID of device containing file:     2049
inode number:           1344553
protection:             100660 (regular file)
number of hard links:          1
user ID of owner:            27
group ID of owner:           27
device ID (if special file):       0
blocksize for filesystem I/O:     4096
number of blocks allocated:     463312
total size, in bytes:      236978176 (226.000 MiB)
Size to process:         236978176 (226.000 MiB)
time of last access:      1440819443 Sat Aug 29 11:37:23 2015
time of last modification:   1440819463 Sat Aug 29 11:37:43 2015
time of last status change:   1440819463 Sat Aug 29 11:37:43 2015
total size, in bytes:      236978176 (226.000 MiB)
Size to process:         236978176 (226.000 MiB)
Opening file: /var/lib/mysql/ibdata1
File information:
ID of device containing file:     2049
inode number:           1344553
protection:             100660 (regular file)
number of hard links:          1
user ID of owner:            27
group ID of owner:           27
device ID (if special file):       0
blocksize for filesystem I/O:     4096
number of blocks allocated:     463312
time of last access:      1440819465 Sat Aug 29 11:37:45 2015
time of last modification:   1440819463 Sat Aug 29 11:37:43 2015
time of last status change:   1440819463 Sat Aug 29 11:37:43 2015
total size, in bytes:      236978176 (226.000 MiB)
Size to process:         236978176 (226.000 MiB)
All workers finished in 0 sec

恢复数据字典

[root@web103 mysql_recovery]# ./recover_dictionary.sh 
Generating dictionary tables dumps... OK
Creating test database ... OK
Creating dictionary tables in database test:
SYS_TABLES ... OK
SYS_COLUMNS ... OK
SYS_INDEXES ... OK
SYS_FIELDS ... OK
All OK
Loading dictionary tables data:
SYS_TABLES ... 48 recs OK
SYS_COLUMNS ... 397 recs OK
SYS_INDEXES ... 67 recs OK
SYS_FIELDS ... 89 recs OK
All OK

分析数据字典,找出来index_id

这里需要注意对于没有主键的表恢复,我们对应的类型是GEN_CLUST_INDEX

mysql> select * from SYS_TABLES where name='test/t1';
+----------------------------------------+-----+-------------+------+--------+---------+--------------+-------+
| NAME                  | ID | N_COLS   | TYPE | MIX_ID | MIX_LEN | CLUSTER_NAME | SPACE |
+----------------------------------------+-----+-------------+------+--------+---------+--------------+-------+
| test/t1                | 100 |      8 |  1 |   0 |    0 |       |   0 | 
+----------------------------------------+-----+-------------+------+--------+---------+--------------+-------+
40 rows in set (0.00 sec)
 
mysql> SELECT * FROM SYS_INDEXES where table_id=100;
+----------+-----+------------------------------+----------+------+-------+------------+
| TABLE_ID | ID | NAME             | N_FIELDS | TYPE | SPACE | PAGE_NO  |
+----------+-----+------------------------------+----------+------+-------+------------+
|   100 | 119 | GEN_CLUST_INDEX       |    0 |  1 |   0 |    2951 | 
+----------+-----+------------------------------+----------+------+-------+------------+
67 rows in set (0.00 sec)

恢复数据

root@web103 mysql_recovery]# ./c_parser -5f pages-ibdata1/FIL_PAGE_INDEX/0000000000000119.page -t dictionary/t1.sql >/tmp/2.txt 2>2.sql
[root@web103 mysql_recovery]# more /tmp/2.txt
-- Page id: 10848, Format: COMPACT, Records list: Valid, Expected records: (73 73)
00000002141B  0000009924F2  80000027133548 t1   "82334502212106951"   "SDK-BBX-010-18681"   "13718311436"  8    "尊敬的用户您好:您的手机验证码为916515如非本人操作,请拨打奥
斯卡客服:400-620-7575。"    "2010-01-01 00:00:00"  0    "1970-01-01 07:00:00"
00000002141C  0000009924F2  80000027133558 t1   "82339012756833423"   "SDK-BBX-010-18681"   "13718311436"  8    "尊敬的用户您好:您的手机验证码为396108如非本人操作,请拨打奥
斯卡客服:400-620-7575。"    "2010-01-01 00:00:00"  0    "1970-01-01 07:00:00"
00000002141D  0000009924F2  80000027133568 t1   "8234322198577796"   "SDK-BBX-010-18681"   "13718311436"  8    "尊敬的用户您好:您的手机验证码为935297如非本人操作,请拨打奥
斯卡客服:400-620-7575。"    "2010-01-01 00:00:00"  0    "1970-01-01 07:00:00"
00000002141E  0000009924F2  80000027133578 t1   "10235259536125650"   "SDK-BBX-010-18681"   "13718311436"  8    "尊敬的用户您好:您的手机验证码为474851如非本人操作,请拨打奥
斯卡客服:400-620-7575。"    "2010-01-01 00:00:00"  0    "1970-01-01 07:00:00"
00000002141F  0000009924F2  80000027133588 t1   "10235353811295807"   "SDK-BBX-010-18681"   "13718311436"  8    "尊敬的用户您好:您的手机验证码为444632如非本人操作,请拨打奥
斯卡客服:400-620-7575。"    "2010-01-01 00:00:00"  0    "1970-01-01 07:00:00"
000000021420  0000009924F2  80000027133598 t1   "102354211240398235"  "SDK-BBX-010-18681"   "13718311436"  8    "尊敬的用户您好:您的手机验证码为478503如非本人操作,请拨打奥
斯卡客服:400-620-7575。"    "2010-01-01 00:00:00"  0    "1970-01-01 07:00:00"
000000021421  0000009924F2  800000271335A8 t1   "102354554052884567"  "SDK-BBX-010-18681"   "13718311436"  8    "尊敬的用户您好:您的手机验证码为216825如非本人操作,请拨打奥
斯卡客服:400-620-7575。"    "2010-01-01 00:00:00"  0    "1970-01-01 07:00:00"
000000021422  0000009924F2  800000271335B8 t1   "132213454294519126"  "SDK-BBX-010-18681"   "13718311436"  8    "尊敬的用户您好:您的手机验证码为854812如非本人操作,请拨打奥
斯卡客服:400-620-7575。"    "2010-01-01 00:00:00"  0    "1970-01-01 07:00:00"
000000021423  0000009924F2  800000271335C8 t1   "82329022242584577"   "SDK-BBX-010-18681"   "13718311436"  8    "尊敬的用户您好:您的手机验证码为253127如非本人操作,请拨打奥
斯卡客服:400-620-7575。"    "2010-01-01 00:00:00"  0    "2015-08-26 22:02:17"
…………
[root@web103 mysql_recovery]# cat /tmp/2.txt|grep -v "Page id:"|wc -l
380731

因为没有主键,使得恢复出来记录可能有一些重复,整体而言,可以较为完美的恢复数据

更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》、《MySQL数据库锁相关技巧汇总》及《MySQL常用函数大汇总

希望本文所述对大家MySQL数据库计有所帮助。

相关文章

  • MySQL数据库远程连接很慢的解决方案

    MySQL数据库远程连接很慢的解决方案

    本文给大家分享的是MySQL数据库远程连接很慢的解决方法,简单的说就是开启skip-name-resolve,非常的简单实用,有需要的小伙伴可以参考下
    2016-12-12
  • mysql快速插入大量数据的正确方法

    mysql快速插入大量数据的正确方法

    在处理大量数据时如果一条一条地插入会极大地影响效率,因此批量插入是一个更好的选择,下面这篇文章主要给大家介绍了关于mysql快速插入大量数据的正确方法,需要的朋友可以参考下
    2024-01-01
  • SQL通用语法以及分类图文详解

    SQL通用语法以及分类图文详解

    本书从初学者的角度出发,由浅入深,循序渐进地介绍了SQL通用语法的相关知识,下面这篇文章主要给大家介绍了关于SQL通用语法以及分类的相关资料,需要的朋友可以参考下
    2022-12-12
  • MySQL中临时表的基本创建与使用教程

    MySQL中临时表的基本创建与使用教程

    这篇文章主要介绍了MySQL中临时表的基本创建与使用教程,注意临时表中数据的清空问题,需要的朋友可以参考下
    2015-12-12
  • MySQL逻辑备份into outfile

    MySQL逻辑备份into outfile

    这篇文章主要介绍了MySQL 备份之 into outfile,文章围绕主题展开详细内容介绍,具有一定的参考价值需要的小伙伴可以参考一下
    2022-05-05
  • 安装和使用percona-toolkit来辅助操作MySQL的基本教程

    安装和使用percona-toolkit来辅助操作MySQL的基本教程

    这篇文章主要介绍了安装和使用percona-toolkit来辅助操作MySQL的基本教程,这里举了五个最常见的命令用法,需要的朋友可以参考下
    2015-11-11
  • InnoDB 类型MySql恢复表结构与数据

    InnoDB 类型MySql恢复表结构与数据

    MySQL中.frm文件:保存了每个表的元数据,包括表结构的定义等,该文件与数据库引擎无关。MySQL中.ibd文件:InnoDB引擎开启了独立表空间(my.ini中配置innodb_file_per_table = 1)产生的存放该表的数据和索引的文件。
    2018-01-01
  • 安装Mysql时可能会遇到的一些疑难杂症

    安装Mysql时可能会遇到的一些疑难杂症

    这篇文章主要给大家介绍了关于安装Mysql时可能会遇到的一些疑难杂症,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • linux 下配置安装mysql以及配置【经验】

    linux 下配置安装mysql以及配置【经验】

    这篇文章主要介绍了linux 下配置安装mysql以及配置【经验】,需要的朋友可以参考下
    2016-05-05
  • MySQL对JSON数据进行查询实例代码

    MySQL对JSON数据进行查询实例代码

    这篇文章主要给大家介绍了关于MySQL对JSON数据进行查询的相关资料,MySQL支持使用JSON类型存储数据,并提供了多种查询JSON数据的方法,需要的朋友可以参考下
    2023-07-07

最新评论