系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 数据库 > Mysql > 详细页面

MySQL恢复误删数据图文教程

时间:2023-10-27来源:系统城装机大师作者:佚名

MySQL 恢复误删数据,针对 window 和 Linux 均适用,只需要找到对应的 binlog 目录文件,默认就是 MySQL 安装目录下的 data 文件夹

一般误删数据,先停止所有操作,备份数据库

1
2
3
4
# 备份所有数据库
mysqldump -uroot -p123456 --all-databases > /backup/mysqldump/all.db
# 恢复数据
mysql -uroot -p db_name < /backup/mysqldump/db_name.db

1、查看是否启用 binlog 日志

1 SHOW VARIABLES LIKE '%log_bin%'

2、查看所有 binlog 日志

1 SHOW BINARY LOGS;

3、查看正在使用的日志

1 SHOW MASTER STATUS;

4、查找日志所在文件夹

1 SHOW VARIABLES LIKE '%datadir%';

5、log 日志转 sql

使用上面使用的 binlog.000056 文件,先把 该文件复制到其他地方

1 mysqlbinlog E:\test\result\binlog.000056 > E:\test\result\db.sql

有可能报错

添加 --no-defaults 参数可以解决,但中文会乱码(使用下面 vbs 或自定义语言实现可解决 乱码问题),一般要加上时间字段转换 sql

1 mysqlbinlog --no-defaults --base64-output=decode-rows -v --database=oauth --start-datetime="2023-06-01 01:44:00" --stop-datetime="2023-06-01 01:48:00" F:\mysql-8.0.29-winx64\data\binlog.000058 > E:\test\result\db.sql

转换成功如下,可以看到删除 sql 的语句

接下来只需要将上面的 delete 语句转换为 inert 即可恢复误删数据,如果需要转换的多可以通过代码自定义实现,主要就是将
delete 转 insert

6、delete 转 insert 恢复误删

通过 vbs 脚本转换 sql 语句,当然也可以使用其他的语言,window 执行 vbs 不需要额外的环境,你只需要修改下面 vbs 文件中输入输出文件名以及编码类型即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'==========================
'用VBS实现 MYSQL binglog DELETE 转 INSERT
'==========================
function replaceregex(patern,str,tagstr)
dim regex,matches
set regex=new regExp
regex.pattern=patern
regex.IgnoreCase=true
regex.global=true
matches=regex.replace(str,tagstr)
replaceregex=matches
end function
 
'Mysql binlog DELETE转INSERT==========
'VBS打开文本文件
Set oldStream = CreateObject("ADODB.Stream")
oldStream.CharSet = "utf-8"
oldStream.Open
'binLog 生成的 DELETE 原日志文件'
oldStream.LoadFromFile("E:\test\result\db.sql")
oldText = oldStream.ReadText()
newText=replace(oldText,"### DELETE FROM", ";INSERT INTO")
newText=replace(newText,"### WHERE", "SELECT")
newText=replace(newText,"###", "")
newText=replace(newText,"@1=", "")
newText=replaceregex("@[1-9]=",newText, ",")
newText=replaceregex("@[1-9][0-9]=",newText, ",")
oldStream.Close
'VBS保存文件
Set newStream = CreateObject("ADODB.Stream")
newStream.Type = 2 'Specify stream type - we want To save text/string data.
newStream.Charset = "utf-8" 'Specify charset For the source text data.
newStream.Open 'Open the stream And write binary data To the object
newStream.WriteText newText
newStream.SaveToFile "mysqllogOK.sql", 2 'DELETE转成INSERT以后的新的SQL文件名
newStream.Close

转换结果如下,自行选择新增恢复数据

总结

到此这篇关于MySQL恢复误删数据的文章就介绍到这了

分享到:

相关信息

  • mysql数据库之count()函数和sum()函数用法及区别说明

    mysql之count()和sum()用法及区别1、mysql 数据库中 count() 函数是统计查询结果中的行数,例如我们有下表 user_auth :使用 count() 函数来查询结果个数,使用如下查询: 1 ...

    2023-10-27

  • Centos 7.9安装MySQL8.0.32的详细教程

    第一步、下载压缩包 第二步,解压压缩包,并复制到安装目录 第三步、编辑配置文件 第四步、确定一些相关的目录 第五步、初始化数据库 第六步、启动数据库,连接并修改 root 密码 第六步、守护 MySQL 进程...

    2023-03-19

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载