利用MySQL日志模拟恢复数据变化轨迹II
在上篇《利用MySQL日志模拟恢复数据变化轨迹》中,我们已经介绍了我们方案的大致思路,其原理就是某网友提到的捞日志方式。 通过mysqlbinlog解析binlog之后,我们可以发现对我们 有用的信息都是以###开头 ,通过正则表达式匹配,我们就可以得到。 在日志中有
在上篇《利用MySQL日志模拟恢复数据变化轨迹》中,我们已经介绍了我们方案的大致思路,其原理就是某网友提到的捞日志方式。
通过mysqlbinlog解析binlog之后,我们可以发现对我们有用的信息都是以###开头,通过正则表达式匹配,我们就可以得到。
在日志中有@1,@2等字符, 这是代表表结构的字段名,即@1表示表中第一个字段,@2表示表中第二个字段等。然后我们通过查询INFORMATION_SCHEMA.COLUMNS表可以找到表的所有字段,然后一一替换掉即可。对于INSERT 和 DELETE两种操作,其数据行只有一份,而UPDATE有两份数据行。对此,我们需要第二份。
例如:
表结构信息
binlog信息
我们需要将其翻译成完全可执行的sql:insert into a (id, num) values (1, 199);
在截取字段值时,我们遇到一下几个坑:
1、字段值为日期类型。在日志中保存的格式为 @1=2012-12-04 13:14:35,此时,必须将2012-12-04 13:14:35加上引号。
2、负数。负数在日志中保存的格式为 @1=-1 (4294967295), 此时,我们其实只需要‘-1’即可。
3、转义字符集。日志当中显示的字段值信息与数据库中字段值信息一致,但是mysql在插入数据库是做了转义,导致日志中的内容不能马上截取直接使用。对此,我们修改了mysqlbinlog这个工具,让其解析出来的文本是未被转义的。例举几个:
root@test 09:50:58>insert into tx values(‘a\’b');
root@test 09:56:47>insert into tx values(‘a\tb’);
root@test 10:06:01>insert into tx values(‘a\bb’);
root@test 10:06:04>insert into tx values(‘a\0b’);
——————————————————-
原版
### INSERT INTO test.tx
### SET
### @1=’a'b’
### INSERT INTO test.tx
### SET
### @1=’a\x09b’
### INSERT INTO test.tx
### SET
### @1=’a\x08b’
### INSERT INTO test.tx
### SET
### @1=’a\x00b’
——————————————————-
修改版
### INSERT INTO test.tx
### SET
### @1=’a\’b’
### INSERT INTO test.tx
### SET
### @1=’a\tb’
### INSERT INTO test.tx
### SET
### @1=’a\bb’
### INSERT INTO test.tx
### SET
### @1=’a\0b’
具体内容可以参照:https://bugs.launchpad.net/percona-server/+bug/949965
4、双字节字符问题。
碰到一次解析binlog得到
·······
### @1=’休闲女鞋白黄粉黒’
······
解析出来的sql是insert into a values (‘黒\’),插回到数据库报错。原因是多了一个转义字符,在回去查看binlog时,并没有这个多余的转义字符‘\’。仔细看后,发现这并不是那个‘黑’,前者的十六进制是(FC5C),后者的十六进制是(BADA)。查看ASCII码表后得知,5C对应的字符是‘\’,而在第三类问题上已经对转义字符集修改,导致在解析出来的时候变成了 @1=’黒\’。
5、未发现问题。这个就需要我们大家更多的测试实践才能发现了。
PS:稍后献上已经修改过的mysqlbinlog以及工具脚本的代码。
原文地址:利用MySQL日志模拟恢复数据变化轨迹II, 感谢原作者分享。

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.
