


Analyze how to restore database data through Mysql binary log (detailed graphic and text explanation)
This article mainly introduces in detail how to restore database data through the binary log of Mysql. It has certain reference value. Those who are interested can learn more.
Frequently, website administrators accidentally delete website data due to various reasons and operations, and do not make website backups. As a result, they are at a loss, and even bring negative consequences to website operations and profitability. Negative impact. So in this article, we will share with you how to recover data through Mysql's second mechanism log (binlog).
System environment:
Operating system: CentOS 6.5 X64 (virtual machine);
WEB service: PHP+Mysql+apache;
Website: For convenience, build a DEMO site directly using the Chanzhi system locally;
Operation steps:
1. Turn on the binlog function and basic operations;
2. Add data to the site;
3. Refresh the binlog log;
4. Delete data;
5. Binlog log content analysis;
6. Restore the specified data;
1. Enable the binlog function and basic operations
To use the binlog log function of Mysql, you must first open the binlog function of Mysql Enable this function in the configuration file and 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 function is usually turned on by default.
After turning on the binlog function, there will be files such as mysql-bin.000001, mysql-bin.000002 and other files in the mysql database directory. This is the binary log file of mysql. A new binary log file will be created every time mysql is started or the log is refreshed manually.
First of all, in our 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 website, I added several pieces of test data.
3. Refresh the binlog log
Previously, the binlog file of mysql was mysql-bin.000001, and it was sent to the database in the background of the website. Three articles have been added to . Now we refresh the binlog log and a new mysql-bin.000002 file will be generated, as follows:
1 2 3 |
|
4. Delete data
Here I delete all the three articles I just added.
5.binlog log content analysis
Mysql’s binary log file records mysql operations, such as the deletion operation just now. Let’s take a look at the specific contents of the log file. .
Use the mysqlbinlog command of mysql:
1 |
|
Note: Because my local mysqlbinlog cannot recognize the default-character-set=utf8 in the binlog configuration , so here I added "-no-defaults" to the command to make it work, and everyone can learn from it.
The following is a partial screenshot of the log content:
6. Restore the specified data;
When restoring data through the binlog log of mysql, we can specify the recovery 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 time point before deletion and restore it to that time point.
Regarding how to use the mysqlbinlog command, we can check it through the mysqlbinlog help command, as follows:
1 |
|
As shown in the help document, data can be restored by specifying time or location. Here I will use the specified time as an example to demonstrate to you.
Let’s check the log file mysql-bin.000001, as follows:
1 |
|
Through the previous Operation steps We know that before deleting data, we generated the mysql-bin.000002 log file, so we only need to restore to this point in time. I have found this time in the picture above.
The command is as follows:
Copy code The code 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 the desired purpose.
Summary:
This article shares with you how to recover data through the mysql binary log file. But I still want to remind everyone that you should back up your website data in normal times. Some of the current mainstream CMS website building systems have built-in database backup functions, such as the Chanzhi system I use here. Data is the lifeblood of the website. Do a good job in backing up data to avoid later problems. unnecessary trouble or loss.
The above is the detailed content of Analyze how to restore database data through Mysql binary log (detailed graphic and text explanation). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.
