MySQL使用binlog2sql闪回误删除数据
发表于:2024-11-27 作者:千家信息网编辑
千家信息网最后更新 2024年11月27日,查询数据库相关配置参数root [test]> show global variables like 'binlog%format%';+---------------+-------+| Varia
千家信息网最后更新 2024年11月27日MySQL使用binlog2sql闪回误删除数据
查询数据库相关配置参数
root [test]> show global variables like 'binlog%format%';+---------------+-------+| Variable_name | Value |+---------------+-------+| binlog_format | ROW |+---------------+-------+1 row in set (0.00 sec)root [test]> show global variables like 'binlog%row%image%';+------------------+-------+| Variable_name | Value |+------------------+-------+| binlog_row_image | FULL |+------------------+-------+1 row in set (0.00 sec)root [test]> show global variables like '%log%bin%';+----------------------------------+-----------------------------------------------+| Variable_name | Value |+----------------------------------+-----------------------------------------------+| log_bin | ON || log_bin_basename | /data1/mysql_log_23306/binlog/mysql-bin || log_bin_index | /data1/mysql_log_23306/binlog/mysql-bin.index || log_bin_trust_function_creators | ON || log_bin_use_v1_row_events | OFF || log_statements_unsafe_for_binlog | ON |+----------------------------------+-----------------------------------------------+6 rows in set (0.01 sec)
安装binlog2sql
正克隆到 'binlog2sql'...remote: Counting objects: 298, done.remote: Compressing objects: 100% (4/4), done.remote: Total 298 (delta 0), reused 1 (delta 0), pack-reused 294接收对象中: 100% (298/298), 147.01 KiB | 49.00 KiB/s, done.处理 delta 中: 100% (151/151), done.[root@mysql-server binlog2sql]# source ../venv4archer/bin/activate(venv4archer) [root@mysql-server binlog2sql]# pip install -r requirements.txtRequirement already satisfied: PyMySQL==0.7.11 in /data1/venv4archer/lib/python3.6/site-packages (from -r requirements.txt (line 1)) (0.7.11)Collecting wheel==0.29.0 (from -r requirements.txt (line 2)) Cache entry deserialization failed, entry ignored Downloading https://files.pythonhosted.org/packages/8a/e9/8468cd68b582b06ef554be0b96b59f59779627131aad48f8a5bce4b13450/wheel-0.29.0-py2.py3-none-any.whl (66kB) 100% |████████████████████████████████| 71kB 103kB/s Collecting mysql-replication==0.13 (from -r requirements.txt (line 3)) Downloading https://files.pythonhosted.org/packages/dd/23/384047702e694139e9fe75a8ba7ad007e8942fd119ebadabc32ce19f70f2/mysql-replication-0.13.tar.gzBuilding wheels for collected packages: mysql-replication Running setup.py bdist_wheel for mysql-replication ... done Stored in directory: /root/.cache/pip/wheels/91/33/05/32b16ccadd4fc566ff38af96afdeb5d57d49c2f1eff0402164Successfully built mysql-replicationInstalling collected packages: wheel, mysql-replication Found existing installation: wheel 0.31.1 Uninstalling wheel-0.31.1: Successfully uninstalled wheel-0.31.1Successfully installed mysql-replication-0.13 wheel-0.29.0
创建测试数据,并执行误删除
root [(none)]> flush logs;Query OK, 0 rows affected (0.00 sec)root [(none)]> use testDatabase changedroot [test]> create table user(id int(12) unsigned auto_increment comment 'id' primary key, name varchar(15), add_time timestamp);Query OK, 0 rows affected (0.01 sec)root [test]> insert into user(name, add_time) values('neo', '2018-09-01'), ('trinity', '2018-09-02'), ('jason', '2018-09-05');Query OK, 3 rows affected (0.00 sec)Records: 3 Duplicates: 0 Warnings: 0root [test]> delete from user where add_time < '2018-09-05';Query OK, 2 rows affected (0.01 sec)
查看二进制日志文件
root [test]> show binary logs;+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000002 | 2236 || mysql-bin.000003 | 201 || mysql-bin.000004 | 1218 |+------------------+-----------+3 rows in set (0.00 sec)
解析出标准SQL
(venv4archer) [root@mysql-server binlog2sql]# python binlog2sql/binlog2sql.py -uroot -p'root' -h 127.0.0.1 -P 23306 -dtest -tuser --start-file='mysql-bin.000004' --start-datetime='2018-09-14 17:00:00' --stop-datetime='2018-09-14 18:25:00' > /tmp/20180914_raw.sql(venv4archer) [root@mysql-server binlog2sql]# cat /tmp/20180914_raw.sqlUSE b'test';create table user(id int(12) unsigned auto_increment comment 'id' primary key, name varchar(15), add_time timestamp);INSERT INTO `test`.`user`(`id`, `name`, `add_time`) VALUES (1, 'neo', '2018-09-01 00:00:00'); #start 411 end 824 time 2018-09-14 18:22:33INSERT INTO `test`.`user`(`id`, `name`, `add_time`) VALUES (2, 'trinity', '2018-09-02 00:00:00'); #start 411 end 824 time 2018-09-14 18:22:33INSERT INTO `test`.`user`(`id`, `name`, `add_time`) VALUES (3, 'jason', '2018-09-05 00:00:00'); #start 411 end 824 time 2018-09-14 18:22:33DELETE FROM `test`.`user` WHERE `id`=1 AND `name`='neo' AND `add_time`='2018-09-01 00:00:00' LIMIT 1; #start 855 end 1187 time 2018-09-14 18:23:31DELETE FROM `test`.`user` WHERE `id`=2 AND `name`='trinity' AND `add_time`='2018-09-02 00:00:00' LIMIT 1; #start 855 end 1187 time 2018-09-14 18:23:31
解析出回滚SQL
(venv4archer) [root@mysql-server binlog2sql]# python binlog2sql/binlog2sql.py -uroot -p'root' -h 127.0.0.1 -P 23306 -dtest -tuser --start-file='mysql-bin.000004' --start-datetime='2018-09-14 17:00:00' --stop-datetime='2018-09-14 18:25:00' -B > /tmp/20180914_rollback.sql (venv4archer) [root@mysql-server binlog2sql]# cat //tmp/20180914_rollback.sql INSERT INTO `test`.`user`(`id`, `name`, `add_time`) VALUES (2, 'trinity', '2018-09-02 00:00:00'); #start 855 end 1187 time 2018-09-14 18:23:31INSERT INTO `test`.`user`(`id`, `name`, `add_time`) VALUES (1, 'neo', '2018-09-01 00:00:00'); #start 855 end 1187 time 2018-09-14 18:23:31DELETE FROM `test`.`user` WHERE `id`=3 AND `name`='jason' AND `add_time`='2018-09-05 00:00:00' LIMIT 1; #start 411 end 824 time 2018-09-14 18:22:33DELETE FROM `test`.`user` WHERE `id`=2 AND `name`='trinity' AND `add_time`='2018-09-02 00:00:00' LIMIT 1; #start 411 end 824 time 2018-09-14 18:22:33DELETE FROM `test`.`user` WHERE `id`=1 AND `name`='neo' AND `add_time`='2018-09-01 00:00:00' LIMIT 1; #start 411 end 824 time 2018-09-14 18:22:33
数据
二进制
参数
对象
数据库
文件
日志
标准
处理
查询
测试
配置
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
广西互联网网络安全工程
成都京软软件开发有限公司
怎么装2000数据库
出租服务器如何找客户托管
聊呗红包软件开发
php获取服务器系统
软件开发克服困难
数据库管理电子书机械工业出版社
网络安全意识的认识
互联网服务器开发流程
软件开发数据库搭建
中信银行软件开发中心社招
新网厦门服务器
公务员网络安全是什么专业
违反网络安全法第27章规定
华为服务器pcie卡安装在哪里
积木软件开发人
服务器代理权腾讯云
刘欣网络安全
科远数据采集服务器
软件开发学习群
网络安全上课课件
爱企推软件开发工作室
数据库系统名称是什么情况
密云区正规软件开发概况
悉尼大学的计算机网络安全硕士
网络运维跟网络安全
碧蓝档案玩什么服务器好
为什么一个公司要用到多个数据库
绵阳网络安全技术研究院