Mysql security settings-binlog to restore database data

View the list of all binlog logs:

 show master logs;
 +------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 149 |
| mysql-bin.000002 | 4102 |
+------------------+-----------+
2 rows in set (0.00 sec)

View the latest binlog

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 4102 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

View the location of the log

 show variables like 'log_%';
mysql>  show variables like 'log_%';
+----------------------------------------+----------------------------------------+
| Variable_name                          | Value                                  |
+----------------------------------------+----------------------------------------+
| log_bin                                | ON                                     |
| log_bin_basename                       | /var/lib/mysql/binlog                  |
| log_bin_index                          | /var/lib/mysql/binlog.index            |
| log_bin_trust_function_creators        | OFF                                    |
| log_bin_use_v1_row_events              | OFF                                    |
| log_error                              | /var/log/mysqld.log                    |
| log_error_services                     | log_filter_internal; log_sink_internal |
| log_error_suppression_list             |                                        |
| log_error_verbosity                    | 2                                      |
| log_output                             | FILE                                   |
| log_queries_not_using_indexes          | OFF                                    |
| log_raw                                | OFF                                    |
| log_slave_updates                      | ON                                     |
| log_slow_admin_statements              | OFF                                    |
| log_slow_extra                         | OFF                                    |
| log_slow_slave_statements              | OFF                                    |
| log_statements_unsafe_for_binlog       | ON                                     |
| log_throttle_queries_not_using_indexes | 0                                      |
| log_timestamps                         | UTC                                    |
+----------------------------------------+----------------------------------------+19 rows in set (0.11 sec)

Simulate database data loss to recover data:

Create a new database test 1 Create a new data table test1 in the library and insert several pieces of data

create database test1 CHARSET utf8;
CREATE TABLE test1(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20) NOT NULL)ENGINE=INNODB CHARSET=utf8;
insert into test1(id,name)values(1,'andy');
insert into test1(id,name)values(2,'kevin');
insert into test1(id,name)values(3,'peter');

Delete database

 drop database test1;
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| blog               |
| goodssystem        |
| hxpurchase         |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
7 rows in set (0.00 sec)

Use binlog to restore the log:

View the binlog log, filter the create database' string, and display the context 5 lines

 mysqlbinlog --base64-output=decode-rows -vvv /var/lib/mysql/binlog.000010|grep 'create database' -C 5
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 35484306
#201107 15:36:45 server id 1  end_log_pos 35484430 CRC32 0xbc93546e 	Query	thread_id=1358	exec_time=0	error_code=0	Xid = 313396
SET TIMESTAMP=1604734605/*!*/;
/*!80016 SET @@session.default_table_encryption=0*//*!*/;
create database test1 CHARSET utf8
/*!*/;

You can see that the start position number of the create database test1 CHARSET utf8 command is 35484306,

#### Then filter the drop database statement, and display the context 5 lines

 mysqlbinlog --base64-output=decode-rows -vvv /var/lib/mysql/binlog.000010|grep 'drop database' -C 5
/*!80014 SET @@session.immediate_server_version=80022*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 35485840
#201107 15:40:50 server id 1  end_log_pos 35485947 CRC32 0x75bb6265 	Query	thread_id=1358	exec_time=0	error_code=0	Xid = 313437
SET TIMESTAMP=1604734850/*!*/;
drop database test1
/*!*/;

You can see that the position number executed by the drop database test is 35485840, then the position number at the end of our interception can be set to 35485839, so that only all logs before the drop statement are intercepted

Start position 35484306, end position 35485839, intercept the log of the specified database test1 and save it as a .sql file

mysqlbinlog --start-position=35484306 --stop-position=35485839 -d test1 /var/lib/mysql/binlog.000010 > /home/binlog_test.sql 

Enter mysql, use source to restore data

source /home/binlog_test.sql;

mysql>  source /home/binlog_test.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.00 sec)

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.00 sec)

Query OK, 0 rows affected (0.00 sec)

Charset changed
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, 1 warning (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected, 1 warning (0.01 sec)

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.00 sec)

Database changed
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.02 sec)

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.00 sec)

Query OK, 0 rows affected (0.01 sec)

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.00 sec)

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.00 sec)

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.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)

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.00 sec)

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.00 sec)

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.00 sec)

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.00 sec)

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.00 sec)

Query OK, 0 rows affected (0.00 sec)

View Results

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| blog               |
| goodssystem        |
| hxpurchase         |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
8 rows in set (0.01 sec)

mysql> use test1;
use test1;
^C
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| test1           |
+-----------------+
1 row in set (0.00 sec)

mysql> select * from test1;
+----+-------+
| id | NAME  |
+----+-------+
|  1 | andy  |
|  2 | kevin |
|  3 | peter |
+----+-------+
3 rows in set (0.01 sec)

Guess you like

Origin blog.csdn.net/qq_43549291/article/details/109678396