Home Database Mysql Tutorial MySQL之truncate表后恢复思路整理(前提是有备份且开启binlog)_MySQL

MySQL之truncate表后恢复思路整理(前提是有备份且开启binlog)_MySQL

May 27, 2016 pm 01:46 PM

1.1对数据库thunder进行备份

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


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)
Copy after login


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
Copy after login


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/*!*/;
Copy after login


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

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


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

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

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


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)
Copy after login


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)
Copy after login


 

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


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values ​​to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the &quot;MySQL Native Password&quot; plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

How to set up MySQL connection pool using PHP? How to set up MySQL connection pool using PHP? Jun 04, 2024 pm 03:28 PM

Setting up a MySQL connection pool using PHP can improve performance and scalability. The steps include: 1. Install the MySQLi extension; 2. Create a connection pool class; 3. Set the connection pool configuration; 4. Create a connection pool instance; 5. Obtain and release connections. With connection pooling, applications can avoid creating a new database connection for each request, thereby improving performance.

How to delete data from MySQL table using PHP? How to delete data from MySQL table using PHP? Jun 05, 2024 pm 12:40 PM

PHP provides the following methods to delete data in MySQL tables: DELETE statement: used to delete rows matching conditions from the table. TRUNCATETABLE statement: used to clear all data in the table, including auto-incremented IDs. Practical case: You can delete users from the database using HTML forms and PHP code. The form submits the user ID, and the PHP code uses the DELETE statement to delete the record matching the ID from the users table.

See all articles