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.
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!

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于架构原理的相关内容,MySQL Server架构自顶向下大致可以分网络连接层、服务层、存储引擎层和系统文件层,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

mysql的msi与zip版本的区别:1、zip包含的安装程序是一种主动安装,而msi包含的是被installer所用的安装文件以提交请求的方式安装;2、zip是一种数据压缩和文档存储的文件格式,msi是微软格式的安装包。

方法:1、利用right函数,语法为“update 表名 set 指定字段 = right(指定字段, length(指定字段)-1)...”;2、利用substring函数,语法为“select substring(指定字段,2)..”。

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,可以利用REGEXP运算符判断数据是否是数字类型,语法为“String REGEXP '[^0-9.]'”;该运算符是正则表达式的缩写,若数据字符中含有数字时,返回的结果是true,反之返回的结果是false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
