MySQL log_bing restores delete/update/drop data

First open the log_bin setting of MySQL and modify the mysql configuration file

#vim /etc/my.cnf

Check if there is log-bin=mysql-bin setting under [mysqld], if not, add this setting

restart mysql

Test whether log-bin is enabled, create a new data table in the database, and then go to the mysql file directory to find whether the mysql-bin.00000X file exists

If there is a description that the configuration takes effect

Let's start with an example

Create a new testtable table, add two fields of id and name, insert 3 pieces of information, and then use delete from testtable; delete the table content

Back to the console:

#mysqlbinlog mysql-bin.00001 > 1.sql

Generate 1.sql file, this file is to record our database operation information, view the sql file

#vim 1.sql

The contents inside are:

# at 21302
# at 21334
#180326 16:11:27 server id 1  end_log_pos 21334 CRC32 0xc4b3a95b        Intvar
SET INSERT_ID=1558/*!*/;
#180326 16:11:27 server id 1  end_log_pos 21509 CRC32 0xd8e0b556        Query   thread_id=21    exec_time=0     error_code=0
SET TIMESTAMP=1522051887/*!*/;
insert into XSJL (SKTNO,JYH,SP_ID,HTH,XSJE,ZKJE,XSSL) values ('1238',218440,818937,1290300,704,176,1)

at refers to the position in the description

180326 16:11:27 is the time record

insert into xsjl()... records the sql statement you execute,

We generally use time stamping to restore data

Find the time record value you want to roll back and roll it back

#mysqlbinlog mysql-bin.000001 --start-datetime='2018-03-26 15:39:50' --stop-datetime='2018-03-26 15:40:39' | mysql -uroot -p

--start-datetime start rollback date marker start position --stop-datetime antisense

It ends here.

This is a simple rollback, because our log-bin records the operation records of all tables in the database, so the update information of the data table is interleaved and complicated. In our actual production

If you want to restore a deleted table, it is quite troublesome, because after we get the sql file of log-bin, we need to record the operation of this table through a series of methods.

Only after filtering can be recovered, otherwise we will directly recover the data of other tables.

Here is a little suggestion, it is best to use another method mentioned in my blog for database backup

https://mp.csdn.net/postedit/79695669

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326460663&siteId=291194637