Mysql database drop table does not need to run away, table space transmission helps you recover data

Today, I will introduce you to a Mysql database that uses InnoDb's table space transmission function to help you restore the business table of the drop.

Mysql table space transfer restrictions
To use the Mysql database table space transfer function, there are two restrictions:
1. Mysql database version must be above 5.6
2. Mysql database must open innodb_file_per_table


The test1 table in the test target test library tmp is dropped, and you need to use the test1 table record in the testdb library to restore

The target database test table drop
logs in to the database, and the tmp.test1 business table is dropped.


[root@localhost] 16:14:12 [tmp]>drop table test1;
Query OK, 0 rows affected (0.41 sec)

Create an empty test1 table in the target database,
export the test1 table definition statement from the source database testdb, and view the original table records

[root@localhost] 15:49:45 [testdb]>show create table testdb.test1\G;
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `id` int(11) NOT NULL,
  `name1` char(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `name2` char(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.00 sec)

ERROR:
No query specified

[root@localhost] 15:49:54 [testdb]>select * from testdb.test1;
+----+-------+-------+
| id | name1 | name2 |
+----+-------+-------+
|  1 | test1 | test1 |
|  2 | test  | test  |
+----+-------+-------+
2 rows in set (0.00 sec)

Create an empty table tmp.test1 on the target library


[root@localhost] 15:50:45 [tmp]>show create table tmp.test1\G;
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `id` int(11) NOT NULL,
  `name1` char(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  `name2` char(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.00 sec)

discard deletes the ibd file of the target database business table


ALTER TABLE tmp.test1 DISCARD TABLESPACE;

Copy the ibd file of the test1 table from the source library to the target library


[mysql@mysql tmp]$ cp /data/mysql/data/3306/testdb/test1.ibd /data/mysql/data/3306/tmp/
[mysql@mysql tmp]$ ls -l
total 108
-rw-r-----. 1 mysql mysql  8620 Sep 17 15:50 test1.frm
-rw-r-----. 1 mysql mysql 98304 Sep 17 15:52 test1.ibd

Import the ibd file
from the target library In the target library, import the ibd file


[root@localhost] 15:51:34 [tmp]>ALTER TABLE tmp.test1 IMPORT TABLESPACE;
Query OK, 0 rows affected, 1 warning (0.02 sec)

[root@localhost] 15:53:11 [tmp]>SHOW WARNINGS;
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Level   | Code | Message                                                                                                                                   |
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Warning | 1810 | InnoDB: IO Read error: (2, No such file or directory) Error opening './tmp/test1.cfg', will attempt to import without schema verification |
+---------+------+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

[root@localhost] 15:53:18 [tmp]>select * from tmp.test1;
+----+-------+-------+
| id | name1 | name2 |
+----+-------+-------+
|  1 | test1 | test1 |
|  2 | test  | test  |
+----+-------+-------+
2 rows in set (0.00 sec)

At this point, you can see that the data in the business table of the source database testdb.test1 has been restored to the business table of the target database tmp.test1.

To sum up
, there is one step that is most important here, which is to import the ibd file. Think about it. When importing the idb file, what exactly did Mysql do? A total of 3 things are done
1. The imported ibd file will be done on every page. A checksum to see if there are bad pages
2. Update the table space id and LSN number of the header of each page
3. Set the page page as a dirty page and flush it to disk

Guess you like

Origin blog.51cto.com/15061930/2642067