mysql recover data from binlog

First, there must be an available mysql, installation procedures here is not the table, and before the article has an introduction installation.

1. Turn on binlog.

To be able to recover from the binlog, the first to open the record binlog:

cat /etc/my.cnf

Add the following lines:

= mysql- bin-log bin # indicate on binlog, and binlog physical files in /var/lib/mysql/mysql-bin.000000x, in the same directory as well as a mysql-bin.index file
 sync_binlog = 1  # every thing submitted regarded binlog written to disk, this is very important, if not open, call the shots from the time replication of data from the master may cause inconsistencies
 open xa distributed transaction support innodb_support_xa = 1 #, but also through xa internal affairs and ensure binlog the actual data is consistent 
binlog_format = ROW #binlog format ROW, three options: a STATEMENT, ROW, MIXED 
Server-the above mentioned id = 7 # must write this, configure only the log-bin without configuring this one, mysql service will vary error but will not start, reportedly written recommendation server IP last

After modifying well, restart the mysql service:

systemctl restart mysqld.service

Well, now opens the binlog, you can find the following files in / var / lib / mysql:

 

First log into the mysql command line (mysql -uroot -pxxxxxx), look at the status:

You can see the current using mysql-bin.000001 this binlog files to record the current position is 154, I observed in the file for each binlog, when there is little operation, the position will be 154.

You can also look at the binlog related to several variables:

 

2. Testing data recovery

Add some test data below:

create database testbindb;
use testbindb;
create table test_order(id int(10), primary key(id) ) engine=innodb;
insert into test_order values(1);
insert into test_order values(2);
insert into test_order values(3);
insert into test_order values(4);
insert into test_order values(5);
insert into test_order values(6);
insert into test_order values(7);

Bin look at the table:

 

 Then delete this table:

drop table test_order;

Then binlog compulsory written and reopen a binlog file:

flush logs;

Look at a table, he was gone:

 

Look at a state:

Can be seen already in use the second binlog files, equivalent to the previous build tables, add test data, delete tables and other operations are recorded in the mysql-bin.000001 it.

Binlog a look at the contents of the documents:

Suppose we delete the operating table is misuse, so we hope the data can be restored to the state before the end of a delete operation.

Use mysqlbinlog order to achieve the objective:

mysqlbinlog /var/lib/mysql/mysql-bin.000001 --start-position 154 --stop-position 2327 | mysql -uroot -p

Represented here should operate between mysql-bin.000001 file recovery position 154-2327 There are two places to note:

  1. binlog file location using the absolute path, otherwise it will say can not find the file, which can not be recovered
  2. Followed by the of | mysql -uroot -p can not be omitted, I personally think it's time to restore the need to log in, you can understand

After the recovery is complete, just deleted that table on the back:

mysqlbinlog command is said to have the following parameters are available: 

  1. --start-datetime: reads the specified binary log is equal to or later than the timestamp of the local computer
  2. --stop-datetime: binary log read from the specified time is less than or equal to the time stamp value and said local computer as
  3. --start-position: Specifies the position to read the event as the start position from the binary log.
  4. --stop-position: Specifies the position to read binary log events from the position as the event ended

Guess you like

Origin www.cnblogs.com/lihan829/p/11408061.html