如何恢复MySQL特定表的数据?
- By : Will
- Category : MySQL & MariaDB

MySQL & MariaDB
1 前言
一个问题,一篇文章,一出故事。
笔者今天与DBA配合做数据恢复,DBA要求把完整的逻辑备份的SQL语句根据两个关键字截取出一段SQL语句,于是整理此文。
2 最佳实践
2.1 筛选恢复语句
2.1.1 取得数据库中的Tab列表
zcat weeklyBackup-20220626.sql.gz | grep "Table structure for table" > tables.list
执行完成后,可使用如下命令查询列表,
less tables.list
键入指令“/tab0x”搜索表“tab0x”,可见如下显示,
#... -- Table structure for table `tab0x` -- Table structure for table `tab0y` #...
从以上列表可知,
– 以上列表我们目标导出的表名称为“tab0x”,
– 表“tab0x”的下一张表名称为“tab0y”
另外,直接从数据库中取得表列表会更快,
echo 'use database_name; show tables;' | mysql -u root -p > tables.list
2.1.2 截取恢复表的语句
zcat weeklyBackup-20220626.sql.gz | sed -n -e '/-- Table structure for table `tab0x`/,/-- Table structure for table `tab0y`/p' > /backup/temp_tabname.sql
2.2 导入数据到临时库
2.2.1 创建临时数据库
mysql -u root -p create database temp_database_name;
2.2.2 将临时表导入临时库
mysql -u root -p use temp_database_name; source /backup/temp_tabname.sql
参阅文档
==============
https://blog.51cto.com/lgdvsehome/1243307
没有评论