찾다
데이터 베이스MySQL 튜토리얼MySQL之truncate表后恢复思路整理(前提是有备份且开启binlog)_MySQL

1.1对数据库thunder进行备份

mysqldump -S /tmp/mysql3316.sock  --single-transaction --master-data=2 thunder >thunder_full_2015112.sql

 

1.2进行truncate table操作并insert into table


(work)root@localhost:mysql3316.sock [(none)]>select * from thunder.tb1;
+----+---------+
| id | name    |
+----+---------+
|  1 | test    |
|  4 | thun    |
|  5 | thunder |
|  6 | thun    |
|  7 | thun    |
|  8 | thun    |
|  9 | test    |
+----+---------+
7 rows in set (0.00 sec)
(work)root@localhost:mysql3316.sock [(none)]>truncate table thunder.tb1;
Query OK, 0 rows affected (0.02 sec)
(work)root@localhost:mysql3316.sock [(none)]>insert into thunder.tb1(name) values('like');  
Query OK, 1 row affected (0.00 sec)
(work)root@localhost:mysql3316.sock [(none)]>insert into thunder.tb1(name) values('lik');
Query OK, 1 row affected (0.00 sec)
(work)root@localhost:mysql3316.sock [(none)]>insert into thunder.tb1(name) values('li');
Query OK, 1 row affected (0.00 sec)
(work)root@localhost:mysql3316.sock [(none)]>insert into thunder.tb1(name) values('l');
Query OK, 1 row affected (0.00 sec)
(work)root@localhost:mysql3316.sock [(none)]>select * from thunder.tb1;
+----+---------+
| id | name    |
+----+---------+
|  1 | test    |
|  4 | thun    |
|  5 | thunder |
|  6 | thun    |
|  7 | thun    |
|  8 | thun    |
|  9 | test    |
| 10 | like    |
| 11 | lik     |
| 12 | li      |
| 13 | l       |
+----+---------+
11 rows in set (0.00 sec)


 

1.3查看备份时binlog位置并找出误操作语句的位置


[root@mysqlnode1 mysql3316]# grep MASTER thunder_full_2015112.sql 
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=25191;
[root@mysqlnode1 ~]# mysql -S /tmp/mysql3316.sock -e "show binlog events in 'mysql-bin.000003'"|grep -i truncate
mysql-bin.000003        1011    Query   1163316 1091    truncate mysql.db
mysql-bin.000003        25239   Query   1163316 25328   truncate table thunder.tb1


1.4用mysqlbinlog命令在binlog中找出相关记录


[root@mysqlnode1 mysql3316]# mysqlbinlog -v --base64-output=decode-rows 
/data/mysql/mysql3316/logs/mysql-bin.000003 >3.sql 
[root@mysqlnode1 mysql3316]# vim 3.sql 
# at 25191
#151122 19:59:50 server id 1163316  end_log_pos 25239 CRC32 0xac936e4e  GTID [commit=yes]
SET @@SESSION.GTID_NEXT= 'c009ae77-8def-11e5-ab11-000c29ea831c:78'/*!*/;
# at 25239
#151122 19:59:50 server id 1163316  end_log_pos 25328 CRC32 0xd020ddc8  Query   thread_id=175   exec_time=0    
 error_code=0
SET TIMESTAMP=1448193590/*!*/;
truncate table thunder.tb1
/*!*/;
# at 25328
#151122 20:00:50 server id 1163316  end_log_pos 25376 CRC32 0xa12c299c  GTID [commit=yes]
SET @@SESSION.GTID_NEXT= 'c009ae77-8def-11e5-ab11-000c29ea831c:79'/*!*/;
# at 25376
#151122 20:00:50 server id 1163316  end_log_pos 25444 CRC32 0xaa7072db  Query   thread_id=175   exec_time=0    
 error_code=0
SET TIMESTAMP=1448193650/*!*/;
BEGIN
/*!*/;
# at 25444
#151122 20:00:50 server id 1163316  end_log_pos 25496 CRC32 0xd45864c2  Table_map: `thunder`.`
tb1` mapped to number 267
# at 25496
#151122 20:00:50 server id 1163316  end_log_pos 25541 CRC32 0x71343558 
 Write_rows: table id 267 flags: STMT_END_F
### INSERT INTO `thunder`.`tb1`
### SET
###   @1=1
###   @2='like'
# at 25541
#151122 20:00:50 server id 1163316  end_log_pos 25572 CRC32 0xd860159a  Xid = 4442
COMMIT/*!*/;


 

 

经查看在备份前master status位置后紧跟truncate命令,而后是insert,所以不需要执行binlog增量恢复,只需要把insert的数据还原,此处如果有数据,在导入备份后还需要执行这条语句

mysqlbinlog  --start-position=18311 --stop-position=19557 mysql-bin.000003 |mysql -S /tmp/mysql3316.sock 
thunder

 

其中--start-position为数据备份文件中的位置,--stop-position为误操作前的位置

 

1.5新建一个数据库thued,并还原之前thunder库的备份

[root@mysqlnode1 mysql3316]# mysql -S /tmp/mysql3316.sock thued<thunder_full_2015112.sql

 

1.6在thued.tb1中插入新增加的数据,并查看


(work)root@localhost:mysql3316.sock [thued]>insert into tb1(name) select name from thunder.tb1;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
(work)root@localhost:mysql3316.sock [thued]>select * from tb1;                                 
+----+---------+
| id | name    |
+----+---------+
|  1 | test    |
|  4 | thun    |
|  5 | thunder |
|  6 | thun    |
|  7 | thun    |
|  8 | thun    |
|  9 | test    |
| 10 | like    |
| 11 | lik     |
| 12 | li      |
| 13 | l       |
+----+---------+
11 rows in set (0.00 sec)


 

 

1.7删除thunder.tb1,把thued.tb1改名成thunder.tb1


(work)root@localhost:mysql3316.sock [thued]>drop table thunder.tb1;
Query OK, 0 rows affected (0.00 sec)
(work)root@localhost:mysql3316.sock [thued]>rename table thued.tb1 to thunder.tb1;
Query OK, 0 rows affected (0.00 sec)
(work)root@localhost:mysql3316.sock [thued]>select * from thunder.tb1;
+----+---------+
| id | name    |
+----+---------+
|  1 | test    |
|  4 | thun    |
|  5 | thunder |
|  6 | thun    |
|  7 | thun    |
|  8 | thun    |
|  9 | test    |
| 10 | like    |
| 11 | lik     |
| 12 | li      |
| 13 | l       |
+----+---------+
11 rows in set (0.00 sec)


 

以上就是MySQL之truncate表后恢复思路整理(前提是有备份且开启binlog)_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
图文详解mysql架构原理图文详解mysql架构原理May 17, 2022 pm 05:54 PM

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

mysql的msi与zip版本有什么区别mysql的msi与zip版本有什么区别May 16, 2022 pm 04:33 PM

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

mysql怎么去掉第一个字符mysql怎么去掉第一个字符May 19, 2022 am 10:21 AM

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

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

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

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

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

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

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

mysql怎么判断是否是数字类型mysql怎么判断是否是数字类型May 16, 2022 am 10:09 AM

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

带你把MySQL索引吃透了带你把MySQL索引吃透了Apr 22, 2022 am 11:48 AM

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

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

mPDF

mPDF

mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.