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

Example code for text origami effect using CSS3

Preface This article mainly shares with you an ex...

Detailed explanation of several ways to create a top-left triangle in CSS

Today we will introduce several ways to use CSS t...

Web Design Tutorial (7): Improving Web Design Efficiency

<br />Previous article: Web Design Tutorial ...

Newbies quickly learn the steps to create website icons

<br />Original URL: http://www.lxdong.com/po...

CocosCreator Getting Started Tutorial: Making Your First Game with TS

Table of contents premise TypeScript vs JavaScrip...

Solve the compatibility issue between MySQL 8.0 driver and Alibaba Druid version

This article mainly introduces the solution to th...

Three ways to achieve background blur in CSS3 (summary)

1. Normal background blur Code: <Style> htm...

In-depth understanding of slot-scope in Vue (suitable for beginners)

There are already many articles about slot-scope ...

HTML tutorial, easy to learn HTML language (2)

*******************Introduction to HTML language (...

MySQL restores data through binlog

Table of contents mysql log files binlog Binlog l...

...