mysql 無備份恢復drop資料-共享表空間

shawnloong發表於2017-11-12

一.安裝undrop-for-innodb工具

點選(此處)摺疊或開啟

  1. [root@mysqltest-213-2 tools]# unzip undrop-for-innodb-master.zip


安裝依賴包

點選(此處)摺疊或開啟

  1. yum -y install gcc flex bison

編譯安裝

點選(此處)摺疊或開啟

  1. cd undrop-for-innodb-master
  2. make
  3. [root@mysqltest-213-2 undrop-for-innodb-master]# make
  4. cc -D_FILE_OFFSET_BITS=64 -Wall -g -O3 -pipe -I./include -c stream_parser.c
  5. cc -D_FILE_OFFSET_BITS=64 -Wall -g -O3 -pipe -I./include -pthread -lm stream_parser.o -o stream_parser
  6. flex sql_parser.l
  7. bison -o sql_parser.c sql_parser.y
  8. sql_parser.y: conflicts: 6 shift/reduce
  9. cc -D_FILE_OFFSET_BITS=64 -Wall -g -O3 -pipe -I./include -c sql_parser.c
  10. lex.yy.c:3084: warning: ‘yyunput’ defined but not used
  11. lex.yy.c:3125: warning: ‘input’ defined but not used
  12. cc -D_FILE_OFFSET_BITS=64 -Wall -g -O3 -pipe -I./include -c c_parser.c
  13. ./include/ctype-latin1.c:359: warning: ‘my_mb_wc_latin1’ defined but not used
  14. ./include/ctype-latin1.c:372: warning: ‘my_wc_mb_latin1’ defined but not used
  15. cc -D_FILE_OFFSET_BITS=64 -Wall -g -O3 -pipe -I./include -c tables_dict.c
  16. cc -D_FILE_OFFSET_BITS=64 -Wall -g -O3 -pipe -I./include -c print_data.c
  17. cc -D_FILE_OFFSET_BITS=64 -Wall -g -O3 -pipe -I./include -c check_data.c
  18. cc -D_FILE_OFFSET_BITS=64 -Wall -g -O3 -pipe -I./include sql_parser.o c_parser.o tables_dict.o print_data.o check_data.o -o c_parser -pthread -lm
  19. cc -D_FILE_OFFSET_BITS=64 -Wall -g -O3 -pipe -I./include -o innochecksum_changer innochecksum.c
  20. Bye




二測試
未開啟binlog

點選(此處)摺疊或開啟

  1. mysql> show variables like '%log_bin%';
  2. +---------------------------------+-------+
  3. | Variable_name | Value |
  4. +---------------------------------+-------+
  5. | log_bin | OFF |
  6. | log_bin_basename | |
  7. | log_bin_index | |
  8. | log_bin_trust_function_creators | OFF |
  9. | log_bin_use_v1_row_events | OFF |
  10. | sql_log_bin | ON |
  11. +---------------------------------+-------+




2.1建立一個庫,然後建一張表

點選(此處)摺疊或開啟

  1. mysql> show create database recovery;
  2. +----------+-------------------------------------------------------------------+
  3. | Database | Create Database |
  4. +----------+-------------------------------------------------------------------+
  5. | recovery | CREATE DATABASE `recovery` /*!40100 DEFAULT CHARACTER SET utf8 */ |
  6. +----------+-------------------------------------------------------------------+
建立恢復相關事例表

點選(此處)摺疊或開啟

  1. mysql> show create table accountinfo \G
  2. *************************** 1. row ***************************
  3. Table: accountinfo
  4. Create Table: CREATE TABLE `accountinfo` (
  5. `id` int(11) NOT NULL AUTO_INCREMENT,
  6. `username` varchar(50) DEFAULT NULL,
  7. `userpwd` varchar(50) DEFAULT NULL,
  8. `createtime` datetime DEFAULT NULL,
  9. PRIMARY KEY (`id`)
  10. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
2.2 建立批次插入儲存過程
隨機函式

點選(此處)摺疊或開啟

  1. DELIMITER $$
  2. USE `recovery`$$
  3. DROP FUNCTION IF EXISTS `rand_string`$$
  4. CREATE DEFINER=`root`@`%` FUNCTION `rand_string`(n INT) RETURNS VARCHAR(255) CHARSET latin1
  5. BEGIN
  6.     DECLARE chars_str VARCHAR(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  7.     DECLARE return_str VARCHAR(255) DEFAULT '';
  8.     DECLARE i INT DEFAULT 0;
  9.     WHILE i < n DO
  10.         SET return_str = CONCAT(return_str,SUBSTRING(chars_str , FLOOR(1 + RAND()*62 ),1));
  11.         SET i = i +1;
  12.     END WHILE;
  13.     RETURN return_str;
  14.     END$$
  15. DELIMITER ;
2.3 批次插入儲存過程

點選(此處)摺疊或開啟

  1. DELIMITER $$
  2. USE `recovery` $$
  3. DROP PROCEDURE IF EXISTS `p_repeat` $$
  4. CREATE DEFINER = `root` @`%` PROCEDURE `p_repeat` ()
  5. BEGIN
  6.   DECLARE v INT ;
  7.   SET v = 0 ;
  8.   REPEAT
  9.     INSERT INTO accountinfo
  10.     VALUES
  11.       (
  12.         NULL,
  13.         rand_string (5),
  14.         rand_string (15),
  15.         NOW()
  16.       ) ;
  17.     SET v = v + 1 ;
  18.     UNTIL v >= 1000
  19.   END REPEAT ;
  20. END $$
  21. DELIMITER ;




2.4呼叫儲存過程

點選(此處)摺疊或開啟

  1. CALL p_repeat;
  2. mysql> select count(*) from accountinfo;
  3. +----------+
  4. | count(*) |
  5. +----------+
  6. | 1000 |
  7. +----------+


2.5 刪除表

點選(此處)摺疊或開啟

  1. mysql> use recovery;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> drop table accountinfo;
  6. Query OK, 0 rows affected (0.02 sec)
2.6 undorp for innodb掃描檔案

點選(此處)摺疊或開啟

  1. [root@mysqltest-213-2 undrop-for-innodb-master]# ./stream_parser -f /var/lib/mysql/ibdata1
  2. Opening file: /var/lib/mysql/ibdata1
  3. File information:
  4. ID of device containing file: 64768
  5. inode number: 393238
  6. protection: 100660 (regular file)
  7. number of hard links: 1
  8. user ID of owner: 498
  9. group ID of owner: 500
  10. device ID (if special file): 0
  11. blocksize for filesystem I/O: 4096
  12. number of blocks allocated: 155648
  13. time of last access: 1510411206 Sat Nov 11 22:40:06 2017
  14. time of last modification: 1510411254 Sat Nov 11 22:40:54 2017
  15. time of last status change: 1510411254 Sat Nov 11 22:40:54 2017
  16. total size, in bytes: 79691776 (76.000 MiB)
  17. Size to process: 79691776 (76.000 MiB)
  18. Worker(0): 52.61% done. 2017-11-11 22:41:49 ETA(in 00:00:01). Processing speed: 31.984 MiB/sec
  19. Worker(0): 94.72% done. 2017-11-11 22:41:49 ETA(in 00:00:00). Processing speed: 32.000 MiB/sec
  20. All workers finished in 2 sec
  21. [root@mysqltest-213-2 undrop-for-innodb-master]# ls
  22. check_data.c dictionary innochecksum_changer pages-ibdata1 recover_dictionary.sh sql_parser.y tables_dict.c vote_record.sql
  23. check_data.o dumps lex.yy.c print_data.c sakila stream_parser tables_dict.o voterecord.sql
  24. c_parser fetch_data.sh LICENSE print_data.o sql_parser.c stream_parser.c test.sh
  25. c_parser.c include Makefile README.md sql_parser.l stream_parser.o t_userinfo.sql
  26. c_parser.o innochecksum.c member.sql recover sql_parser.o sys_parser.c userinfo.sql
2.7 解析檔案

點選(此處)摺疊或開啟

  1. [root@mysqltest-213-2 undrop-for-innodb-master]# ./c_parser -4Df pages-ibdata1/FIL_PAGE_INDEX/0000000000000001.page -t dictionary/SYS_TABLES.sql | grep accountinfo
  2. 00000007A103 030000024B0AC4 SYS_TABLES "recovery/accountinfo" 69 4 1 0 80 "" 55
  3. 00000007A103 030000024B0AC4 SYS_TABLES "recovery/accountinfo" 69 4 1 0 80 "" 55
  4. SET FOREIGN_KEY_CHECKS=0;
  5. LOAD DATA LOCAL INFILE '/usr/local/tools/undrop-for-innodb-master/dumps/default/SYS_TABLES' REPLACE INTO TABLE `SYS_TABLES` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'SYS_TABLES\t' (`NAME`, `ID`, `N_COLS`, `TYPE`, `MIX_ID`, `MIX_LEN`, `CLUSTER_NAME`, `SPACE`);

2.8 根據索引找到對應資料檔案72

點選(此處)摺疊或開啟

  1. [root@mysqltest-213-2 undrop-for-innodb-master]# ./c_parser -4Df pages-ibdata1/FIL_PAGE_INDEX/0000000000000003.page -t dictionary/SYS_INDEXES.sql |grep 69
  2. 00000007A103 030000024B096F SYS_INDEXES 69 72 "PRIMARY" 1 3 55 4294967295
  3. 00000007A103 030000024B096F SYS_INDEXES 69 72 "PRIMARY" 1 3 55 4294967295
  4. SET FOREIGN_KEY_CHECKS=0;
  5. LOAD DATA LOCAL INFILE '/usr/local/tools/undrop-for-innodb-master/dumps/default/SYS_INDEXES' REPLACE INTO TABLE `SYS_INDEXES` FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"' LINES STARTING BY 'SYS_INDEXES\t' (`TABLE_ID`, `ID`, `NAME`, `N_FIELDS`, `TYPE`, `SPACE`, `PAGE_NO`);




2.9 建立表結構

點選(此處)摺疊或開啟

  1. [root@mysqltest-213-2 undrop-for-innodb-master]# cat accountinfo.sql
  2. CREATE TABLE `accountinfo` (
  3.   `id` INT(11) NOT NULL AUTO_INCREMENT,
  4.   `username` VARCHAR(50) DEFAULT NULL,
  5.   `userpwd` VARCHAR(50) DEFAULT NULL,
  6.   `createtime` DATETIME DEFAULT NULL,
  7.   PRIMARY KEY (`id`)
  8. ) ENGINE=INNODB DEFAULT CHARSET=utf8;
2.10 解析檔案

點選(此處)摺疊或開啟

  1. [root@mysqltest-213-2 undrop-for-innodb-master]# ./c_parser -6f pages-ibdata1/FIL_PAGE_INDEX/0000000000000072.page -t accountinfo.sql | head -5
  2. -- Page id: 5, Format: COMPACT, Records list: Valid, Expected records: (301 301)
  3. 000000079BC0 B70000028B0110 accountinfo 151 "1TemU" "jHpNAo1LCC4mpQN" "2017-11-11 22:34:33.0"
  4. 000000079BC1 B80000016B0110 accountinfo 152 "iVzVM" "ZoQURpjcSEsbm7l" "2017-11-11 22:34:33.0"
  5. 000000079BC2 B90000016C0110 accountinfo 153 "cMcrs" "QDxBd9X9IYAPewJ" "2017-11-11 22:34:33.0"
  6. 000000079BC3 BA0000016D0110 accountinfo 154 "Xq4Ud" "c90s5T1ie6DLIav" "2017-11-11 22:34:33.0"
2.12建立恢復目錄

點選(此處)摺疊或開啟

  1. ./c_parser -6f pages-ibdata1/FIL_PAGE_INDEX/0000000000000072.page -t accountinfo.sql -o dumps/default/accountinfo.dump -l dumps/default/accountinfo.sql
  2. [root@mysqltest-213-2 undrop-for-innodb-master]# ls dumps/default/accountinfo.*
  3. dumps/default/accountinfo.dump dumps/default/accountinfo.sql
  4. mkdir -pv dumps/default/
2.13 恢復資料

點選(此處)摺疊或開啟

  1. [root@mysqltest-213-2 undrop-for-innodb-master]# mysql recovery;
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 2
  4. Server version: 5.6.27 MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
恢復表結構

點選(此處)摺疊或開啟

  1. mysql> source accountinfo.sql
  2. Query OK, 0 rows affected (0.04 sec)
恢復資料

點選(此處)摺疊或開啟

  1. mysql> source dumps/default/accountinfo.sql
  2. Query OK, 0 rows affected (0.00 sec)
  3. Query OK, 1000 rows affected (0.02 sec)
  4. Records: 1000 Deleted: 0 Skipped: 0 Warnings: 0
  5. mysql> select count(*) from accountinfo;
  6. +----------+
  7. | count(*) |
  8. +----------+
  9. | 1000 |
  10. +----------+
  11. 1 row in set (0.00 sec)


此文章參閱


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24486203/viewspace-2147147/,如需轉載,請註明出處,否則將追究法律責任。

相關文章