MySQL——Recover data through binlog

Table of contents

1.Basic concepts of binlog

2.MySQL turns on binlog

3. Use binlog logs to restore data

3.1. Preparations before recovery

3.2.Data recovery

3.2.1. Convert binlog to sql through mysqlbinlog to facilitate querying the specific location

3.2.2. View the generated backuptmp.sql and finally determine that the starting position to be restored is 219 and the ending position is 982

3.2.3. Perform recovery operations through mysqlbinlog


1.Basic concepts of binlog

        The binary log records all operations that perform changes to the MySQL database in the form of events.
        Binlog is a binary log that records all database table structure changes (such as CREATE, ALTER TABLE, DROP, etc.) and table data modifications (INSERT, UPDATE, DELETE, TRUNCATE, etc.). Operations such as SELECT and SHOW will not be recorded because such operations do not modify the data itself, but you can view all statements executed by MySQL by querying the general log.


Binlog has two common usage scenarios:

2.MySQL turns on binlog

        After MySQL is installed, binlog is not enabled by default in version MySQL 5.7, and binlog is enabled by default in MySQL 8. After logging in to MySQL, check the binlog status sql as follows:

show variables like '%log_bin%';

If the binlog log is not enabled, follow the steps below to enable the binlog log.

 Enable binlog log

Modify the MySQL configuration file. The configuration file in Linux is my.conf. In Windows, ask my.ini. The following uses centos as an example for demonstration.

  • Edit configuration file
vim /etc/my.cnf
  • Add configuration items
log-bin=mysql-bin
server-id=1
  • Restart the MySQL service
systemctl restart mysqld
  • Enter MySQL to check whether the binlog log is successfully opened.

If log_bin is ON, it means that this parameter is turned on, which means that the system records the bin log.

log_bin_basename configures the file path and file prefix of bin log

log_bin_index configures the path of the bin log index file

  • View log list
show master logs;

  • View the binlog specific file according to the path of log_bin_basename

 3. Use binlog logs to restore data

Principle : When the database changes, the binlog will record all changes in the database; when recovery is needed, the original part of the operation can be restored based on the start position and end position in the binlog; the end position is generally the position before the data is destroyed or deleted.

 3.1. Preparations before recovery

After turning on binlog, create a test database, create a test table in the test database, and write data:

create database test;
use test;
CREATE TABLE `testuser` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `age` decimal(18,2) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

insert  into `testuser`(`id`,`name`,`age`) values (1,'张三',24.00);

 At this time, if the test database is accidentally deleted, you can use binlog to reply to the database, the tables in the database, and the data in the tables;

drop database test;

3.2.Data recovery

When the data needs to be restored, in order to prevent the latest business from being affected after the data is restored, flush logs needs to be executed to generate a new binlog file. At this time, the old binlog file will no longer be written;

The following specifically uses mysql-bin.000001 to perform data recovery. 

When restoring, you need to find two locations in the binlog:

  • Starting point for data recovery
  • Ending point of data recovery

For example, during the drop operation in data preparation, you need to find the location in the binlog and use this location as the end location of data recovery.

3.2.1. Convert binlog to sql through mysqlbinlog to facilitate querying the specific location

mysqlbinlog --set-charset=utf-8 /var/lib/mysql/mysql-bin.000001>backuptmp.sql

3.2.2. View the generated backuptmp.sql and finally determine that the starting position to be restored is 219 and the ending position is 982

 

3.2.3. Perform recovery operations through mysqlbinlog

mysqlbinlog -v /var/lib/mysql/mysql-bin.000001 --start-position=219 --stop-position=982 | mysql -uroot -p123456

/var/lib/mysql/mysql-bin.000001 To operate the binlog file

--start-position=219 starting position of data recovery

--stop-position=982 end position of data recovery

mysql -uroot -p123456 Data recovery requires logging into the database

Guess you like

Origin blog.csdn.net/DreamEhome/article/details/130010601