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

Tips for optimizing MySQL SQL statements

When faced with a SQL statement that is not optim...

JavaScript flow control (loop)

Table of contents 1. for loop 2. Double for loop ...

How to install Nginx in CentOS7 and configure automatic startup

1. Download the installation package from the off...

5 MySQL GUI tools recommended to help you with database management

There are many database management tools for MySQ...

Detailed explanation of how to connect Java to Mysql version 8.0.18

Regarding the connection method between Java and ...

HTML table markup tutorial (22): row border color attribute BORDERCOLORLIGHT

Within rows, light border colors can be defined i...

Detailed process of using nginx to build a webdav file server in Ubuntu

Install nginx Note that you must install nginx-fu...

How to use mysql to complete the data generation in excel

Excel is the most commonly used tool for data ana...

Example of how to exit the loop in Array.forEach in js

Table of contents forEach() Method How to jump ou...

CSS syntax for table borders

<br /> CSS syntax for table borders The spec...

MySQL 5.7.12 installation and configuration tutorial under Mac OS 10.11

How to install and configure MySQL on Mac OS 10.1...

How to delete extra kernels in Ubuntu

Step 1: View the current kernel rew $ uname -a Li...