


Detailed steps to restore mysql database through binlog file on Linux_MySQL
1. Binlog introduction
The server’s binary log records all additions, deletions, and modifications of the database (provided that binlog is enabled on your own server), and also includes the execution time of these operations. In order to display these binary contents, we can use the mysqlbinlog command to view it.
Usage 1: Master-slave synchronization
Usage 2: Restoring the database (I only learned about this after a database file was lost online)
Mysqlbinlog command usage: shell> mysqlbinlog [options] log_file ...
1) mysqlbinlog option example
Common options include the following:
--start-datetime
Read from the binary log a specified time equal to the timestamp or later than the local computer. Values such as:="1470733768" or="2016-08-09 5:09:28"
Example:
1 2 |
|
Read the specified time from the binary log that is less than the timestamp or equal to the local computer. The value is the same as above
--start-position
Read the specified position event position from the binary log as the start. Value:="2698"
Example:
1 2 |
|
Read the specified position event position from the binary log as the event end. Value:="2698"
2. Environment preparation and backup and recovery
1) After installing mysql, check to enable binlog
mysql> SHOW BINARY LOGS;
ERROR 1381 (HY000): You are not using binary logging: The above prompt indicates that there is no server to enable binlog
Modify /etc/my.cnf
Add a line in the mysqld option as follows:
log-bin=mysql-bin
By default, if no value is given, log-bin will use mysqld-bin as the index and create mysqld-bin.00001, etc.
Just restart mysqld.
2) Check the binlog
1 2 3 4 5 6 7 |
|
3) First create some raw data.
1 2 3 4 5 6 7 8 9 |
|
Check the data:
1 2 3 4 5 6 7 8 9 |
|
4) Backup and restore (full backup and restore)
Here we simulate the daily complete backup database task.
1 2 |
|
An operation error occurred during simulation and the data was modified incorrectly.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Now we use traditional methods to restore and restore.
1 |
|
Check again:
1 2 3 4 5 6 7 8 9 |
|
You can see that the data has been restored.
5) Use binlog to simulate restoration
Create several pieces of data based on the original table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
If we accidentally modify the data or delete the library at this time, causing all data to be lost, if we use the latest backup file Test_DB_0809-16:50.sql at this time to restore the data, it will be lost. Newly inserted data after backup.
Note: If you really use the most recent backup file, it must be a last resort (for example, the binlog is deleted, the entire hard disk hangs... It's scary to think about it...).
Simulate misoperation and change user names in batches.
1 2 3 |
|
No, the previous step was not ruthless enough. Let’s be more ruthless here and delete all the tables
1 2 3 4 5 6 |
|
Since we turned on the binlog option at the beginning, we used binlog to restore the database. Let's start with the binlog. First check the binlog file. Currently, my mysql service has been restarted twice since binlog was turned on, so there are 2 binlog files (each time it is restarted, a binlog file will be regenerated. Another situation is to run The FLUSH LOGS command will also rebuild one);
What is recorded in the mysql-bin.index file is: a list of all binary logs recorded since the log-bin option is turned on.
Note: In an actual production environment, if you encounter a situation where you need to restore the database, do not allow users to access the database to avoid new data being inserted, and in a master-slave environment, shut down the master-slave.
Use the mysqlbinlog command to view the binlog file. Let’s take a look at the latest file mysql-bin.00002
You can see from the end that there is a deletion operation. But we can't completely restore it because there is still a deletion operation at the end.
现在我的思路就是,先将第一个binlog 和第二个binlog 文件导出来à利用指定的position位置的方式(过滤掉删除表操作和update Test_DB.OneTb set name='user10';这条语句 ),导出2个sql 语句,最后我们将2个sql 合成一个sql,导入到数据库中即可。
我们先用mysqlbinlog命令找到update 那条语句的位置,然后指定position 将mysql-bin.00001 导出来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
从上面可以看到我们在做插入正常数据后的position 是2698,那么使用下面的命令导出sql
1 |
|
然后导出mysql-bin.00002的sql 语句(注:由于演示操作,该文件只有一个drop 表操作,所以不做处理,但是在实际环境中,由于中途可能会有重启数据库操作,那时就需要检测最新的binlog有没有业务需要的语句。)
sql 语句已经导出来了。我们可以利用该语句直接恢复所有正常的数据。
注:本次恢复没有利用到之前的完整备份,因为我是开启binlog后,然后才做的所有建库建表操作,第一个binlog文件里已经记录了所有的数据库操作,所以不需要使用之前的完整备份(另外:实际的生产环境,还是需要利用到完整备份的,因为线上环境可能会有N多个binlog文件,所以需要利用到完整备份和最新的binlog文件来结合恢复)
开始恢复前,我们将原有的Test_DB数据库也给干掉吧。毕竟我们的binlog中有创建操作
1 2 |
|
恢复数据库时还可以利用在登陆mysql 后,用source 命令导入sql语句,这里暂不介绍
1 |
|
Enter password:
恢复完成后,我们检查下表的数据是否完整
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Ok完整的都恢复过来了。
三、总结
1) 恢复方式
a) 利用最新一次的完整备份加binlog 指定事件起始时间和终止时间或者position恢复数据库
b) 利用所有binlog指定事件起始位置和终止时间来合并sql文件恢复数据库(此方法要确保binlog文件的完整)
c) 利用mysqldump 使用完整恢复。(在确保最新一次的完整备份后的数据不重要,允许丢掉的情况下,直接恢复。该方法最简单、效率最高)
2) 附:官方建议的备份原则(为了能睡个好觉….嗯,是的)
a) 在mysql安装好并运行时,就始终开启 log-bin选项,该日志文件位于datadir目录下,也要确保该目录所在存储介质是安全的。
b) 定期做完整的mysql 备份。
c) 定期使用 FlUSH LOGS 或者 mysqladmin flush-logs ,该操作会关闭当前的二进制日志文件,并新建一个binlog日志文件。(和重启mysql后新建的binlog操作一样)。以备份binlog日志,利用binlog日志也可以做增量备份。
以上所述是小编给大家介绍的Linux上通过binlog文件恢复mysql数据库详细步骤,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

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

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

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)
