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

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use