Home  >  Article  >  Database  >  Teach you how to restore a single table in MySQL through physical methods

Teach you how to restore a single table in MySQL through physical methods

藏色散人
藏色散人forward
2023-01-22 08:30:021824browse

This article brings you relevant knowledge about MySQL, which mainly introduces how to use physical methods to quickly restore a single table in MySQL, and teaches you step by step! Let’s take a look at it together, I hope it will be helpful to everyone.

Teach you how to restore a single table in MySQL through physical methods

Usage method

1. First create a test table test1 and insert several pieces of data:

mysql> create table test1 (id int auto_increment primary key,name varchar(20));
Query OK, 0 rows affected (0.05 sec)

mysql> insert into test1 (name) values ('张三'),('李四'),('王二');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test1;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 李四   |
|  3 | 王二   |
+----+--------+
3 rows in set (0.00 sec)

2. Create the target table test2:

mysql> create table test2 like test1;
Query OK, 0 rows affected (0.10 sec)
查看数据目录里面的ibd文件(test2.ibd、test1.ibd):
-rw-r-----. 1 * * 114688 Nov  2 16:20 test1.ibd
-rw-r-----. 1 * * 114688 Nov  2 16:23 test2.ibd

3. Discard the idb file of table test2 through the alter table discard method (to prepare for the next step of copying the data of test1):

mysql> alter table test2 discard tablespace;   
Query OK, 0 rows affected (0.02 sec)

查看ibd文件情况,发现test2的ibd文件已经被删除

-rw-r----- 1 * * 114688 Nov  2 16:20 test1.ibd

4. Execute the following command, Generate a cfg file of test1, as follows:

mysql> flush table test1 for export; 
Query OK, 0 rows affected (0.00 sec)

生成了一个test1.cfg的cfg文件

-rw-r----- 1 * *    655 Nov  2 16:25 test1.cfg
-rw-r----- 1 * * 114688 Nov  2 16:20 test1.ibd

5. Copy the cfg file and ibd file of the source table test1 to the target table test2, and modify the file permissions:

cp test1.cfg test2.cfg
cp test1.ibd test2.ibd
chown -R mysql.mysql test2.*

6. After the copy is completed , execute the select command and find the following error:

mysql> select * from test2;
ERROR 1100 (HY000): Table 'test2' was not locked with LOCK TABLES

7. Execute unlock tables, release the test1.cfg file of the source table, and then import the ibd file:

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

并用alter table的方法为目标表test2导入这个ibd文件:

mysql> alter table test2 import tablespace; 
Query OK, 0 rows affected (0.03 sec)
1 row in set (0.00 sec)

8. Execute select again and find that The data has been imported:

mysql> select * from test2;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 李四   |
|  3 | 王二   |
+----+--------+
3 rows in set (0.00 sec)

Introduction to physical copy method

The core of the above single table physical copy method lies in the cp command. Because it is a physical copy, if the copied table is very large, then through Physical copy will be much faster than logical SQL writing, such as insert into select statement.

简单总结一下上述物理复制过程:
  • 1. Create table like syntax creates an empty target table with the same table structure
  • 2. The target table executes alter table discard and discards the ibd file
  • 3. Execute the alter table for export syntax in the source table to generate the .cfg file and lock the table
  • 4. Use the cp command to copy the source table cfg file and ibd file to the target table
  • 5. Unlock tables releases the cfg file and lock of the source table
  • 6. The alter table import command imports the ibd data file of the target table.

alter table for export syntax introduction:

  • 1. This command is to refresh the data about this table in the memory to the disk to ensure that the data can be binlogged Recorded;
  • 2. This operation requires flush table or reload permission;
  • 3. This operation will hold the shared MDL lock of the current table, preventing other sessions from modifying the table structure. In FOR EXPORT The previously acquired MDL lock will not be released when the operation is completed and needs to be released manually
  • 4. InnoDB will generate a file named table_name.cfg in the same database directory as the table
  • 5 After processing the table copy, you need to use UNLOCK tables to release the MDL lock of the source table or disconnect the connection.

Note:

Because alter table for export locks the table, this method is more suitable to stop the replication relationship on the slave database, and then perform the table replication operation. If there is a business operation on the current source table, careful consideration is required.

Recommended learning: "MySQL Video Tutorial"

The above is the detailed content of Teach you how to restore a single table in MySQL through physical methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete