Use of all equipment + binlog log recovery database

1.binlog log types

  • Statement only records sql statement execution, small disk footprint, but the recovery time is easy to go wrong. InodeDB not use the Statement.
  • Row record specific data modified, more disk footprint
  • Mixed mixing the above two, mysqlsql depending on circumstances, to select a better solution

View the current types:
Show the Variables like 'binlog_format'

/etc/my.cnf inside binlog_format parameters

2. recovery mode

Preparation of Full recovery portion binlog + log

3. Recovery Methods

1. Full backup

mysqldump -uroot -pfjselinfgsiengiseg test3 > /data1/test3.sql. 

2. Perform operation

SQL1:update student set age=21 where id=4;
SQL2:update student set age=22 where id=5;

Then execute the two sql, it corresponds to the two transactions.
After if we want to restore the database state to execute SQL1

3. Refresh log

To refresh the log, we can view the log, find the need to restore the binlog

carried out:

flush logs;

4. Find the latest log

Found binlog log: /var/lib/mysql/mysql-bin.000031

5. Analysis of Logs

mysql execute:

show binlog events in "mysql-bin.000031"
  • Log_name log file name
  • Starting position Pos events
  • Event_type event type
  • Server_id server ID, and the relationship between the primary counter from server_id
  • End position End_log_pos events
  • Info Event Information

Find just twice affairs

|

 mysql-bin.000031 | 6622  | Query        | 1           | 6691          | BEGIN                                                                       |
| mysql-bin.000031 | 6691  | Table_map    | 1           | 6745          | table_id: 308 (test3.student)                                               |
| mysql-bin.000031 | 6745  | Update_rows  | 1           | 6801          | table_id: 308 flags: STMT_END_F                                             |
| mysql-bin.000031 | 6801  | Xid          | 1           | 6828          | COMMIT /* xid=6771600 */                                                    |
| mysql-bin.000031 | 6828  | Query        | 1           | 6897          | BEGIN                                                                       |
| mysql-bin.000031 | 6897  | Table_map    | 1           | 6951          | table_id: 308 (test3.student)                                               |
| mysql-bin.000031 | 6951  | Update_rows  | 1           | 7007          | table_id: 308 flags: STMT_END_F                                             |
| mysql-bin.000031 | 7007  | Xid          | 1           | 7034          | COMMIT /* xid=6771607 */                                                    |
| mysql-bin.000031 | 7034  | Rotate       | 1           | 7077          | mysql-bin.000032;pos=4                                                      |
+------------------+-------+--------------+-------------+---------------+-----------------------------------------------------------------------------+

BEGIN is the beginning of a transaction
COMMIT is to commit the transaction

So we need to restore a 6622-6828

6. Re-building a database

The old database test3 renamed, or deleted. Recommended that you back

drop database test3;

Create a database test3

create database test3;

Import full backup

source /data1/test3.sql;

Restore some binlog log

mysqlbinlog --start-position=6622     --stop-position=6828   --database=test3  /var/lib/mysql/mysql-bin.000031   |mysql -uroot -pfjselinfgsiengiseg  -D test3
  • start-position starting position
  • stop-position end position
  • test3 get only log database library
  • /var/lib/mysql/mysql-bin.000031 binlog log path

mysqlbinlog binlog command will log converter corresponding to sql statement, lead out, and then performed by the sql statement mysql command
data before recovery:

mysql root@localhost:test3> select * from student;
+------+--------+-------+---------+
| id   | name   | age   | class   |
|------+--------+-------+---------|
| 4    | 2      | 20    | B       |
| 5    | 2      | 21    | B       |
+------+--------+-------+---------+

Data recovery

mysql root@localhost:test3> select * from student;
+------+--------+-------+---------+
| id   | name   | age   | class   |
|------+--------+-------+---------|
| 4    | 2      | 21    | B       |
| 5    | 2      | 21    | B       |
+------+--------+-------+---------+

We can see, SQL1 performed, SQL2 not performed.

other

  1. All equipment regularly, otherwise the recovery time needed to recover a large amount of binlog log, error-prone
  2. Pre-operational recovery, make a backup
  3. In addition to screening binlog log the position, time can also be used
  4. Some need to restore files across multiple binlog

Without permission, please do not reprint

Guess you like

Origin www.cnblogs.com/Xjng/p/11401850.html