How to recover database data through MySQL's binary log

Summary: System Environment Operating System: CentOS 6.5 X64 (Virtual Machine); Web Service: PHP+MySQL+apache; Website: For convenience, build a demo site directly with Cicada system locally; Operation step 1, enable binlog function and basic Operation To use MySQL's binlog log function, you must first enable this function in the MySQL configuration file. The operation is very simple.

System Environment
Operating System : CentOS 6.5 X64 (Virtual Machine);
Web Service: PHP+MySQL+apache;
Website: For convenience, build a demo site directly with Cicada system locally;
Step 1
, enable binlog function and basic operations
To use MySQL's binlog log function, you must first enable this 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. In fact, in the various MySQL environments I have installed, this feature is usually enabled by default.

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

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


2. Add data to
the site In the background article module of the site, I added several pieces of test data.


3. Refresh the binlog log
Previously, the MySQL binlog file was mysql-bin.000001, and three articles were added to the database in the background of the website. Now we refresh the binlog log, and a new mysql-bin.000002 file will be generated, as follows:

flush logs;
show master logs;

4. Delete data
Here I delete all the three articles I just added.

5. Binlog log content analysis
MySQL operations recorded in the MySQL binary log file, such as the delete operation just now, let's take a look at the specific content of the log file.

Use MySQL's mysqlbinlog command:

mysqlbinlog /data/mysql/mysql-bin.000002
Note: Because my local mysqlbinlog cannot recognize default-character-set=utf8 in the binlog configuration, so here I add --no-defaults to the command It only works, everyone can learn from it.


The following is a screenshot of the log content:


6. Restoring specified data
When through MySQL's binlog log, we can specify to restore to a specific point in time, which is a bit like server snapshot management. So now we want to restore the article we just deleted, we can find a point in time before deletion and restore to that point in time.

For 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 documentation, you can restore data by specifying the time or location. Here I will show you the specified time as an example.

Let's check the log file mysql-bin.000001, as follows:

mysqlbinlog -no--defaults /data/mysql/mysql-bin.000001

Through the previous steps, we know that before deleting the data, we generated mysql-bin.000002 log file, so we only need to restore to this point in time, which I have found in the above picture.

The command is as follows:

mysqlbinlog –no-defaults –stop-datetime='2017-04-11 09:48:48'/data/mysql/mysql-bin.000001 |mysql –uroot –p123456
At this time, we were looking at the background and found that just now The three deleted articles have all been restored, thus achieving our desired purpose.

Summary
This article shared with you how to recover data from MySQL's binary log files. But I still want to remind everyone that it is necessary to do a good job of website data backup in peacetime. Some mainstream CMS website building systems now have built-in database backup functions, such as the cicada system I use here. Data is the lifeblood of the website, and data backup is necessary to avoid later unnecessary trouble or loss.

The original release time is: 2017-04-17

This article is from Yunqi community partner "Linux China"
[url]http://click.aliyun.com/m/21743/ [/url]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326642319&siteId=291194637