Preface
 
    ibdata1 file is a shared system tablespace of innodb engine.Although we always set variable of "innodb_file_per_table" to "on" which means put the data of table into their individual ibd files.The ibdata file will still store undo log of our innodb tables.As the ibdata1 file is inevitable to make the MySQL database running properly.Today,I'm gonna demonstrate a way to rescue the table in an instance whose ibdata file is destroied by a certain way such as write several unmeaningful characters into it.
 
Porcedure
 
Backup the instance using Xtrabackup first.
  1. [root@zlm1 :: ~]
  2. #innobackupex --defaults-file=/data/mysql/mysql3306/my3306.cnf --user=root --password=Passw0rd /data/backup
  3. :: innobackupex: Starting the backup operation
  4. ... //Omitted.
  5.  
  6. [root@zlm1 :: /data/backup]
  7. #ls -l
  8. total
  9. drwxr-x--- root root Aug : --05_13--
  10.  
  11. [root@zlm1 :: /data/backup]

Check the target table which we want to rescue in plan.

  1. zlm@192.168.56.100: [sysbench]>show tables;
  2. +--------------------+
  3. | Tables_in_sysbench |
  4. +--------------------+
  5. | sbtest1 |
  6. | sbtest2 |
  7. | sbtest3 |
  8. | sbtest4 |
  9. +--------------------+
  10. rows in set (0.00 sec)
  11.  
  12. zlm@192.168.56.100: [sysbench]>select count(*) from sbtest4;
  13. +----------+
  14. | count(*) |
  15. +----------+
  16. | |
  17. +----------+
  18. row in set (0.05 sec)
  19.  
  20. zlm@192.168.56.100: [sysbench]>delete from sbtest4 limit ;
  21. Query OK, rows affected (0.17 sec)
  22.  
  23. zlm@192.168.56.100: [sysbench]>select count(*) from sbtest4;
  24. +----------+
  25. | count(*) |
  26. +----------+
  27. | |
  28. +----------+
  29. row in set (0.00 sec)

Destroy the ibdata1 file with "dd" command.

  1. [root@zlm1 :: ~]
  2. #cd /data/mysql/mysql3306/data
  3.  
  4. [root@zlm1 :: /data/mysql/mysql3306/data]
  5. #ls -l
  6. total
  7. -rw-r----- mysql mysql Mar : auto.cnf
  8. -rw-r----- mysql mysql Aug : error.log
  9. -rw-r----- mysql mysql Jul : ib_buffer_pool
  10. -rw-r----- mysql mysql Aug : ibdata1
  11. -rw-r----- mysql mysql Aug : ib_logfile0
  12. -rw-r----- mysql mysql Aug : ib_logfile1
  13. -rw-r----- mysql mysql Jul : ib_logfile2
  14. -rw-r----- mysql mysql Aug : ibtmp1
  15. -rw-r----- mysql mysql Aug : innodb_status.
  16. -rw-r----- mysql mysql Jul : innodb_status.
  17. -rw-r----- mysql mysql Jun : innodb_status.
  18. drwxr-x--- mysql mysql Jul : mrbs
  19. drwxr-x--- mysql mysql Mar : mysql
  20. -rw-r----- mysql mysql Aug : mysql.pid
  21. drwxr-x--- mysql mysql Mar : performance_schema
  22. -rw-r----- mysql mysql Jun : relay-bin.
  23. -rw-r----- mysql mysql Jun : relay-bin.index
  24. -rw-r----- mysql mysql Aug : slow.log
  25. drwxr-x--- mysql mysql Mar : sys
  26. drwxr-x--- mysql mysql Jul : sysbench
  27. -rw-r----- mysql mysql Jul : zlm1.log
  28.  
  29. [root@zlm1 :: /data/mysql/mysql3306/data]
  30. #dd if=/dev/zero of=./ibdata1 bs= count=
  31. + records in
  32. + records out
  33. bytes ( MB) copied, 1.61997 s, MB/s
  34.  
  35. [root@zlm1 :: /data/mysql/mysql3306/data]
  36. #ls -l|grep ibdata1
  37. -rw-r----- mysql mysql Aug : ibdata1 //The ibdata1 turned out to be 256M and filled with zero.

Restart the MySQL instance.

  1. zlm@192.168.56.100: [sysbench]>exit
  2. Bye
  3.  
  4. [root@zlm1 :: ~]
  5. #mysqladmin shutdown
  6.  
  7. [root@zlm1 :: ~]
  8. #ps aux|grep mysqld
  9. root 0.0 0.0 pts/ R+ : : grep --color=auto mysqld
  10.  
  11. [root@zlm1 :: ~]
  12. #./mysqld.sh
  13.  
  14. [root@zlm1 :: ~]
  15. #mysql
  16. ERROR (HY000): Can't connect to MySQL server on '192.168.56.100' (111)
  17.  
  18. [root@zlm1 :: ~]
  19. #mysql
  20. ERROR (HY000): Can't connect to MySQL server on '192.168.56.100' (111)
  21.  
  22. [root@zlm1 :: ~]
  23. #cd /data/mysql/mysql3306/data
  24.  
  25. [root@zlm1 :: /data/mysql/mysql3306/data]
  26. #tail error.log
  27. ... /Omitted.
  28. --05T12::.242723Z [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
  29. --05T12::.242806Z [Note] mysqld (mysqld 5.7.-log) starting as process ...
  30. --05T12::.249168Z [Note] InnoDB: PUNCH HOLE support available
  31. --05T12::.249207Z [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
  32. --05T12::.249213Z [Note] InnoDB: Uses event mutexes
  33. --05T12::.249218Z [Note] InnoDB: GCC builtin __sync_synchronize() is used for memory barrier
  34. --05T12::.249222Z [Note] InnoDB: Compressed tables use zlib 1.2.
  35. --05T12::.249227Z [Note] InnoDB: Using Linux native AIO
  36. --05T12::.249426Z [Note] InnoDB: Number of pools:
  37. --05T12::.249507Z [Note] InnoDB: Using CPU crc32 instructions
  38. --05T12::.251488Z [Note] InnoDB: Initializing buffer pool, total size = 100M, instances = , chunk size = 100M
  39. --05T12::.256630Z [Note] InnoDB: Completed initialization of buffer pool
  40. --05T12::.257913Z [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
  41. --05T12::.280321Z [Note] InnoDB: Highest supported file format is Barracuda.
  42. :: UTC - mysqld got signal ;
  43. This could be because you hit a bug. It is also possible that this binary
  44. or one of the libraries it was linked against is corrupt, improperly built,
  45. or misconfigured. This error can also be caused by malfunctioning hardware.
  46. Attempting to collect some information that could help diagnose the problem.
  47. As this is a crash and something is definitely wrong, the information
  48. collection process might fail.
  49.  
  50. key_buffer_size=
  51. read_buffer_size=
  52. max_used_connections=
  53. max_threads=
  54. thread_count=
  55. connection_count=
  56. It is possible that mysqld could use up to
  57. key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = K bytes of memory
  58. Hope that's ok; if not, decrease some variables in the equation.
  59.  
  60. Thread pointer: 0x0
  61. Attempting backtrace. You can use the following information to find out
  62. where mysqld died. If you see no messages after this, something went
  63. terribly wrong...
  64. stack_bottom = thread_stack 0x30000
  65. mysqld(my_print_stacktrace+0x35)[0xf4a495]
  66. mysqld(handle_fatal_signal+0x4a4)[0x7ce2f4]
  67. /lib64/libpthread.so.(+0xf130)[0x7f6bbee76130]
  68. mysqld(_Z26page_cur_search_with_matchPK11buf_block_tPK12dict_index_tPK8dtuple_t15page_cur_mode_tPmS9_P10page_cur_tP8rtr_info+0x148)[0x1074478]
  69. mysqld(_Z27btr_cur_search_to_nth_levelP12dict_index_tmPK8dtuple_t15page_cur_mode_tmP9btr_cur_tmPKcmP5mtr_t+0x1598)[0x11806d8]
  70. mysqld(_Z30btr_pcur_open_on_user_rec_funcP12dict_index_tPK8dtuple_t15page_cur_mode_tmP10btr_pcur_tPKcmP5mtr_t+0x212)[0x1184b62]
  71. mysqld[0x11df28d]
  72. mysqld(_Z19dict_load_sys_tableP12dict_table_t+0x69)[0x11e0609]
  73. mysqld(_Z9dict_bootv+0xdfb)[0x11bf48b]
  74. mysqld(_Z34innobase_start_or_create_for_mysqlv+0x3212)[0x11150a2]
  75. mysqld[0x100023a]
  76. mysqld(_Z24ha_initialize_handlertonP13st_plugin_int+0x51)[0x819721]
  77. mysqld[0xd39226]
  78. mysqld(_Z40plugin_register_builtin_and_init_core_sePiPPc+0x3e4)[0xd397a4]
  79. mysqld[0x7c48f7]
  80. mysqld(_Z11mysqld_mainiPPc+0x92f)[0x7c7e9f]
  81. /lib64/libc.so.(__libc_start_main+0xf5)[0x7f6bbd857af5]
  82. mysqld[0x7be479]
  83. The manual page at http://dev.mysql.com/doc/mysql/en/crashing.html contains
  84. information that should help you find out what is causing the crash.
  85.  
  86. [root@zlm1 :: /data/mysql/mysql3306/data]
  87.  
  88. //Okay,becasue of the lack of normal ibdata1 file in datadir,the instance cannot be started again.
  89. //Then,how can we resuce the data in target table 'sbtest4'?
  90. //First of all,let's get the ibd and frm file of it from the crashed server.

Copy ibd and frm file of table "sbtest4" to another newly initialized instance on zlm2.

  1. [root@zlm1 :: /data/mysql/mysql3306/data/sysbench]
  2. #scp sbtest4.* zlm2:/data/backup
  3. sbtest4.frm % .4KB/s :
  4. sbtest4.ibd % 10MB .0MB/s :

Install mysql-utilities package on zlm2.

  1. [root@zlm2 :: ~]
  2. #mysqlfrm --help
  3. -bash: mysqlfrm: command not found
  4.  
  5. [root@zlm2 :: ~]
  6. #yum install mysql-utilities
  7. Loaded plugins: fastestmirror
  8. base | 3.6 kB ::
  9. epel/x86_64/metalink | 7.0 kB ::
  10. epel | 3.2 kB ::
  11. extras | 3.4 kB ::
  12. updates | 3.4 kB ::
  13. (/): epel/x86_64/group_gz | kB ::
  14. (/): epel/x86_64/updateinfo | kB ::
  15. (/): extras//x86_64/primary_db | kB ::
  16. (/): epel/x86_64/primary | 3.6 MB ::
  17. (/): updates//x86_64/primary_db | 4.3 MB ::
  18. Loading mirror speeds from cached hostfile
  19. * base: mirrors.shu.edu.cn
  20. * epel: mirrors.tongji.edu.cn
  21. * extras: mirrors..com
  22. * updates: mirrors..com
  23. epel /
  24. Resolving Dependencies
  25. --> Running transaction check
  26. ---> Package mysql-utilities.noarch :1.3.-.el7 will be installed
  27. --> Processing Dependency: mysql-connector-python for package: mysql-utilities-1.3.-.el7.noarch
  28. --> Running transaction check
  29. ---> Package mysql-connector-python.noarch :1.1.-.el7 will be installed
  30. --> Finished Dependency Resolution
  31.  
  32. ... //Omitted.
  33.  
  34. Installed:
  35. mysql-utilities.noarch :1.3.-.el7
  36.  
  37. Dependency Installed:
  38. mysql-connector-python.noarch :1.1.-.el7
  39.  
  40. Complete!

Restore the table structure by mysqlfrm.

  1. [root@zlm2 :: ~]
  2. #cd /data/backup/
  3.  
  4. [root@zlm2 :: /data/backup]
  5. #ls -l|grep sbtest4
  6. -rw-r----- root root Aug : sbtest4.frm
  7. -rw-r----- root root Aug : sbtest4.ibd
  8.  
  9. [root@zlm2 :: /data/backup]
  10. #mysqlfrm --basedir=/usr/local/mysql --port= --diagnostic ./sbtest4.frm
  11. # WARNING The --port option is not used in the --diagnostic mode.
  12. # WARNING: Cannot generate character set or collation names without the --server option.
  13. # CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
  14. # Reading .frm file for ./sbtest4.frm:
  15. # The .frm file is a TABLE.
  16. # CREATE TABLE Statement:
  17.  
  18. CREATE TABLE `sbtest4` (
  19. `id` int() NOT NULL AUTO_INCREMENT,
  20. `k` int() NOT NULL,
  21. `c` char() NOT NULL,
  22. `pad` char() NOT NULL,
  23. PRIMARY KEY `PRIMARY` (`id`),
  24. KEY `k_4` (`k`)
  25. ) ENGINE=InnoDB;
  26.  
  27. #...done.

Create a same table structure in the new instance using the restored "create table" statement.

  1. [root@zlm2 :: /data/backup]
  2. #mysql
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is
  5. Server version: 5.7.-log MySQL Community Server (GPL)
  6.  
  7. Copyright (c) , , Oracle and/or its affiliates. All rights reserved.
  8.  
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12.  
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14.  
  15. zlm@192.168.56.101: [(none)]>show databases;
  16. +--------------------+
  17. | Database |
  18. +--------------------+
  19. | information_schema |
  20. | mrbs |
  21. | mysql |
  22. | performance_schema |
  23. | sys |
  24. +--------------------+
  25. rows in set (0.00 sec)
  26.  
  27. zlm@192.168.56.101: [(none)]>create database sysbench;
  28. Query OK, row affected (0.00 sec)
  29.  
  30. zlm@192.168.56.101: [(none)]>use sysbench
  31. Database changed
  32. zlm@192.168.56.101: [sysbench]>show tables;
  33. Empty set (0.00 sec)
  34.  
  35. zlm@192.168.56.101: [sysbench]>CREATE TABLE `sbtest4` (
  36. -> `id` int() NOT NULL AUTO_INCREMENT,
  37. -> `k` int() NOT NULL,
  38. -> `c` char() NOT NULL,
  39. -> `pad` char() NOT NULL,
  40. -> PRIMARY KEY `PRIMARY` (`id`),
  41. -> KEY `k_4` (`k`)
  42. -> ) ENGINE=InnoDB;
  43. ERROR (): Column length too big for column 'c' (max = ); use BLOB or TEXT instead
  44. zlm@192.168.56.101: [sysbench]>CREATE TABLE `sbtest4` (
  45. -> `id` int() NOT NULL AUTO_INCREMENT,
  46. -> `k` int() NOT NULL,
  47. -> `c` char() NOT NULL,
  48. -> `pad` char() NOT NULL,
  49. -> PRIMARY KEY `PRIMARY` (`id`),
  50. -> KEY `k_4` (`k`)
  51. -> ) ENGINE=InnoDB;
  52. ERROR (42S02): Table 'sysbench.sbtest4' doesn't exist
  53. zlm@192.168.56.101: [sysbench]>CREATE TABLE `sbtest4_bak` (
  54. -> `id` int() NOT NULL AUTO_INCREMENT,
  55. -> `k` int() NOT NULL,
  56. -> `c` char() NOT NULL,
  57. -> `pad` char() NOT NULL,
  58. -> PRIMARY KEY `PRIMARY` (`id`),
  59. -> KEY `k_4` (`k`)
  60. -> ) ENGINE=InnoDB;
  61. Query OK, rows affected (0.01 sec)
  62.  
  63. zlm@192.168.56.101: [sysbench]>rename table sbtest4_bak to sbtest4;
  64. Query OK, rows affected (0.00 sec)
  65.  
  66. zlm@192.168.56.101: [sysbench]>show tables;
  67. +--------------------+
  68. | Tables_in_sysbench |
  69. +--------------------+
  70. | sbtest4 |
  71. +--------------------+
  72. row in set (0.00 sec)
  73.  
  74. zlm@192.168.56.101: [sysbench]>show create table sbtest4\G
  75. *************************** . row ***************************
  76. Table: sbtest4
  77. Create Table: CREATE TABLE `sbtest4` (
  78. `id` int() NOT NULL AUTO_INCREMENT,
  79. `k` int() NOT NULL,
  80. `c` char() NOT NULL,
  81. `pad` char() NOT NULL,
  82. PRIMARY KEY (`id`),
  83. KEY `k_4` (`k`)
  84. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  85. row in set (0.00 sec)
  86.  
  87. zlm@192.168.56.101: [sysbench]>select count(*) from sbtest4;
  88. +----------+
  89. | count(*) |
  90. +----------+
  91. | |
  92. +----------+
  93. row in set (0.00 sec)

Discard the tablesapce of new table "sbtest4".

  1. zlm@192.168.56.101: [sysbench]>alter table sbtest4 discard tablespace;
  2. Query OK, rows affected (0.00 sec)
  3.  
  4. [root@zlm2 :: /data/mysql/mysql3306/data/sysbench]
  5. #ls -l
  6. total
  7. -rw-r----- mysql mysql Aug : db.opt
  8. -rw-r----- mysql mysql Aug : sbtest4.frm
  9.  
  10. //After the discard operation,the new ibd file has gone.

Copy the ibd file of table "sbtest4" to the sysbench directory in datadir(notice the owner of file).

  1. [root@zlm2 :: /data/backup]
  2. #cp sbtest4.ibd /data/mysql/mysql3306/data/sysbench/
  3.  
  4. [root@zlm2 :: /data/backup]
  5. #ll /data/mysql/mysql3306/data/sysbench/ |grep sbtest4
  6. -rw-r----- mysql mysql Aug : sbtest4.frm
  7. -rw-r----- root root Aug : sbtest4.ibd
  8.  
  9. [root@zlm2 :: /data/backup]
  10. #chown mysql.mysql /data/mysql/mysql3306/data/sysbench/sbtest4.ibd
  11.  
  12. [root@zlm2 :: /data/backup]
  13. #ll /data/mysql/mysql3306/data/sysbench/ |grep sbtest4
  14. -rw-r----- mysql mysql Aug : sbtest4.frm
  15. -rw-r----- mysql mysql Aug : sbtest4.ibd

Import the original tablespace of table "sbtest4".

  1. zlm@192.168.56.101: [sysbench]>alter table sbtest4 import tablespace;
  2. Query OK, rows affected, warning (1.77 sec)
  3.  
  4. zlm@192.168.56.101: [sysbench]>select count(*) from sbtest4;
  5. +----------+
  6. | count(*) |
  7. +----------+
  8. | |
  9. +----------+
  10. row in set (0.01 sec)
  11.  
  12. //Now the table has been rescued.
  13. //Because of the destroying of ibdata1 in the original instance,it should be restored by Xtrabackup again.
Summary
  • This method is only used to resuce a single table without backup when MySQL instance cannot startup beause of the destoryed ibdata file.
  • mysqlfrm is a tool which can load table structure from .frm files.We need to install the mysql-utilities package first.
  • If we don't have a properly full Xtrabackup and binlog,the .ibd file may lose the undo information on target table.In this situation,it's an incompletely recovery.

 

ibdata1文件损坏时恢复InnoDB单表测试的更多相关文章

  1. Innodb单表数据物理恢复

    本文将介绍使用物理备份恢复Innodb单表数据的方法 前言: 随着innodb的普及,innobackup也成为了主流备份方式.物理备份对于新建slave,全库恢复的需求都能从容应对. 但当面临单表数 ...

  2. 记录SQL Server2008日志文件损坏的恢复过程

    记录SQL Server2008日志文件损坏的恢复过程: 环境: 系 统:Windows Server2003 数据库:SQL Server2008 故障原因: 通过mstsc链接同一服务器时,用户界 ...

  3. mysql启动后随即关闭问题解决(ibdata1文件损坏导致)

    机房一台服务器上的mysql运行一段时间了,突然出现了一个很奇怪的现象:重启后无法恢复了!准确情况是:启动mysql后随即就又关闭了. 查看mysql错误日志如下: 160920 22:41:41 m ...

  4. mongodb文件损坏的恢复--无可恢复数据

    1.mongodb 启动异常error code 100,检查日志,数据文件损坏 2 检查collection-15-6548623434943640018.wt 可恢复数据,为空,不存在恢复的数据 ...

  5. django第8天(在测试文件中运行django项目|单表操作)

    django第8天 在测试文件中运行django项目 1.将项目配置文件数据库该为mysql,修改配置信息 PORT = '127.0.0.1' DATABASES = { 'default': { ...

  6. 详解MySQL表空间以及ibdata1文件过大问题

    ibdata1文件过大 原因分析 ibdata1是一个用来构建innodb系统表空间的文件,关于系统表空间详细介绍参考MySQL官网文档 上面是一个数据库的ibdata1文件,达到了780多G,而且还 ...

  7. ibdata1文件--缩小mysql数据库的ibdata1文件

    摘要 在MySQL数据库中,如果不指定innodb_file_per_table参数,单独存在每个表的数据,MySQL的数据都会存放在ibdata1文件. mysql ibdata1存放数据,索引等, ...

  8. Oracle - 各类文件损坏处理办法(附实验步骤)

    一.概述 本文将给大家介绍oracle各类文件损坏的现象和应对策略,请注意所有的恢复都是基于有备份的情况,所以请开启数据库的日常备份.文章将从以下文件展开 a. 密码文件 b. 参数文件 c. 控制文 ...

  9. SQL Server单表已700w+将普通表转换成分区表

    最近项目中,某个表数据量爆发时增长,单表已700w+,读写性能急剧下降,所以考虑加入分区表以解燃眉之急,后续还是要分表分库,当然这是后话.下面简要说一下将普通表转为分区表的步骤.   一.创建文件组 ...

随机推荐

  1. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

  2. 【luogu P1268 树的重量】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1268 给定所有点间的最短路求原图所有路径和 形如: 我们需要计算红边+绿边 绿边 = (红边+蓝边+紫边)/ ...

  3. Trident中 FixedBatchSpout分析

    FixedBatchSpout 继承自 IBatchSpout IBatchSpout 方法 public interface IBatchSpout extends Serializable { v ...

  4. UIlable上下居中

    - (void)setView{ self.chooseImg = [[UIImageView alloc] initWithFrame:CGRectMake(TO_LEFT_D, (ROW_HEIG ...

  5. wsgiref手写一个web服务端

    ''' 通过wsgiref写一个web服务端先讲讲wsgiref吧,基于网络通信其根本就是基于socket,所以wsgiref同样也是通过对socket进行封装,避免写过多的代码,将一系列的操作封装成 ...

  6. Vue+node.js+express+mysql实例---对图书信息进行管理

    一个简单的 CURD 实例 ---对图书信息进行管理 目录 1 开发环境 1.1 前端开发环境 1.2 后端开发环境 2 数据库设计和创建 2.1 数据库和表设计 2.2 book 表设计 2.3 s ...

  7. Evercookie

    1. Evercookie Evercookie是一个Javascript API,可以在浏览器中生成极其持久的cookie. 它的目标是在客户删除标准cookie,Flash cookie(本地共享 ...

  8. 数论(一)LOJ1282

    1.题目来源LOJ1282 You are given two integers: n and k, your task is to find the most significant three d ...

  9. 动态规划(二)HDU1114

    1.题目来源HDU1114 Sample Input 3 10 110 2 1 1 30 50 10 110 2 1 1 50 30 1 6 2 10 3 20 4 Sample Output The ...

  10. ABAP术语-Business Object

    Business Object 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/07/1028364.html Represents a ce ...