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. Deep Learning on Graphs: A Survey第五章自动编码论文总结

    论文地址:https://arxiv.org/pdf/1812.04202.pdf 最近老师让我们读的一片论文,已经开组会讲完了,我负责的是第五章,图的自动编码,现在再总结一遍,便于后者研读.因为这篇 ...

  2. @EnableDiscoveryClient 注解如何实现服务注册与发现

    @EnableDiscoveryClient 是如何实现服务注册的?我们首先需要了解 Spring-Cloud-Commons 这个模块,Spring-Cloud-Commons 是 Spring-C ...

  3. log4j2 lookup漏洞修复方法

    2021.12.10凌晨,Apache Log4j远程代码执行漏洞细节被公开,参考链接:https://unit42.paloaltonetworks.com/apache-log4j-vulnera ...

  4. .NET开源免费的跨平台框架 - MAUI(附学习资料)

    前言 前几天分享了一个.NET MAUI开源免费的UI工具包 - Uranium,然后技术群有不少同学问.NET MAUI是不是免费的?能做什么?今天特意写这篇文章来介绍一下.NET开源.免费(基于M ...

  5. 剑指 Offer 58 - II(Java)-左旋转字符串(简单)

    题目: 字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部.请定义一个函数实现字符串左旋转操作的功能.比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位 ...

  6. 1.css的初认识

    1.什么是CSS? Cascading Style Sheet 层叠级联样式表 CSS:表现层(美化网页) 字体.颜色.边距.高度.宽度.背景图片.网页定位.网页浮动.... 2.CSS发展史 CSS ...

  7. 重磅发布 | Serverless 应用中心:Serverless 应用全生命周期管理平台

    ​简介:Serverless 应用中心,是阿里云 Serverless 应用全生命周期管理平台.通过 Serverless 应用中心,用户在部署应用之前无需进行额外的克隆.构建.打包和发布操作,即可快 ...

  8. 性能提升3倍、时延降低70%,阿里云企业级存储ESSD云盘再升级!

    9月22日,阿里云存储年度新品发布会上,阿里云基础产品资深产品总监陈起鲲发布了其全球领先的旗舰级块存储产品ESSD的两款新规格(ESSD Auto PL.ESSD PL-X),并宣布了新增的多项企业级 ...

  9. [GPT] golang代码组织的核心思想

    1/golang代码组织的核心思想 Go语言(Golang)在代码组织上的核心思想是模块化和封装,主要体现在以下几个方面: 1.包(Packages): Go语言通过包(Packages)来组织代码, ...

  10. [Go] go-nsq 使用指南

    首先你需要有一个 nsq 的服务端,nsq 由三部分构成:nsqd.nsqlookupd.nsqadmin. 快速启动 nsq 一个节点看这里:https://github.com/farwish/n ...