Data recovery operation using MySQL binlog


This article is based on the operating environment under centos7, if necessary, please read this article installation

MySQL8.0.19 install blog: MySQL8.0.19

A, binlog Profile

binlog: binlog is a binary format file that records all changes in the database, and stored in binary form on disk; it can be used to view the change history database, database incremental backup and recovery, Mysql replication ( from the master copy of the database).

Two, binlog format

1)Statement

Each sql will modify the data recorded in the binlog.

Advantages : the variation of each row need not be recorded, reducing the amount of log binlog, the IO savings, improved performance.

Drawback : Since only execute the statement, these statements in order to run properly on the slave, each statement must also record some information in the course of implementation, in order to ensure that all statements get executed in the master and slave in the end, when the same record the result of. In addition mysql replication, as some specific functions function, slave and the master can be consistent there will be many related issues.

Summary:
of Statement Level advantages:
1, to solve the shortcomings row level, and do not need to record changes in each row.
2, log less, saving the IO, the application log block from the library.
Statement level Cons: There may be some new features synchronized obstacles, such as functions, triggers, and so on.

2)Row

5.1.5 version of MySQL supports replication began to row level, it does not record the sql statement contextually relevant information, which saves only the modified records.

Advantages : context-sensitive information can not be recorded in the binlog sql statement is executed, only need to record that a record is modified into anything. So log content rowlevel record will be very clear details of each line of data modifications under. And function, as well as problems trigger the call and triggers can not be replicated stored procedure does not appear correctly in certain specific circumstances, or.

Drawback : All statements executed when logged when all will modify each line to record the record, this may generate a lot of log content.

Summary: The
advantages of the row level:
1, detailed record
2, solve the problem statement level copy mode can not be solved.
the row level disadvantages: large logs, as is line to split.

3)Mixed

From the beginning of version 5.1.8, MySQL provides Mixed format, in fact, combined with the Statement of Row.

In Mixed mode, the general statement modified using the statment format for saving the binlog, such as some functions, statement can not be completed master-slave operation replication, is used row format stored binlog, MySQL will be treated separately recorded according to each specific sql statement executed log form, that is, between the Statement and select a Row.

Third, configure and view binlog format

1) modify and configure the MySQL binlog log file format

# 使用命令
[root@ chenc01 ~]# vim /etc/my.cnf
# 打开配置文件,并在[mysqld]下面增加以下内容
log-bin = mysql-bin
binlog_format="ROW"

Restart the database, it will generate a new file /var/lib/mysql/mysql-bin.000001, if before the mysql-bin.000001 deleted, the file will increase in the current index basis.

# 使用命令重启数据库
[root@ chenc01 ~]# service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
# 使用命令 ls /var/lib/mysql  查看binlog文件
[root@ chenc01 ~]# ls /var/lib/mysql
ibdata1      ib_logfile1  mysql-bin.000001  mysql.sock
ib_logfile0  mysql        mysql-bin.index   test

2) View binlog format

mysql> show variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)

mysql> 

Fourth, create a database and backup data

1) Create a database

mysql> create database demo;
Query OK, 1 row affected (0.11 sec)

mysql> use demo;
Database changed
mysql> create table user (
    -> id int(10) not null auto_increment,
    -> name varchar(32) not null,
    -> type int(10) not null,
    -> primary key(id)
    -> ) ENGINE=innoDB;
Query OK, 0 rows affected (0.05 sec)

mysql> 

2) created, refresh binlog file

mysql> flush logs;
Query OK, 0 rows affected (0.11 sec)

mysql> 

3) Back up the database

使用命令 mysqldump -u root -p demo user > db_demo_bak.sql
或者 mysqldump -u root -p --databases demo > db_demo_bak.sql
[root@ chenc01 ~]# mysqldump -u root -p demo user > db_demo_bak.sql
Enter password: 

Fifth, use the backup and restore the database using binlog to complete rollback

1) inserted into the database and the database

mysql> use demo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into user(id,name,type) value (10001,"A",1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into user(id,name,type) value (10002,"B",1);
Query OK, 1 row affected (0.00 sec)

mysql> 

2) View data

mysql> select * from user;
+-------+------+------+
| id    | name | type |
+-------+------+------+
| 10001 | A    |    1 |
| 10002 | B    |    1 |
+-------+------+------+
2 rows in set (0.00 sec)

mysql> 

3) update the database to modify, simulate the operation failed

mysql> update user set name="C";
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> select * from user;
+-------+------+------+
| id    | name | type |
+-------+------+------+
| 10001 | C    |    1 |
| 10002 | C    |    1 |
+-------+------+------+
2 rows in set (0.00 sec)

mysql> 
# user表的name字段被误操作修改,抓紧刷新掉binlog文件
mysql> flush logs;
Query OK, 0 rows affected (0.10 sec)

mysql> 

4) look at the mysql binlog file

[root@ chenc01 ~]# ls /var/lib/mysql
demo         ib_logfile1       mysql-bin.000002  mysql.sock
ibdata1      mysql             mysql-bin.000003  test
ib_logfile0  mysql-bin.000001  mysql-bin.index

5) delete the table, use a database backup and recovery

mysql> use demo;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> drop table user;
Query OK, 0 rows affected (0.01 sec)

mysql> source /root/db_demo_bak.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
....................................
mysql> select * from user;
Empty set (0.00 sec)

mysql> 
# 此时数据库表就备份好了,但是没有数据。
mysql> show binlog events in"mysql-bin.000002";
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                  |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| mysql-bin.000002 |   4 | Format_desc |         1 |         106 | Server ver: 5.1.73-log, Binlog ver: 4 |
| mysql-bin.000002 | 106 | Query       |         1 |         174 | BEGIN                                 |
| mysql-bin.000002 | 174 | Table_map   |         1 |         221 | table_id: 15 (demo.user)              |

[root@ chenc01 ~]# mysqlbinlog --start-position=4 --stop-position=1037 --database=demo /var/lib/mysql/mysql-bin.000004 | /var/lib/mysql -u root -p TestBicon@123 -v demo
# 然后我们来检查一下user表
mysql> select * from user;
+-------+------+------+
| id    | name | type |
+-------+------+------+
| 10001 | A    |    1 |
| 10002 | B    |    1 |
+-------+------+------+
2 rows in set (0.00 sec)
Published 60 original articles · won praise 58 · views 10000 +

Guess you like

Origin blog.csdn.net/chen_jimo_c/article/details/104906452