Home Database Mysql Tutorial MySQL使用二进制日志来恢复数据_MySQL

MySQL使用二进制日志来恢复数据_MySQL

Jun 01, 2016 pm 01:52 PM
binary tool

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

Video Face Swap

Video Face Swap

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

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
What kind of software is a digital currency app? Top 10 Apps for Digital Currencies in the World What kind of software is a digital currency app? Top 10 Apps for Digital Currencies in the World Apr 30, 2025 pm 07:06 PM

With the popularization and development of digital currency, more and more people are beginning to pay attention to and use digital currency apps. These applications provide users with a convenient way to manage and trade digital assets. So, what kind of software is a digital currency app? Let us have an in-depth understanding and take stock of the top ten digital currency apps in the world.

Quantitative Exchange Ranking 2025 Top 10 Recommendations for Digital Currency Quantitative Trading APPs Quantitative Exchange Ranking 2025 Top 10 Recommendations for Digital Currency Quantitative Trading APPs Apr 30, 2025 pm 07:24 PM

The built-in quantization tools on the exchange include: 1. Binance: Provides Binance Futures quantitative module, low handling fees, and supports AI-assisted transactions. 2. OKX (Ouyi): Supports multi-account management and intelligent order routing, and provides institutional-level risk control. The independent quantitative strategy platforms include: 3. 3Commas: drag-and-drop strategy generator, suitable for multi-platform hedging arbitrage. 4. Quadency: Professional-level algorithm strategy library, supporting customized risk thresholds. 5. Pionex: Built-in 16 preset strategy, low transaction fee. Vertical domain tools include: 6. Cryptohopper: cloud-based quantitative platform, supporting 150 technical indicators. 7. Bitsgap:

Is the digital currency app formal? Top 10 formal and legal virtual currency trading apps in the world Is the digital currency app formal? Top 10 formal and legal virtual currency trading apps in the world Apr 30, 2025 pm 07:09 PM

Recommended cryptocurrency trading platforms include: 1. Binance: the world's largest trading volume, supports 1,400 currencies, FCA and MAS certification. 2. OKX: Strong technical strength, supports 400 currencies, approved by the Hong Kong Securities Regulatory Commission. 3. Coinbase: The largest compliance platform in the United States, suitable for beginners, SEC and FinCEN supervision. 4. Kraken: a veteran European brand, ISO 27001 certified, holds a US MSB and UK FCA license. 5. Gate.io: The most complete currency (800), low transaction fees, and obtained a license from multiple countries. 6. Huobi Global: an old platform that provides a variety of services, and holds Japanese FSA and Hong Kong TCSP licenses. 7. KuCoin

Ranking of the top ten formal virtual currency exchange apps in 2025 (digital currency trading platform app list) Ranking of the top ten formal virtual currency exchange apps in 2025 (digital currency trading platform app list) Apr 30, 2025 pm 05:39 PM

Ranking of the top ten formal virtual currency exchange apps in 2025: 1. OKX, 2. Binance, 3. Huobi, 4. Coinbase, 5. Kraken, 6. Bitfinex, 7. KuCoin, 8. Gemini, 9. Bitstamp, 10. Poloniex, each performs outstandingly in terms of security, user experience, transaction fees, liquidity, currency richness, professional tools, compliance, privacy protection, leveraged trading, degree of internationalization, customer service, etc.

How to download the Hong Kong Digital Currency Exchange app? The top ten digital currency exchange apps are included How to download the Hong Kong Digital Currency Exchange app? The top ten digital currency exchange apps are included Apr 30, 2025 pm 07:12 PM

The methods to download the Hong Kong Digital Currency Exchange APP include: 1. Select a compliant platform, such as OSL, HashKey or Binance HK, etc.; 2. Download through official channels, iOS users download on the App Store, Android users download through Google Play or official website; 3. Register and verify their identity, use Hong Kong mobile phone number or email address to upload identity and address certificates; 4. Set security measures, enable two-factor authentication and regularly check account activities.

Is there a future for digital currency apps? Apple mobile digital currency trading platform app download TOP10 Is there a future for digital currency apps? Apple mobile digital currency trading platform app download TOP10 Apr 30, 2025 pm 07:00 PM

The prospects of digital currency apps are broad, which are specifically reflected in: 1. Technology innovation-driven function upgrades, improving user experience through the integration of DeFi and NFT and AI and big data applications; 2. Regulatory compliance trends, global framework improvements and stricter requirements for AML and KYC; 3. Function diversification and service expansion, integrating lending, financial management and other services and optimizing user experience; 4. User base and global expansion, and the user scale is expected to exceed 1 billion in 2025.

What is the core difference between Huobi's previous Huoxin and OKX's current pay? What is the core difference between Huobi's previous Huoxin and OKX's current pay? Apr 30, 2025 pm 06:33 PM

Neither Huoxin nor OKX Pay directly supports fiat currency payment. Huoxin is mainly used for digital asset management and transactions, and users need to exchange fiat currency through the Huobi Exchange; OKX Pay focuses on digital asset payment and transfer, and users need to exchange fiat currency through the OKX platform.

What are the three giants in the currency circle? Top 10 Recommended Virtual Currency Main Exchange APPs What are the three giants in the currency circle? Top 10 Recommended Virtual Currency Main Exchange APPs Apr 30, 2025 pm 06:27 PM

In the currency circle, the so-called Big Three usually refers to the three most influential and widely used cryptocurrencies. These cryptocurrencies have a significant role in the market and have performed well in terms of transaction volume and market capitalization. At the same time, the mainstream virtual currency exchange APP is also an important tool for investors and traders to conduct cryptocurrency trading. This article will introduce in detail the three giants in the currency circle and the top ten mainstream virtual currency exchange APPs recommended.

See all articles