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来恢复被删除的数据库的更多相关文章

  1. MySQL使用mysqldump+binlog完整恢复被删除的数据库

    (一)概述 在日常的MySQL数据库运维过程中,可能会遇到用户误删除数据,常见的误删除数据操作有: 用户执行delete,因为条件不对,删除了不应该删除的数据(DML操作): 用户执行update,因 ...

  2. MySQL通过binlog日志恢复数据

    一.查看下自己的MySQL是否开启了binlog日志 # 是否启用binlog日志 OFF:关闭 ON:开启 show variables like 'log_bin'; 二.开启binlog日志 在 ...

  3. Mysql利用binlog日志恢复数据操作(转)

    a.开启binlog日志:1)编辑打开mysql配置文件/etc/mys.cnf[root@vm-002 ~]# vim /etc/my.cnf在[mysqld] 区块添加 log-bin=mysql ...

  4. MySQL的binlog日志恢复(转)

    binlog 基本认识 MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日 ...

  5. MySQL利用binlog来恢复数据库

    1.根据binlog解析出所有ring数据库的所有sql [mysql@localhost ]$ mysqlbinlog --no-defaults --database=ring --start-d ...

  6. MySQL全备+binlog恢复方法之伪装master【原创】

    利用mysql全备 +binlog server恢复方法之伪装master 单实例试验 一.试验环境 10.72.7.40 实例 mysql3306为要恢复的对象,mysql3306的全备+binlo ...

  7. mysql利用binlog恢复数据详细例子

    模拟数据恢复的案例 有些时候脑瓜就会短路,难免会出错 场景:在生产环境中,我们搭建了mysql主从,备份操作都是在从备份数据库上 前提:有最近一天或者最近的全备 或者最近一天相关数据库的备份 最重要的 ...

  8. 如何通过Mysql的二进制日志恢复数据库数据

    经常有网站管理员因为各种原因和操作,导致网站数据误删,而且又没有做网站备份,结果不知所措,甚至给网站运营和盈利带来负面影响.所以本文我们将和大家一起分享学习下如何通过Mysql的二机制日志(binlo ...

  9. 不小心删除数据--利用MySQL的binlog恢复数据

    MySQL Binary Log也就是常说的bin-log, ,是mysql执行改动产生的二进制日志文件,其主要作用有两个: * 数据回复 * 主从数据库.用于slave端执行增删改,保持与maste ...

  10. 【转】【MySQL】mysql 通过bin-log恢复数据方法详解

    mysql中bin-log在mysql默认状态下是没有打开的,我们要先打开mysql 开启bin-log功能,然后再通过备份的bin-log进行数据库恢复了. 具体的操作是通过mysqlbinlog这 ...

随机推荐

  1. Vim 安装与基础操作指南

    0x00 链接 Vim 官网 Vim GitHub Vim 中文文档 0x01 准备 (1)下载与安装 在官网地址找到 Download 标签,在其中根据操作系统选择相应的版本,以下以 Windows ...

  2. windows server 出现.net 3.5安装不上去

    前言 有人说为什么到了现在还有人用.net 3.5呢?其实我也不用,主要是mmsql安装的时候要用到. 正文 一般我们不同电脑在程序和功能中添加,如果添加不上会弹出一个自动获取的一个过程,这时我们点击 ...

  3. ubuntu 20.04.1 安装 PHP+Nginx

    ubuntu 20.04.1 安装 PHP+Nginx 更新源 sudo apt-get update 安装环境包 sudo apt-get -y install nginx sudo apt-get ...

  4. mysql入门操作(部分操作,不为完全格式)

    查询数据库在电脑中绝对路径: show variables like '%datadir%'; 设置字符集 set names gbk; 导入数据库 source 绝对路径 eg: source D: ...

  5. node统计指定文件夹内代码行数

    1. 来源 想对于自己接触前端日常学习与思考的代码行数进行一个统计,看自己大约敲了多少代码 2.代码 const fs = require('fs') const path = require('pa ...

  6. 第二課:Mirth培養興趣之旅 ——由定時刷庫接口編程講起

    1.准备工作 1.1 本机安装vs2019:(https://visualstudio.microsoft.com/zh-hans/) 1.2 本机安装win64的MariaDB 10.3.27版本数 ...

  7. HarmonyOS NEXT应用开发—听歌识曲水波纹特效案例

    介绍 在很多应用中,会出现点击按钮出现水波纹的特效. 效果图预览 使用说明 进入页面,点击按钮,触发水波纹动画. 再次点击按钮,停止水波纹动画. 实现思路 本例涉及的关键特性和实现方案如下: 要实现存 ...

  8. SpringBoot Admin2.0 集成 Java 诊断神器 Arthas 实践

    简介: 项目最初使用 Arthas 主要有两个目的: 1. 通过 arthas 解决实现测试环境.性能测试环境以及生产环境性能问题分析工具的问题. 2. 通过使用 jad.mc.redefine 功能 ...

  9. 如何保证 Serverless 业务部署更新的一致性?

    简介: 代码在其他场景被更新,需要我们在当前得到感知,这个事情其实是非常重要的,和代码的安全发布密不可少.而此时,通过 Serverless Devs 是可以做到的. 作者|Anycodes​ 从我做 ...

  10. WPF 通过 GetMessageExtraInfo 方法获取当前收到的鼠标消息是否由触摸转换过来

    本文将告诉大家如何在 WPF 或者其他 Win32 应用里面,在收到鼠标消息时,通过 GetMessageExtraInfo 方法获取当前收到的鼠标消息是否由触摸消息提升而来 大家都知道,在不开启 W ...