Database series-MySQL binary log to restore database data

MySQL Binary Log, also commonly referred to as bin-log, is a binary log file generated by mysql performing changes. It has two main functions:
* Data recovery
* Master-slave database. It is used to perform additions, deletions and changes on the slave side and keep it synchronized with the master.

1. Turn on the binlog function and basic operations

To use MySQL's binlog log function, you must first enable the function in the MySQL configuration file. The operation is very simple. Find the MySQL configuration file and add a line log_bin = mysql-bin to the file.

(The configuration file is my.ini under mysql installation directory\MySQL Server 8.0, just add log_bin = mysql_bin)

[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录# 切记此处一定要用双斜杠
basedir=D:\\DevPackTool\\mysql-8.0.22-winx64 
# 设置mysql数据库的数据的存放目录
datadir=D:\\DevPackTool\\mysql-8.0.22-winx64\\data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=UTF8MB4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
#开启binlog
log-bin=mysql-bin
server-id=1  
[mysql]
# 设置mysql客户端默认字符集
default-character-set=UTF8MB4

[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=UTF8MB4

It is convenient for everyone to unify the environment. Provide a configuration of your own.

In fact, in various MySQL environments I installed, this feature is usually turned on by default.

After the binlog function is enabled, there will be files such as mysql-bin.000001, mysql-bin.000002, etc. in the MySQL database directory, which are the binary log files of MySQL. A binary log file will be created whenever MySQL is started or manually refreshed.

First of all, in the MySQL  command line, use the show master logs  command to view the existing binlog file.

 

2. Add data to the database

 

3. Refresh the binlog log

Previously, the binlog file of MySQL was

+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 |       179 | No        |
| binlog.000002 |       952 | No        |
| binlog.000003 |       179 | No        |
| binlog.000004 |       179 | No        |
| binlog.000005 |       179 | No        |
| binlog.000006 |       179 | No        |
| binlog.000007 |   2172794 | No        |
| binlog.000008 |    433836 | No        |
| binlog.000009 |      1651 | No        |
+---------------+-----------+-----------+

Now three pieces of data are added to the database. Now we refresh the binlog log, and a new mysql-bin.000010 file will be generated (some default is binlog). Mine is mysql8, as follows:

flush logs;
show master logs;

4. Delete data

Here I delete all the three pieces of data I just added.

Add first

Delete again 

 

5. Binlog log content analysis

MySQL binary log file records MySQL operations, such as the delete operation just now, let's look at the specific content of the log file.

Use MySQL's mysqlbinlog command:

mysqlbinlog /data/mysql/mysql-bin.000010

Note: Because my local mysqlbinlog can't recognize the default-character-set=utf8 in the binlog configuration, so here I added –no-defaults to the command to work, and everyone took a lesson.

 

6. Restore the specified data

When recovering data through MySQL's binlog log, we can specify the recovery to a specific point in time, which is a bit like server snapshot management. So now we need to restore the 3 pieces of data that were just deleted. You can find a point in time before the deletion and restore to that point in time.

Regarding the usage of the mysqlbinlog command, we can view it through the help command of mysqlbinlog, as follows:

mysqlbinlog –no-defaults –help

As shown in the help document, you can restore data by specifying a time or a specified location. Here I will take the specified time as an example to show you.

Let's check the log file mysql-bin.000009, (that is, the log before the current binlog log, the current log is 000010) as follows:

The command is as follows:

>mysqlbinlog --no-defaults --stop-datetime="2020-12-08 16:03:09" binlog.000009 |mysql -uroot -p

At this time, we were looking at the background and found that the three articles that were deleted just now have been restored, thus achieving our desired purpose.

to sum up

How to recover data through MySQL binary log file, you must do a good job of website data backup at ordinary times, data is the lifeblood of the website, do a good job of data backup to avoid unnecessary trouble or loss later.

 Finally, we will introduce several bin_log operations:
   1. Check the last bin log file which is the current location.

show master status;

  2. Enable a new log file, generally executed after the database is backed up.

flush logs;

3. Clear the existing bin-log used 

reset master;

 

Guess you like

Origin blog.csdn.net/Coder_Boy_/article/details/110870799