MySQL Technical Chapter-mysql database under linux uses binlog file for data rollback example demonstration, two ways to restore the database by binlog

Chapter 1: Using binlog for data rollback

① Check the location of the binlog file

First check the location of the binlog file through the following statement .

show variables like '%log_bin%';

You can see that the location is /usr/local/mysql-8.0.11/data/
Insert picture description here

② View the binlog file name of the data being stored in the master data

View show master status
Insert picture description here
can locate the stored data is currently being binlog.000025
Insert picture description here

③ View the contents of the binlog file on the console

View the binlog file through the mysqlbinlog tool under bin , and you can see what is recorded.

[root@localhost bin]# "/usr/local/mysql-8.0.11/bin/mysqlbinlog" --no-defaults  "/usr/local/mysql-8.0.11/data/binlog.000025"

④ Rollback method 1: Export sql statement through time

Take a look at the time of the accidental deletion.
take a look
Exporting SQL statements through intermediate time points , there is now a certain time difference, because the local time and server time are not synchronized, the next section explains how to calculate the time difference.

[root@localhost bin]# "/usr/local/mysql-8.0.11/bin/mysqlbinlog" --no-defaults  --start-datetime="2020.08.13 18:50:00" --stop-datetime="2020.08.13 18:55:00" "/usr/local/mysql-8.0.11/data/binlog.000025" > 0813.sql

Insert picture description here

⑤ Note: Compare the server and local time to calculate the time difference to avoid time interception errors

Check the server time.

[root@localhost bin]# date
Thu Aug 13 07:44:36 EDT 2020

Check the local time, you can see that my local time is about 8 minutes faster, and when you intercept the log, you need to look at the content 8 minutes in advance .
Insert picture description here

⑥ Rollback method 2: Export sql statement through location

First look at the location information between the two recovery points.
The above is intercepted at two time points, this one is intercepted at two locations.
Insert picture description here
Insert picture description here
Export the database by specifying the location.

[root@localhost mysql-8.0.11]# "/usr/local/mysql-8.0.11/bin/mysqlbinlog" --no-defaults  --start-position=204590 -d ncc_0807mysql --stop-position=252554 "/usr/local/mysql-8.0.11/data/binlog.000025" > 0815.sql

⑦ sql file recovery method one: recover the database through the source method

Log in to the database, switch the database, and restore the database.

[root@localhost ~]# "/usr/local/mysql-8.0.11/bin/mysql" -uroot -p123456 -A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 41
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| ncc_0807auto       |
| ncc_0807mysql      |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.01 sec)

mysql> use ncc_0807mysql;
Database changed
mysql> source /usr/local/mysql-8.0.11/bin/0813.sql;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

⑧ Skill: Filter and export the SQL scripts related to the specified library

Specify the database: -d database name, or -database= database name.

[root@localhost mysql-8.0.11]# "/usr/local/mysql-8.0.11/bin/mysqlbinlog" --no-defaults  --start-datetime="2020.08.13 06:50:00" -d ncc_0807mysql --stop-datetime="2020.08.13 06:55:00" "/usr/local/mysql-8.0.11/data/binlog.000025" > 0814.sql

⑨ sql file recovery method two: import the recovery database

Import the recovery database.

[root@localhost mysql-8.0.11]# "/usr/local/mysql-8.0.11/bin/mysql" -uroot -p123456 ncc_0807mysql < "/usr/local/mysql-8.0.11/0814.sql"

⑩ Skill: Binlog direct recovery method

Location of the direct binlog recovery library method.
Recover directly without transfer.

[root@localhost mysql-8.0.11]# "/usr/local/mysql-8.0.11/bin/mysqlbinlog" --no-defaults  --start-position=204590 --stop-position=252554  "/usr/local/mysql-8.0.11/data/binlog.000025" | "/usr/local/mysql-8.0.11/bin/mysql" -uroot -p123456

Like it if you like it ❤!

Guess you like

Origin blog.csdn.net/qq_38161040/article/details/107989565