Before using mysql binlog for data recovery, binlog must be enabled first. Set up a test environment here to learn how to use mysql binlog to restore the database. The principle is relatively simple. Binlog will store changed data in mysql. For example, if you create a database and write some data, these will be stored in the binlog of mysql.
When you need to restore, you need to find two positions: a starting position and an ending position. Half of the ending position is the position before the data was destroyed or deleted. Mysql 8 has binlog enabled by default
mysql> show variables like '%log_bin%'; +---------------------------------+--------------------------+ | Variable_name | Value | +---------------------------------+--------------------------+ | log_bin | ON | | log_bin_basename | /data/mysql/binlog | | log_bin_index | /data/mysql/binlog.index | | log_bin_trust_function_creators | OFF | | log_bin_use_v1_row_events | OFF | | sql_log_bin | ON | +---------------------------------+--------------------------+
You can see that log_bin has been enabled. At the same time, you can see that the storage location is in the /daba/mysql directory. The prefix is binlog
ls /data/mysql/binlog.* /data/mysql/binlog.000143 /data/mysql/binlog.000144 /data/mysql/binlog.000145 /data/mysql/binlog.000146 /data/mysql/binlog.index
You can see that there are several A binlog log file, because this is the test database (there is historical information, previous data is useless), in order to facilitate testing, the reset operation is performed directly. Be careful when deleting all binlog files. If it is a production environment, the value of the data is immeasurable.
show master logs; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000143 | 156 | No | | binlog.000144 | 200 | No | | binlog.000145 | 156 | No | | binlog.000146 | 156 | No | +---------------+-----------+-----------+ 4 rows in set (0.01 sec) mysql> reset master; Query OK, 0 rows affected (0.02 sec) mysql> show master logs; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 156 | No | +---------------+-----------+-----------+ 1 row in set (0.00 sec) ls /data/mysql/binlog.* /data/mysql/binlog.000001 /data/mysql/binlog.index
After executing the reset (reset master), you can see that the previous binlog file has been deleted and a new binlog file is generated.
You can check the contents of this file
$mysqlbinlog binlog.000001 # The proper term is pseudo_replica_mode, but we use this compatibility alias # to make the statement usable on server versions 8.0.24 and older. /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; ...
At this time, we add some data
mysql <<EOT create database test_liuhaolin_com; select now(); EOT mysql <<EOT use test_liuhaolin_com; create table if not exists test( \`id\` int unsigned not null auto_increment primary key, \`key\` varchar(100), \`val\` varchar(255) ) engine=myisam charset=utf8mb4; EOT mysql <<EOT use test_liuhaolin_com; insert into test values ('1','website', 'https://www.liuhaolin.com'); EOT
At this time, we accidentally deleted the database test_liuhaolin_com
mysql> drop database test_liuhaolin_com; Query OK, 1 row affected (0.09 sec)
Now we need to solve a practical problem, how to restore this database
First of all, in order to prevent interference, execute flush logs to generate a new binlog file.
mysql> show master logs; +---------------+-----------+-----------+ | Log_name | File_size | Encrypted | +---------------+-----------+-----------+ | binlog.000001 | 1594 | No | +---------------+-----------+-----------+ 1 row in set (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000001 | 1594 | | | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> flush logs; Query OK, 0 rows affected (0.08 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +---------------+----------+--------------+------------------+-------------------+ | binlog.000002 | 156 | | | | +---------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
To recover data, you must first find where the data was deleted.
mysqlbinlog binlog.000001 | grep -n "drop database" 113:drop database test_liuhaolin_com
You can see that there is a delete statement at line 113. Finally, you can find two places
The starting position of the data that needs to be restored
The ending position of the data that needs to be restored
The starting position here is where the database is created, and the ending position is where the database is deleted.
mysqlbinlog --set-charset=utf8 binlog.000001 > tmp.sql
You can know the starting and ending positions in the file tmp.sql
binlog
You can see that the starting position is 233
binlog
You can see that the end position is 1371, so perform data recovery.
mysqlbinlog -v binlog.000001 --start-position=233 --stop-position=1371 | mysql
As a verification, you can check whether the database exists before execution.
mysql> use test_liuhaolin_com; ERROR 1049 (42000): Unknown database 'test_liuhaolin_com' # 执行恢复操作 mysqlbinlog -v binlog.000001 --start-position=233 --stop-position=1371 | mysql # 再次检查,可以发现数据已经恢复 mysql> mysql> use test_liuhaolin_com; Database changed
The above is the detailed content of How to use mysql binlog to restore data. For more information, please follow other related articles on the PHP Chinese website!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor