Home Database Mysql Tutorial Detailed explanation of how to restore database data through Mysql binary log (picture and text)

Detailed explanation of how to restore database data through Mysql binary log (picture and text)

Apr 18, 2017 am 09:20 AM

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 do website backup. As a result, they are at a loss, and even bring negative consequences to website operation 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 specified data;

1. Enable binlog function and basic operations

Touse MysqlThe binlog log function must first be turned on in the configuration file of Mysql. 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 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:


flush logs;

show master logs;
Copy after login

4. Delete Data

Here I have deleted all 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:

 mysqlbinlog /data/mysql/mysql-bin.000002
Copy after login

Note: Because my local mysqlbinlog cannot recognize the default-character-set=utf8 in the binlog configuration, so here I am using the command It only works after adding "-no-defaults", 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:

mysqlbinlog –no-defaults –help
Copy after login

As shown in the help document, you can specify To restore data at a specific time or specified location, here I will demonstrate to you using 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
Copy after login

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

The commands are as follows:

复制代码 代码如下:

mysqlbinlog –no-defaults –stop-datetime='2017-04-11 09:48:48'/data/mysql/mysql-bin.000001 |mysql –uroot –p123456
Copy after login

这时我们在看后台,发现刚才删除的三篇文章都已恢复回来了,从而到达我们期望的目的。

 总结:

本文和大家分享了如何通过mysql的二进制日志文件恢复数据。但还是要提醒大家,在平时要做好网站数据备份,现在的一些主流CMS建站系统都会内置数据库备份功能,比如这里我用的蝉知系统,数据是网站的命脉,做好数据备份以避免后期不必要的麻烦或损失。

The above is the detailed content of Detailed explanation of how to restore database data through Mysql binary log (picture and text). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

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.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

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".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

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.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

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.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

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.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles