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

핫 AI 도구

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

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

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

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

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

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

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제









MySQL 8.4(2024년 최신 LTS 릴리스)에 도입된 주요 변경 사항 중 하나는 "MySQL 기본 비밀번호" 플러그인이 더 이상 기본적으로 활성화되지 않는다는 것입니다. 또한 MySQL 9.0에서는 이 플러그인을 완전히 제거합니다. 이 변경 사항은 PHP 및 기타 앱에 영향을 미칩니다.

PHP를 사용하여 MySQL 연결 풀을 설정하면 성능과 확장성을 향상시킬 수 있습니다. 단계에는 다음이 포함됩니다. 1. MySQLi 확장을 설치합니다. 2. 연결 풀 클래스를 생성합니다. 4. 연결 풀 인스턴스를 생성합니다. 연결 풀링을 통해 애플리케이션은 각 요청에 대해 새 데이터베이스 연결을 생성하는 것을 방지하여 성능을 향상시킬 수 있습니다.

PHP는 MySQL 테이블의 데이터를 삭제하기 위해 다음 방법을 제공합니다. DELETE 문: 테이블에서 조건과 일치하는 행을 삭제하는 데 사용됩니다. TRUNCATETABLE 문: 자동 증가된 ID를 포함하여 테이블의 모든 데이터를 지우는 데 사용됩니다. 실제 사례: HTML 양식과 PHP 코드를 사용하여 데이터베이스에서 사용자를 삭제할 수 있습니다. 양식은 사용자 ID를 제출하고 PHP 코드는 DELETE 문을 사용하여 사용자 테이블에서 ID와 일치하는 레코드를 삭제합니다.

PHP가 MySQL에 연결 한 후 페이지가 비어 있고 Die () 함수가 실패한 이유가 있습니다. PHP와 MySQL 데이터베이스 간의 연결을 배울 때는 종종 혼란스러운 것들이 발생합니다 ...

PHP ...

많은 웹 사이트 개발자는 램프 아키텍처에서 Node.js 또는 Python 서비스를 통합하는 문제에 직면 해 있습니다. 기존 램프 (Linux Apache MySQL PHP) 아키텍처 웹 사이트 요구 사항 ...

PC 및 모바일 측면에서 동일한 페이지를 공유하고 캐시 문제를 처리하는 방법은 무엇입니까? Baota 배경, PC 측면을 만드는 방법 및 ...

이 기사에서 PHP 낙관적 잠금 및 거래와 함께 균형을 공제하는 문제에 대한 자세한 설명은 PHP, 낙관적 잠금 및 데이터베이스 트랜잭션을 사용한 균형 공제를 자세히 분석합니다.
