mysql通过binlog来恢复被删除的数据库
binlog日志
查询:
MariaDB [(none)]> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | OFF |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> show variables like 'binlog_format';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.01 sec)
设置:
log_bin/data/mysql/mysql-bin
binlog_format=row
详细:
[mysqld]
basedir=/application/mysql
datadir=/application/mysql/data
socket=/tmp/mysql.sock
log-error=/var/log/mysql.log
log-bin=/data/mysql/mysql-bin
server-id=1
port=3306
log_bin=/data/mysql/mysql-bin
binlog_format=row
[client]
socket=/tmp/mysql.sock
设置完成后检查:
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON | <------ binlog日志开启
+---------------+-------+
1 row in set (0.00 sec)
mysql> show variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW | <------ binlog日志开启
+---------------+-------+
1 row in set (0.00 sec)
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 177 |
| mysql-bin.000002 | 177 |
| mysql-bin.000003 | 97002 |
| mysql-bin.000004 | 97002 |
| mysql-bin.000005 | 97002 |
| mysql-bin.000006 | 143 |
| mysql-bin.000007 | 120 |
+------------------+-----------+
7 rows in set (0.01 sec)
binlog开启后,可以通过命令查看到哪些日志是正在使用的
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#这里的日志 只有一个是在被使用的
查看一个binlog日志详情:
mysql> show binlog events in 'mysql-bin.000004' limit 5;
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000004 | 4 | Format_desc | 6 | 123 | Server ver: 5.7.20-log, Binlog ver: 4 |
| mysql-bin.000004 | 123 | Previous_gtids | 6 | 154 | |
| mysql-bin.000004 | 154 | Anonymous_Gtid | 6 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000004 | 219 | Query | 6 | 317 | CREATE DATABASE mysql; |
| mysql-bin.000004 | 317 | Anonymous_Gtid | 6 | 382 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
5 rows in set (0.00 sec)
这是查看这个binlog日志的前五个事件。
记住: 在binlog最小的记录单元叫event,一个事务会被拆分为多个event。
event特性: 每个event都有一个开始位置[start-position]和一个结束位置[stop-position]
所谓的位置就是: event对于整个二进制文件的相对位置
在一个二进制日志中,前120个 position是文件格式信息的预留空间
也就是mysql第一个记录的时间,都是从120开始的
--row模式下的二进制日志分析案例:
1. 查看日志量
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2. 查看日志量
mysql> create database binlog;
Query OK, 1 row affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 220 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) ## 说并已经记录了二进制日志了
再添加数据:
mysql> use binlog;
Database changed
mysql> create table bt(id int);
Query OK, 0 rows affected (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 321 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
插入一个ddl内容:
mysql> insert into bt values(1);
Query OK, 1 row affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 513 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[自动提交,如果没有变化可以使用 commit 提交一次]
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 705 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
主要命令:
mysql> show master status;
mysql> create database binlog;
mysql> show master status;
mysql> use binlog;
#更新数据
mysql> update bt set id=2 where id=2;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 1862 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
此时感觉错了,想恢复到delete之前的操作发现不会,所以我果断直接删了:
mysql> select * from bt;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> drop table bt;
Query OK, 0 rows affected (0.00 sec)
此时通过binlog查看数据:
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 1981 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
此时。。连库也直接删了:
mysql> drop database binlog;
Query OK, 0 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| world |
+--------------------+
5 rows in set (0.00 sec)
这时候,整个binlog库都被删除了。
此时来了个大神需要恢复数据:
此时需要用二进制日志进行恢复
恢复思路:
二进制日志是以时间进记录的.
1. 通过show master status; 查看到记录的日志文件。
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 | 2070 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2. 查看这个日志:
mysql> show binlog events in 'mysql-bin.000007';
+------------------+------+-------------+-----------+-------------+---------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+-------------+-----------+-------------+---------------------------------------------------------+
| mysql-bin.000007 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.38-log, Binlog ver: 4 |
| mysql-bin.000007 | 120 | Query | 1 | 220 | create database binlog |
| mysql-bin.000007 | 220 | Query | 1 | 321 | use `binlog`; create table bt(id int) |
| mysql-bin.000007 | 321 | Query | 1 | 395 | BEGIN |
| mysql-bin.000007 | 395 | Table_map | 1 | 442 | table_id: 70 (binlog.bt) |
| mysql-bin.000007 | 442 | Write_rows | 1 | 482 | table_id: 70 flags: STMT_END_F |
| mysql-bin.000007 | 482 | Xid | 1 | 513 | COMMIT /* xid=46 */ |
| mysql-bin.000007 | 513 | Query | 1 | 587 | BEGIN |
| mysql-bin.000007 | 587 | Table_map | 1 | 634 | table_id: 70 (binlog.bt) |
| mysql-bin.000007 | 634 | Write_rows | 1 | 674 | table_id: 70 flags: STMT_END_F |
| mysql-bin.000007 | 674 | Xid | 1 | 705 | COMMIT /* xid=48 */ |
| mysql-bin.000007 | 705 | Query | 1 | 779 | BEGIN |
| mysql-bin.000007 | 779 | Table_map | 1 | 826 | table_id: 70 (binlog.bt) |
| mysql-bin.000007 | 826 | Write_rows | 1 | 866 | table_id: 70 flags: STMT_END_F |
| mysql-bin.000007 | 866 | Xid | 1 | 897 | COMMIT /* xid=79 */ |
| mysql-bin.000007 | 897 | Query | 1 | 971 | BEGIN |
| mysql-bin.000007 | 971 | Table_map | 1 | 1018 | table_id: 70 (binlog.bt) |
| mysql-bin.000007 | 1018 | Write_rows | 1 | 1058 | table_id: 70 flags: STMT_END_F |
| mysql-bin.000007 | 1058 | Xid | 1 | 1089 | COMMIT /* xid=81 */ |
| mysql-bin.000007 | 1089 | Query | 1 | 1163 | BEGIN |
| mysql-bin.000007 | 1163 | Table_map | 1 | 1210 | table_id: 70 (binlog.bt) |
| mysql-bin.000007 | 1210 | Write_rows | 1 | 1250 | table_id: 70 flags: STMT_END_F |
| mysql-bin.000007 | 1250 | Xid | 1 | 1281 | COMMIT /* xid=87 */ |
| mysql-bin.000007 | 1281 | Query | 1 | 1355 | BEGIN |
| mysql-bin.000007 | 1355 | Table_map | 1 | 1402 | table_id: 70 (binlog.bt) |
| mysql-bin.000007 | 1402 | Delete_rows | 1 | 1442 | table_id: 70 flags: STMT_END_F |
| mysql-bin.000007 | 1442 | Xid | 1 | 1473 | COMMIT /* xid=89 */ |
| mysql-bin.000007 | 1473 | Query | 1 | 1547 | BEGIN |
| mysql-bin.000007 | 1547 | Table_map | 1 | 1594 | table_id: 70 (binlog.bt) |
| mysql-bin.000007 | 1594 | Delete_rows | 1 | 1634 | table_id: 70 flags: STMT_END_F |
| mysql-bin.000007 | 1634 | Xid | 1 | 1665 | COMMIT /* xid=91 */ |
| mysql-bin.000007 | 1665 | Query | 1 | 1739 | BEGIN |
| mysql-bin.000007 | 1739 | Table_map | 1 | 1786 | table_id: 70 (binlog.bt) |
| mysql-bin.000007 | 1786 | Delete_rows | 1 | 1831 | table_id: 70 flags: STMT_END_F |
| mysql-bin.000007 | 1831 | Xid | 1 | 1862 | COMMIT /* xid=93 */ |
| mysql-bin.000007 | 1862 | Query | 1 | 1981 | use `binlog`; DROP TABLE `bt` /* generated by server */ |
| mysql-bin.000007 | 1981 | Query | 1 | 2070 | drop database binlog |
+------------------+------+-------------+-----------+-------------+---------------------------------------------------------+
37 rows in set (0.00 sec)
发现binlog也记录了删除数据的操作。但是又很多看不懂,此时需要使用binlog的专门工具进行查看
查看:
mysqlbinlog --base64-output=decode-rows -vvv /data/mysql/mysql-bin.000007
截取删除之前的日志:
mysqlbinlog --start-position=120 --stop-position=1402 /data/mysql/mysql-bin.000007 >bin.sql
在数据库中暂时关闭日志记录:
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.01 sec)
开始恢复数据:
mysql> source /root/bin.sql
验证数据:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| binlog |
| mysql |
| performance_schema |
| test |
| world |
+--------------------+
6 rows in set (0.00 sec)
已经恢复了 被删除的 binlog 数据库
mysql> show tables;
+------------------+
| Tables_in_binlog |
+------------------+
| bt |
+------------------+
1 row in set (0.00 sec)
经过检查 也恢复了被删除的BT表。
mysql通过binlog来恢复被删除的数据库的更多相关文章
- MySQL使用mysqldump+binlog完整恢复被删除的数据库
(一)概述 在日常的MySQL数据库运维过程中,可能会遇到用户误删除数据,常见的误删除数据操作有: 用户执行delete,因为条件不对,删除了不应该删除的数据(DML操作): 用户执行update,因 ...
- MySQL通过binlog日志恢复数据
一.查看下自己的MySQL是否开启了binlog日志 # 是否启用binlog日志 OFF:关闭 ON:开启 show variables like 'log_bin'; 二.开启binlog日志 在 ...
- Mysql利用binlog日志恢复数据操作(转)
a.开启binlog日志:1)编辑打开mysql配置文件/etc/mys.cnf[root@vm-002 ~]# vim /etc/my.cnf在[mysqld] 区块添加 log-bin=mysql ...
- MySQL的binlog日志恢复(转)
binlog 基本认识 MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日 ...
- MySQL利用binlog来恢复数据库
1.根据binlog解析出所有ring数据库的所有sql [mysql@localhost ]$ mysqlbinlog --no-defaults --database=ring --start-d ...
- MySQL全备+binlog恢复方法之伪装master【原创】
利用mysql全备 +binlog server恢复方法之伪装master 单实例试验 一.试验环境 10.72.7.40 实例 mysql3306为要恢复的对象,mysql3306的全备+binlo ...
- mysql利用binlog恢复数据详细例子
模拟数据恢复的案例 有些时候脑瓜就会短路,难免会出错 场景:在生产环境中,我们搭建了mysql主从,备份操作都是在从备份数据库上 前提:有最近一天或者最近的全备 或者最近一天相关数据库的备份 最重要的 ...
- 如何通过Mysql的二进制日志恢复数据库数据
经常有网站管理员因为各种原因和操作,导致网站数据误删,而且又没有做网站备份,结果不知所措,甚至给网站运营和盈利带来负面影响.所以本文我们将和大家一起分享学习下如何通过Mysql的二机制日志(binlo ...
- 不小心删除数据--利用MySQL的binlog恢复数据
MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...
- 【转】【MySQL】mysql 通过bin-log恢复数据方法详解
mysql中bin-log在mysql默认状态下是没有打开的,我们要先打开mysql 开启bin-log功能,然后再通过备份的bin-log进行数据库恢复了. 具体的操作是通过mysqlbinlog这 ...
随机推荐
- CentOS 5.5编译安装vsftpd-2.3.4配置虚拟用户
CentOS 5.5编译安装vsftpd-2.3.4配置虚拟用户 2012-02-22 17:18:15| 分类: rhel_vsftp|举报|字号 订阅 centos 5.5编译安装vsftpd- ...
- centos6.5源码编译http2.4.9、虚拟主机、基于用户认证
centos6.5源码编译http2.4.9.虚拟主机.基于用户认证 2014-04-23 07:45 作者: 51linux 来源: 本站 浏览: 0 views 我要评论 字号: 大 中 ...
- Git中 fork, clone,branch
一.是什么 fork fork,英语翻译过来就是叉子,动词形式则是分叉,如下图,从左到右,一条直线变成多条直线 转到git仓库中,fork则可以代表分叉.克隆 出一个(仓库的)新拷贝 包含了原来的仓库 ...
- 【ESP32 IDF】用RMT控制 WS2812 彩色灯带
在上一篇中,老周用 .NET Nano Framework 给大伙伴们演示了 WS2812 灯带的控制,包括用 SPI 和 红外RMT 的方式.利用 RMT 是一个很机灵的方案,不过,可能很多大伙伴对 ...
- 力扣121(java&python)-买卖股票的最佳时机(简单)
题目: 给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格. 你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票.设计一 ...
- Apache ShenYu 网关正式支持 Dubbo3 服务代理
简介: 本文介绍了如何通过 Apache ShenYu 网关访问 Dubbo 服务,主要内容包括从简单示例到核心调用流程分析,并对设计原理进行了总结. 作者:刘良 Apache Dubbo 在去年发布 ...
- [FAQ] golang-migrate/migrate error: migration failed in line 0: (details: Error 1065: Query was empty)
当我们使用 migrate create 创建了迁移文件. 没有及时填写内容,此时运行 migrate 的后续命令比如 up.down 会抛出错误: error: migration failed i ...
- [FAQ] Cannot use object of type MongoInt64 as array
此类情况出现在 需要使用字符串/数组,实际却使用了对象. 解决方式是调试数据,对数据类型进行转换即可. MongoInt64 对象提供 __toString 方法,那么可以强转成 64 位 integ ...
- [FAQ] uni-app 导航路由切换时如何强制刷新页面?
使用 this.$forceUpdate() 强制刷新页面. Refer:uni-app自定义导航 Link:https://www.cnblogs.com/farwish/p/13870801.ht ...
- dotnet 通过 DockerfileContext 解决项目放在里层文件夹导致 VisualStudio 构建失败
本文告诉大家,如何解决 csproj 项目文件放入到里层的文件夹,不放在 sln 所在文件夹的第一层子文件夹,导致 VisualStudio 2022 在构建 docker 映像提示找不到文件的问题 ...