人工误删除InnoDB ibdata数据文件如何恢复?


在日常工作中,因不熟悉InnoDB引擎,在群里看到有很多人误删除了InnoDB ibdata(数据文件)和ib_logfile(redo log重做事务日志文件),结果导致了杯具的发生。如果你有做主从复制同步,那还好,如果是单机呢?如何恢复?

下面,请看恢复演示:

一、你可以用sysbench模拟数据的写入,如:

  1. sysbench --test=oltp --mysql-table-engine=innodb --oltp-table-size=10000000 --max-requests=10000 
  2. --num-threads=90 --mysql-host=192.168.110.140 --mysql-port=3306 --mysql-user=admin --mysql-password=123456 
  3. --mysql-db=test --oltp-table-name=uncompressed  --mysql-socket=/tmp/mysql.sock run 

二、rm -f ib*

三、此时我估计你被吓得够呛,脸白手哆嗦,如果你看到这篇文章,心可以稳稳了,没事,可以恢复的。

四、此时,你会发现数据库还可以正常工作,数据照样可以写入,切记,这时千万别把mysqld进程杀死,否则你只有跳楼了,神仙都没法救你。

五、先找到mysqld的进程pid

  1. # netstat -ntlp | grep mysqld 
  2. tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      30426/mysqld  

我这里是30426

六、执行关键的一步

  1. # ll /proc/30426/fd | egrep 'ib_|ibdata'      
  2. lrwx------ 1 root root 64  9月 24 16:51 10 -> /u2/mysql/data/ib_logfile1 
  3. lrwx------ 1 root root 64  9月 24 16:51 11 -> /u2/mysql/data/ib_logfile2 
  4. lrwx------ 1 root root 64  9月 24 16:51 4 -> /u2/mysql/data/ibdata1 
  5. lrwx------ 1 root root 64  9月 24 16:51 9 -> /u2/mysql/data/ib_logfile0 

10,11,4,9就是我们要恢复的文件。

七、你可以把前端业务关闭,或者执行FLUSH TABLES WITH READ LOCK;这一步的作用是让数据库没有写入操作,以便后面的恢复工作。

八、如何验证没有写入操作呢?分以下几步,记住要结合在一起观察。

  1. set global innodb_max_dirty_pages_pct=0;   
  2. # 让脏页尽快刷入到磁盘里。
  1. mysql> show master status; 
  2. +------------------+----------+--------------+------------------+ 
  3. File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
  4. +------------------+----------+--------------+------------------+ 
  5. mysql-bin.000002 |      107 |              |               | 
  6. +------------------+----------+--------------+------------------+ 
  7. 1 row in set (0.00 sec) 
确保File和Position值不在变化
  1. show engine innodb status\G;  
  2.   
  3. ------------  
  4. TRANSACTIONS  
  5. ------------  
  6. Trx id counter A21837  
  7. Purge done for trx's n:o < A21837 undo n:o < 0  
  8. ## 确保后台Purge进程把undo log全部清除掉,事务ID要一致。  
  9.   
  10. -------------------------------------  
  11. INSERT BUFFER AND ADAPTIVE HASH INDEX  
  12. -------------------------------------  
  13. Ibuf: size 1, free list len 65, seg size 67, 0 merges  
  14. ## insert buffer合并插入缓存等于1  
  15.  
  16. --- 
  17. LOG 
  18. --- 
  19. Log sequence number 18158813743 
  20. Log flushed up to 18158813743 
  21. Last checkpoint at 18158813743 
  22. ## 确保这3个值不在变化   
  23.  
  24. ---------------------- 
  25. BUFFER POOL AND MEMORY 
  26. ----------------------  
  27. Total memory allocated 643891200; in additional pool allocated 0 
  28. Dictionary memory allocated 39812 
  29. Buffer pool size   38400 
  30. Free buffers   37304 
  31. Database pages 1095 
  32. Old database pages 424 
  33. Modified db pages  0 
  34. ## 确保脏页数量为0  
  35.  
  36. -------------- 
  37. ROW OPERATIONS 
  38. -------------- 
  39. 0 queries inside InnoDB, 0 queries in queue 
  40. 1 read views open inside InnoDB 
  41. Main thread process no. 30426, id 140111500936976, state: waiting for server activity Number of rows inserted 0, updated 0, deleted 0, read 0 
  42. 0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s 
  43. ## 确保插入、更新、删除为0 

九、上面一系列确认工作完成之后,我们就可以恢复了。还记得刚才我们记录的删除文件吗?

  1. # ll /proc/30426/fd | egrep 'ib_|ibdata'       
  2. lrwx------ 1 root root 64  9月 24 16:51 10 -> /u2/mysql/data/ib_logfile1  
  3. lrwx------ 1 root root 64  9月 24 16:51 11 -> /u2/mysql/data/ib_logfile2  
  4. lrwx------ 1 root root 64  9月 24 16:51 4 -> /u2/mysql/data/ibdata1  
  5. lrwx------ 1 root root 64  9月 24 16:51 9 -> /u2/mysql/data/ib_logfile0  

把这些文件拷贝到原来的目录下并修改用户属性即可。

  1. #cd /proc/10755/fd 
  2. #cp 10 /u2/mysql/data/ib_logfile1 
  3. #cp 11 /u2/mysql/data/ib_logfile2 
  4. #cp 4 /u2/mysql/data/ibdata1 
  5. #cp 9 /u2/mysql/data/ib_logfile0 

并修改用户属性

  1. #cd /u2/mysql/data/ 
  2. #chown mysql:mysql ib* 

十、大功告成,只需要重启MySQL即可。

  1. /etc/init.d/mysql restart 

怎样?就这么简单,你也动手试试吧。

相关内容