search
HomeDatabaseMysql TutorialMySQL使用二进制日志来恢复数据_MySQL

mysqlbinlog工具的使用,大家可以看MySQL的帮助手册。里面有详细的用,

在这个例子中,重点是--start-position参数和--stop-position参数的使用。

 

·--start-position=N

 

从二进制日志中第个位置等于N参量时的事件开始读。

 

·--stop-position=N

 

从二进制日志中第个位置等于和大于N参量时的事件起停止读。

 

 

 

OK,现在开始,要启动二进制日志记录,要先在my.cnf / my.ini文件的mysqld里添加

 

log-bin=日志名

 

在这里,偶是的设置是log-bin=liangck

 

然后再启动mysql服务,因为偶是用windows系统,所以执行net start mysql命令即可。

 

 

 

然后在一测试数据库里,创建一个表,并添加记录。

 

mysql> create table test(id int auto_increment not null primary key,val int,data varchar(20));

 

mysql> insert into test(val,data) values(10,'liang');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> insert into test(val,data) values(20,'jia');

 

Query OK, 1 row affected (0.08 sec)

 

mysql> insert into test(val,data) values(30,'hui');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> flush logs;   --产生第二个日志文件

 

Query OK, 0 rows affected (0.09 sec)

 

mysql> insert into test(val,data) values(40,'aaa');

 

Query OK, 1 row affected (0.05 sec)

 

mysql> insert into test(val,data) values(50,'bbb');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> insert into test(val,data) values(60,'ccc');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> delete from test where id between 4 and 5;  --删除记录

 

Query OK, 2 rows affected (0.05 sec)

 

mysql> insert into test(val,data) values(70,'ddd');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> flush logs;          --产生第三个文件文件

 

Query OK, 0 rows affected (0.11 sec)

 

mysql> insert into test(val,data) values(80,'dddd');

 

Query OK, 1 row affected (0.05 sec)

 

mysql> insert into test(val,data) values(90,'eeee');

 

Query OK, 1 row affected (0.03 sec)

 

mysql> drop table test;       --删除表

 

Query OK, 0 row affected (0.05 sec)

 

――――――――――――――――――――――――――――――――――

 

OK,现在测试数据已经建好了,要求是什么呢?

 

就是将test表的数据全部恢复出来。

 

先用mysqlbinlog工具将日志文件生成txt文件出来分析。

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000001 > G:/001.txt

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000002 > G:/002.txt

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000003 > G:/003.txt

 

通过这三个命令,可以在G盘下生成个文件,里面分别记录了日志文件的内容,也就是用户操作的步骤。

 

因为我们需要重做第一个日志文件的所有操作,所以这里只需要将第一个日志文件全恢复就行了。

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000001 | mysql -uroot –p

 

Ok,接着,我们需要分析的是第二个日志文件。为什么要分析它呢,因为它中途执行了一个操作是DELETE,因为我们要做的是恢复全部数据,也就是我们不希望去重做这个语句。所以在这里我们要想办法去绕开它。

 

我们先打开.txt文件来分析一下。

 

/*

 

 

/*!40019 SET @@session.max_insert_delayed_threads=0*/;

 

/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;

 

DELIMITER /*!*/;

 

# at 4

 

#090427 15:27:56 server id 1  end_log_pos 106 Start: binlog v 4, server v 5.1.32-community-log created 090427 15:27:56

 

BINLOG '

 

fF71SQ8BAAAAZgAAAGoAAAAAAAQANS4xLjMyLWNvbW11bml0eS1sb2cAAAAAAAAAAAAAAAAAAAAA

 

AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAUwAEGggAAAAICAgC

 

'/*!*/;

 

# at 106

 

#090427 15:28:37 server id 1  end_log_pos 176 Query  thread_id=1   exec_time=0   error_code=0

 

use mytest/*!*/;

 

SET TIMESTAMP=1240817317/*!*/;

 

SET @@session.pseudo_thread_id=1/*!*/;

 

SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;

 

SET @@session.sql_mode=1344274432/*!*/;

 

SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;

 

/*!/C gbk *//*!*/;

 

SET @@session.character_set_client=28,@@session.collation_connection=28,@@session.collation_server=28/*!*/;

 

SET @@session.lc_time_names=0/*!*/;

 

SET @@session.collation_database=DEFAULT/*!*/;

 

BEGIN

 

/*!*/;

 

# at 176

 

#090427 15:28:37 server id 1  end_log_pos 204 Intvar

 

SET INSERT_ID=4/*!*/;

 

# at 204

 

#090427 15:28:37 server id 1  end_log_pos 312 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817317/*!*/;

 

insert into test(val,data) values(40,'aaa')

 

/*!*/;

 

# at 312

 

#090427 15:28:37 server id 1  end_log_pos 339 Xid = 12

 

COMMIT/*!*/;

 

# at 339

 

#090427 15:28:46 server id 1  end_log_pos 409 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817326/*!*/;

 

BEGIN

 

/*!*/;

 

# at 409

 

#090427 15:28:46 server id 1  end_log_pos 437 Intvar

 

SET INSERT_ID=5/*!*/;

 

# at 437

 

#090427 15:28:46 server id 1  end_log_pos 545 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817326/*!*/;

 

insert into test(val,data) values(50,'bbb')

 

/*!*/;

 

# at 545

 

#090427 15:28:46 server id 1  end_log_pos 572 Xid = 13

 

COMMIT/*!*/;

 

# at 572

 

#090427 15:29:35 server id 1  end_log_pos 642 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817375/*!*/;

 

BEGIN

 

/*!*/;

 

# at 642

 

#090427 15:29:35 server id 1  end_log_pos 670 Intvar

 

SET INSERT_ID=6/*!*/;

 

# at 670

 

#090427 15:29:35 server id 1  end_log_pos 778 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817375/*!*/;

 

insert into test(val,data) values(60,'ccc')

 

/*!*/;

 

# at 778

 

#090427 15:29:35 server id 1  end_log_pos 805 Xid = 14

 

COMMIT/*!*/;

 

# at 805

 

#090427 15:30:21 server id 1  end_log_pos 875 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817421/*!*/;

 

BEGIN

 

/*!*/;

 

# at 875

 

#090427 15:30:21 server id 1  end_log_pos 981 Query  thread_id=1   exec_time=0   error_code=0

 

SET TIMESTAMP=1240817421/*!*/;

 

delete from test where id between 4 and 5

 

/*!*/;

 

# at 981

 

#090427 15:30:21 server id 1  end_log_pos 1008    Xid = 15

 

COMMIT/*!*/;

 

# at 1008

 

#090427 15:30:34 server id 1  end_log_pos 1078    Query  thread_id=1   exec_time=0    error_code=0

 

SET TIMESTAMP=1240817434/*!*/;

 

BEGIN

 

/*!*/;

 

# at 1078

 

#090427 15:30:34 server id 1  end_log_pos 1106    Intvar

 

SET INSERT_ID=7/*!*/;

 

# at 1106

 

#090427 15:30:34 server id 1  end_log_pos 1214    Query  thread_id=1   exec_time=0    error_code=0

 

SET TIMESTAMP=1240817434/*!*/;

 

insert into test(val,data) values(70,'ddd')

 

/*!*/;

 

# at 1214

 

#090427 15:30:34 server id 1  end_log_pos 1241    Xid = 16

 

COMMIT/*!*/;

 

# at 1241

 

#090427 15:30:41 server id 1  end_log_pos 1282    Rotate to liangck.000003  pos: 4

 

DELIMITER ;

 

# End of log file

 

ROLLBACK /* added by mysqlbinlog */;

 

/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

 

 

 

―――――――――――――――――――――――――――――――――――――

 

*/

 

 

在这个文件中,我们可以看到DELETE的操作的起始位置是,终止位置是.那么我们只要重做第二个日志文件的开头到的操作,然后再从到末尾的操作,我们就可以把数据给恢复回来,而不会DELETE数据。所以执行两个命令:

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000002 --stop-pos=875 | mysql -uroot -p

 

 

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000002 --start-pos=1008 | mysql -uroot -p mytest

 

 

 

OK,现在第二个日志文件的数据了。

 

第三个日志文件也是同理,只要找到DROP TABLE的位置,就可以了。

 

F:/Program Files/MySQL_Data/data/log>mysqlbinlog liangck.000003 --stop-pos=574 | mysql -uroot –p

 

 

 

现在我们再查一下数据看看:

 

mysql> select * from test;

 

+----+------+-------+

 

| id | val  | data  |

 

+----+------+-------+

 

|  1 |   10 | liang |

 

|  2 |   20 | jia   |

 

|  3 |   30 | hui   |

 

|  4 |   40 | aaa   |

 

|  5 |   50 | bbb   |

 

|  6 |   60 | ccc   |

 

|  7 |   70 | ddd   |

 

|  8 |   80 | dddd  |

 

|  9 |   90 | eeee  |

 

+----+------+-------+

 

9 rows in set (0.00 sec)

 

 

 

可以看到,全部数据都回来了。

Statement
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
How to Grant Permissions to New MySQL UsersHow to Grant Permissions to New MySQL UsersMay 09, 2025 am 12:16 AM

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

How to Add Users in MySQL: A Step-by-Step GuideHow to Add Users in MySQL: A Step-by-Step GuideMay 09, 2025 am 12:14 AM

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

MySQL: Adding a new user with complex permissionsMySQL: Adding a new user with complex permissionsMay 09, 2025 am 12:09 AM

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

MySQL: String Data Types and CollationsMySQL: String Data Types and CollationsMay 09, 2025 am 12:08 AM

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

MySQL: What length should I use for VARCHARs?MySQL: What length should I use for VARCHARs?May 09, 2025 am 12:06 AM

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQL BLOB : are there any limits?MySQL BLOB : are there any limits?May 08, 2025 am 12:22 AM

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

MySQL : What are the best tools to automate users creation?MySQL : What are the best tools to automate users creation?May 08, 2025 am 12:22 AM

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

MySQL: Can I search inside a blob?MySQL: Can I search inside a blob?May 08, 2025 am 12:20 AM

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment