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.
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)
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
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
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
cp test1.cfg test2.cfg cp test1.ibd test2.ibd chown -R mysql.mysql test2.*
mysql> select * from test2; ERROR 1100 (HY000): Table 'test2' was not locked with LOCK TABLES
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)
mysql> select * from test2; +----+--------+ | id | name | +----+--------+ | 1 | 张三 | | 2 | 李四 | | 3 | 王二 | +----+--------+ 3 rows in set (0.00 sec)
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.
简单总结一下上述物理复制过程:
alter table for export syntax introduction:
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!