How to use MySQL binlog to restore accidentally deleted databases

How to use MySQL binlog to restore accidentally deleted databases

1 View the current database content and back up the database

View database information:

Back up the database:

[root@localhost ~]# mysqldump -u root -pt > /mnt/t.sql
Enter password: 
[root@localhost ~]# ll /mnt/t.sql 
-rw-r--r-- 1 root root 1771 Aug 25 11:56 /mnt/t.sql

2 Enable bin_log function

First check whether the bin_log function is enabled in the database

mysql> show variables like "%log_bin%";

You need to modify the mysql configuration file, my.cnf in /etc/, and add a line log_bin = mysql_bin

3 Simulate misoperation (insert 3 pieces of data, delete the database)

mysql> insert into t1 values ​​(3);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 values ​​(4);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t1 values ​​(5);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+
|id|
+------+
| 1 |
| 2 |
| 5 |
| 4 |
| 3 |
+------+
5 rows in set (0.00 sec)

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

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql_bin.000003 | 106 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

Deleting data:

mysql> truncate t1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from t1;
Empty set (0.00 sec)

At this time, the database is suddenly damaged or manually deleted

mysql> drop table t1;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
Empty set (0.00 sec)

4 Data Recovery

1 Use the backed up /mnt/t.sql to restore data

mysql> source /mnt/t.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)

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, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0

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)
mysql> show tables;
+-------------+
| Tables_in_t |
+-------------+
| t1 |
+-------------+
1 row in set (0.00 sec)

mysql> select * from t1;
+------+
|id|
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)

2 There are still three pieces of data that have not been restored. What should I do? Can only be restored using bin-log

[root@localhost ~]# mysqlbinlog --no-defaults /var/lib/mysql/mysql_bin.000002 | mysql -u root -p123.com t
mysql> use t;
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> select * from t1;
+------+
|id|
+------+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
+------+
5 rows in set (0.00 sec)

mysql>

5 Conclusion

Backing up your data

mysqldump -uroot -p123456 test -l -F '/tmp/test.sql'
-l: read lock (can only read, cannot update)
-F: flush logs, which can regenerate new log files, including log-bin logs

View binlog log

mysql>show master status;

Back up data before importing

mysql -uroot -pt -v -f </mnt/t.sql
-v shows detailed information about the import -f means that if an error occurs in the middle, you can skip it and continue to execute the following statement

Restore binlog-file binary log file

mysqlbinlog --no-defaults binlog-file | mysql -uroot -pt

Recover from a certain point (367)

mysqlbinlog --no-defaults --stop-position="367" mysql-bin.000001| mysql -uroot -pt

Check that point first, use more to check

[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | more

Then restore

[root@localhost mysql]# /usr/bin/mysqlbinlog --no-defaults mysql-bin.000002 --start-position="794" --stop-position="1055" | /usr/bin/mysql -uroot -pt

Reset binlog

mysql> reset master;
Query OK, 0 rows affected (0.01 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 106 | | |
+------------------+----------+--------------+------------------+
mysql> flush logs;#Close the current binary log file and create a new one. The name of the new binary log file is the number of the current binary file plus 1.

This is the end of this article on how to use MySQL binlog to recover accidentally deleted databases. For more information about MySQL binlog recovery of accidentally deleted databases, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL database recovery (using mysqlbinlog command)
  • Binlog related commands and recovery techniques in MySQL
  • MySQL Binlog Data Recovery: Detailed Explanation of Accidentally Deleting a Database
  • How to use binlog for data recovery in MySQL
  • Teach you to automatically restore the log file (binlog) of the MySQL database
  • Detailed steps to restore MySQL database through binlog files on Linux
  • Explain MySQL's binlog log and how to use binlog log to recover data
  • MySQL uses mysqldump+binlog to completely restore the deleted database principle analysis
  • MySQL uses binlog logs to implement data recovery
  • How to restore data using binlog in mysql5.7

<<:  Detailed explanation of the implementation method and usage of CSS3 border-radius rounded corners

>>:  Detailed explanation of HTML's <input> tag and how to disable it

Recommend

React sample code to implement automatic browser refresh

Table of contents What is front-end routing? How ...

Mysql cannot select non-aggregate columns

1. Introduction I recently upgraded my blog and a...

Vue implements simple slider verification

This article example shares the implementation of...

Vue easily realizes watermark effect

Preface: Use watermark effect in vue project, you...

Summary of CSS counter and content

The content property was introduced as early as C...

Why Google and Facebook don't use Docker

The reason for writing this article is that I wan...

Docker installation tutorial in Linux environment

1. Installation environment Docker supports the f...

How to use nginx to block a specified interface (URL)

1. Introduction Sometimes, after the web platform...

Details on macrotasks and microtasks in JavaScript

Table of contents 1. What are microtasks? 2. What...

Double loading issue when the page contains img src

<br />When the page contains <img src=&qu...

React Router V6 Updates

Table of contents ReactRouterV6 Changes 1. <Sw...

How to implement remote access control in Centos 7.4

1. SSH remote management SSH is a secure channel ...

Share some key interview questions about MySQL index

Preface An index is a data structure that sorts o...