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这 ...
随机推荐
- Scrapy 基础入门
0x01 框架概述 Scrapy 是基于 Python 的一个非常流行的网络爬虫框架,可以用来抓取 Web 站点并从页面中提取结构化的数据 (1)核心组件 引擎:用来控制整个系统的数据处理流程 调度器 ...
- 美团二面:如何保证Redis与Mysql双写一致性?连续两个面试问到了!
引言 Redis作为一款高效的内存数据存储系统,凭借其优异的读写性能和丰富的数据结构支持,被广泛应用于缓存层以提升整个系统的响应速度和吞吐量.尤其是在与关系型数据库(如MySQL.PostgreSQL ...
- Nginx 简介、安装与配置文件详解
〇.前言 在日常工作中,Nginx 的重要性当然不言而喻. 经常用,但并不意味着精通,还会有很多不清楚的方式和技巧,那么本文就简单汇总下,帮助自己理解. 一.Nginx 简介 1.1 关于 Nginx ...
- Xilinx USB JTAG两种JTGA-HS3和Platfrom下载器速度对比
下面测试速度,以一个V7的配置文件为例子.文件大小如下,27MB.特别是对于有点规模的项目配置文件都是很大的.总不能是点灯项目. 选择普通的下载器,Platform Cable USB.这种下载器是基 ...
- opensips使用drouting进行路由
操作系统 :CentOS 7.6_x64 opensips版本:2.4.9 drouting是Dynamic Routing(动态路由)的缩写,该模块可为特定呼叫选择(基于多个条件)最佳网关.今天整理 ...
- 力扣8(java)-字符串转整数(atoi)(中等)
题目: 请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数). 函数 myAtoi(string s) 的算法 ...
- MaxCompute同步数据的网络配置
MaxCompute可以通过数据集成加载不同数据源(例如:MySQL数据库等)数据,同样也可以通过数据集成把MaxCompute的数据导出到各种业务数据库.数据集成功能已经集成到DataWorks作为 ...
- 深度解密|基于 eBPF 的 Kubernetes 问题排查全景图发布
简介:通过 eBPF 无侵入地采集多语言.多网络协议的黄金指标/网络指标/Trace,通过关联 Kubernetes 对象.应用.云服务等各种上下文,同时在需要进一步下钻的时候提供专业化的监测工具( ...
- MySQL深潜|剖析Performance Schema内存管理
简介: 本文主要是通过对PFS引擎的内存管理源码的阅读,解读PFS内存分配及释放原理,深入剖析其中存在的一些问题,以及一些改进思路. 一 引言 MySQL Performance schema(PF ...
- dotnet6 C# 一个国内还能用的 NTP 时间校准客户端的实现
本文来记录一个我自己在使用的 NTP 时间校准客户端的实现 核心方法是在国内使用 腾讯 和 阿里 提供的 NTP 时间服务器来获取网络时间,如果连接不上,再依次换成 国家服务器 和 中国授时 服务,如 ...