先来一段 自行体会

 #----------------------------------------------------------------------------------
#模拟通过binlog进行数据恢复
#---------------------------------------------------------------------------------- #开启新的二进制日志 便于记录
mysql> flush logs;
Query OK, 0 rows affected (0.02 sec) mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) #开启自动commit 便于测试
mysql> set autocommit=1;
mysql> create database nod;
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.000005 | 211 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql>use nod;
mysql> create table luna(id int);
Query OK, 0 rows affected (0.08 sec) mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 314 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql> insert into luna values(1);
Query OK, 1 row affected (0.00 sec) mysql> insert into luna values(2);
Query OK, 1 row affected (0.00 sec) mysql> insert into luna values(3);
Query OK, 1 row affected (0.02 sec) mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 896 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql> select * from luna;
+------+
| id |
+------+
| 1 |
| 22 |
| 3 |
+------+
3 rows in set (0.00 sec) mysql> drop table luna;
Query OK, 0 rows affected (0.02 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| binlog |
| db |
| mysql |
| nod |
| performance_schema |
| test |
| world |
+--------------------+
8 rows in set (0.00 sec) mysql> drop database nod;
Query OK, 0 rows affected (0.00 sec) mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 1494 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) [root@db01 ~]# mysqlbinlog --base64-output=decode-rows -vvv /data/mysql/mysql-bin.000005
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#180804 15:24:48 server id 6 end_log_pos 120 CRC32 0x2f031b05 Start: binlog v 4, server v 5.6.38-log created 180804 15:24:48
# Warning: this binlog is either in use or was not closed properly.
# at 120
#180804 15:24:48 server id 6 end_log_pos 211 CRC32 0x7fc33332 Query thread_id=3 exec_time=8182 error_code=0
SET TIMESTAMP=1533367488/*!*/;
SET @@session.pseudo_thread_id=3/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create database nod
/*!*/;
# at 211
#180804 15:24:48 server id 6 end_log_pos 314 CRC32 0xae4fffd2 Query thread_id=3 exec_time=8274 error_code=0
use `binlog`/*!*/;
SET TIMESTAMP=1533367488/*!*/;
create table luna(id int)
/*!*/;
# at 314
#180804 15:24:48 server id 6 end_log_pos 388 CRC32 0x829f43a8 Query thread_id=3 exec_time=8363 error_code=0
SET TIMESTAMP=1533367488/*!*/;
BEGIN
/*!*/;
# at 388
#180804 15:24:48 server id 6 end_log_pos 437 CRC32 0x1801dec8 Table_map: `binlog`.`luna` mapped to number 73
# at 437
#180804 15:24:48 server id 6 end_log_pos 477 CRC32 0xfb860ce0 Write_rows: table id 73 flags: STMT_END_F
### INSERT INTO `binlog`.`luna`
### SET
### @1=1 /* INT meta=0 nullable=1 is_null=0 */
# at 477
#180804 15:24:48 server id 6 end_log_pos 508 CRC32 0x88bca0f6 Xid = 135
COMMIT/*!*/;
# at 508
#180804 15:24:48 server id 6 end_log_pos 582 CRC32 0xfdea7f27 Query thread_id=3 exec_time=8366 error_code=0
SET TIMESTAMP=1533367488/*!*/;
BEGIN
/*!*/;
# at 582
#180804 15:24:48 server id 6 end_log_pos 631 CRC32 0xac422642 Table_map: `binlog`.`luna` mapped to number 73
# at 631
#180804 15:24:48 server id 6 end_log_pos 671 CRC32 0x3817c497 Write_rows: table id 73 flags: STMT_END_F
### INSERT INTO `binlog`.`luna`
### SET
### @1=2 /* INT meta=0 nullable=1 is_null=0 */
# at 671
#180804 15:24:48 server id 6 end_log_pos 702 CRC32 0x4c81edc8 Xid = 136
COMMIT/*!*/;
# at 702
#180804 15:24:48 server id 6 end_log_pos 776 CRC32 0xf3c75c08 Query thread_id=3 exec_time=8369 error_code=0
SET TIMESTAMP=1533367488/*!*/;
BEGIN
/*!*/;
# at 776
#180804 15:24:48 server id 6 end_log_pos 825 CRC32 0x7d9b565c Table_map: `binlog`.`luna` mapped to number 73
# at 825
#180804 15:24:48 server id 6 end_log_pos 865 CRC32 0x773957ea Write_rows: table id 73 flags: STMT_END_F
### INSERT INTO `binlog`.`luna`
### SET
### @1=3 /* INT meta=0 nullable=1 is_null=0 */
# at 865
#180804 15:24:48 server id 6 end_log_pos 896 CRC32 0x3df025e7 Xid = 137
COMMIT/*!*/;
# at 896
#180804 15:24:48 server id 6 end_log_pos 970 CRC32 0x500edb7f Query thread_id=3 exec_time=8432 error_code=0
SET TIMESTAMP=1533367488/*!*/;
BEGIN
/*!*/;
# at 970
#180804 15:24:48 server id 6 end_log_pos 1019 CRC32 0xdaf097a5 Table_map: `binlog`.`luna` mapped to number 73
# at 1019
#180804 15:24:48 server id 6 end_log_pos 1065 CRC32 0x3fccc29c Update_rows: table id 73 flags: STMT_END_F
### UPDATE `binlog`.`luna`
### WHERE
### @1=2 /* INT meta=0 nullable=1 is_null=0 */
### SET
### @1=22 /* INT meta=0 nullable=1 is_null=0 */
# at 1065
#180804 15:24:48 server id 6 end_log_pos 1096 CRC32 0x355a2b60 Xid = 141
COMMIT/*!*/;
# at 1096
#180804 15:24:48 server id 6 end_log_pos 1170 CRC32 0x4f936f7d Query thread_id=3 exec_time=8703 error_code=0
SET TIMESTAMP=1533367488/*!*/;
BEGIN
/*!*/;
# at 1170
#180804 15:24:48 server id 6 end_log_pos 1219 CRC32 0x36b88afd Table_map: `binlog`.`luna` mapped to number 73
# at 1219
#180804 15:24:48 server id 6 end_log_pos 1259 CRC32 0xd3e293de Delete_rows: table id 73 flags: STMT_END_F
### DELETE FROM `binlog`.`luna`
### WHERE
### @1=1 /* INT meta=0 nullable=1 is_null=0 */
# at 1259
#180804 15:24:48 server id 6 end_log_pos 1290 CRC32 0x0a3ec6c4 Xid = 143
COMMIT/*!*/;
# at 1290
#180804 15:24:48 server id 6 end_log_pos 1411 CRC32 0x653cd09b Query thread_id=3 exec_time=8723 error_code=0
SET TIMESTAMP=1533367488/*!*/;
DROP TABLE `luna` /* generated by server */
/*!*/;
# at 1411
#180804 15:24:48 server id 6 end_log_pos 1494 CRC32 0x2ea0c2db Query thread_id=3 exec_time=8745 error_code=0
SET TIMESTAMP=1533367488/*!*/;
drop database nod
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/; #------------------------------------------------------- [root@db01 ~]# mysqlbinlog --start-position=120 --stop-position=1170 /data/mysql/mysql-bin.000005 >/tmp/nod.sql; mysql> source /tmp/nod.sql; #-------------------------------------------------------
查看
#-------------------------------------------------------
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| binlog |
| db |
| mysql |
| nod |
| performance_schema |
| test |
| world |
+--------------------+ mysql> select * from luna;
+------+
| id |
+------+
| 1 |
| 22 |
| 3 |
+------+
3 rows in set (0.00 sec)

升级版本:

多库单表误删除期中某个库的恢复操作

使用mysqlbinlog -d参数  注意-d参数的使用

 #-------------------------------------------------------------------------------
#
# 多库单表的操作
# Author:nod
# Date:18-08-05
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# 模拟环境
#-------------------------------------------------------------------------------
mysql> flush logs; mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000010 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec) mysql> create database nod;
Query OK, 1 row affected (0.00 sec) mysql> create database luna;
Query OK, 1 row affected (0.00 sec) mysql> use nod;
Database changed
mysql> create table t1(id int);
Query OK, 0 rows affected (0.06 sec) mysql> insert into t1 values(1);
Query OK, 1 row affected (0.01 sec) mysql> insert into t1 values(2);
Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values(3);
Query OK, 1 row affected (0.00 sec) mysql> use luna
Database changed
mysql> create table t2(id int);
Query OK, 0 rows affected (0.15 sec) mysql> insert into t2 values(44);
Query OK, 1 row affected (0.00 sec) mysql> insert into t2 values(55);
Query OK, 1 row affected (0.06 sec) mysql> use nod;
Database changed
mysql> insert into t1 values(88);
Query OK, 1 row affected (0.02 sec) mysql> select * from nod.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 88 |
+------+
4 rows in set (0.00 sec) mysql> select * from luna.t2;
+------+
| id |
+------+
| 44 |
| 55 |
+------+
2 rows in set (0.00 sec) mysql> drop database nod;
Query OK, 1 row affected (0.05 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| binlog |
| db |
| luna |
| mysql |
| performance_schema |
| test |
| world |
+--------------------+
8 rows in set (0.00 sec) #-------------------------------------------------------------------------------
# 分析binlog
# 注意参数使用-d 指定数据库 nod 此处不希望出现 luna
#-------------------------------------------------------------------------------
mysqlbinlog -d nod --base64-output=decode-rows -vvv /data/mysql/mysql-bin.000010
mysqlbinlog -d nod --start-position=120 --stop-position=1617 /data/mysql/mysql-bin.000010 >/tmp/bak_nod.sql #-------------------------------------------------------------------------------
# 数据库开始恢复
#-------------------------------------------------------------------------------
mysql> set sql_log_bin=0;
Query OK, 0 rows affected (0.00 sec) mysql> source /tmp/bak_nod.sql;
Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Charset changed
Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 1 row affected (0.00 sec) Database changed
Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.10 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.02 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) #-------------------------------------------------------------------------------
# 检查恢复数据
#-------------------------------------------------------------------------------
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| binlog |
| db |
| luna |
| mysql |
| nod |
| performance_schema |
| test |
| world |
+--------------------+
9 rows in set (0.00 sec) mysql> select * from nod.t1;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
| 88 |
+------+
4 rows in set (0.00 sec) mysql> select * from luna.t2;
+------+
| id |
+------+
| 44 |
| 55 |
+------+
2 rows in set (0.00 sec) #-------------------------------------------------------------------------------
# 数据恢复完毕,如之前drop前所示
#-------------------------------------------------------------------------------

sql层的查询日志

3.1、二进制日志都记录了什么? 除了标准的select语句

(1) 已提交的DML事务语句,并拆分为多个事件(event)来进行记录

已提交的事务语句 注意是从begin-commit

例如此处begin—commit  拆分成5个event

begin --- 1

1      ---  2

2      ---  3

3      ---  4

commit --- 5

(2) 记录所有DDL、DCL等语句

总之二进制日志,会记录所有对数据库发生修改的操作

2、二进制记录格式有哪些?

statement:语句模式   就是sql语句

row:行模式,即数据行的变化过程

mixed:以上两者的混合模式。

我们企业推荐使用row模式,5.6中默认模式statement,5.7中默认row

3、两种模式有什么优缺点?

statement:

优点:简单明了,容易被看懂,就是sql语句,记录时需要更小的磁盘空间

缺点:记录不够严谨。特别害怕函数类的操作

row 模式:

优点:记录更加严谨

缺点:有可能需要更多的磁盘空间,不太容易读懂

为什么说row模式严谨:比如插入操作中含有now()  仔细揣摩   建议使用row模式

4、binlog的作用

binlog是一个二进制格式的文件,用于记录用户对数据库更新的SQL语句信息,例如更改数据库表和更改内容的SQL语句都会记录到binlog里,但是对库表等内容的查询不会记录。默认情况下,binlog日志是二进制格式的,不能使用查看文本工具的命令(比如,cat,vi等)查看,而使用mysqlbinlog解析查看

记住一句话:

如果我拥有从数据库搭建开始所有的二进制日志,那么我可以把数据库恢复到任意一个时刻

备份恢复

复制

二进制日志管理操作实战:

  • 1、查看二进制日志开关状态

mysql> show variables like 'log_bin';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| log_bin       | ON    |

+---------------+-------+

1 row in set (0.00 sec)

mysql> show variables like 'binlog_format';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| binlog_format | ROW   |

+---------------+-------+

1 row in set (0.00 sec)

  • 2、设置binlog

vim /etc/my.cnf

二进制日志的开关,并且设置二进制日志的位置(/data/mysql/,路径必须事先存在并且有权限),

并且设定二进制日志格式前缀(mysql-bin),例如:mysql-bin.000001 mysql-bin.000002

# 在配置文件当中进行修改 /etc/my.cnf

log_bin=/data/mysql/mysql-bin   # 二进制日志的开关并且设置二进制日志的位置

binlog_format=row

  • 3、二进制日志的操作:

3.1 查询二进制日志的基本信息

(1)操作系统层面查看

cd /data/mysql/

ls –l   #会发现有很多 最大数值编号的是最新的

(2)数据库内部查看    现在是statement格式

show binary logs;   #查看有多少mysql-bin文件 查看目录作用类似

show master status;#查看当前的使用的mysql-bin文件

写满&重启数据库&执行命令会做文件的切割

show binlog events in 'mysql-bin.000032' limit 5;#查看mysql-bin文件内容

说明:

event:binlog最小的记录单元为event,一个事务会被拆分为多个event

evenet特性:每个event都有一个开始位置(start-position)和一个结束位置(stop-position)

所谓的位置:是evenet对整个二进制文件的相对位置

对于一个二进制日志中,前120个position是文件格式信息预留空间

也就是mysql第一个记录的事件,都是从120开始的。

截取日志一定要从Begin到commit 完整

show master status当中 Positon表示的含义 最后一个事件的结束位置

MySQL 二进制文件恢复数据基础版本的更多相关文章

  1. mysql binlog恢复数据实战

    在前面,我们了解了mysql binlog日志的作用以及使用方法:  http://www.php20.cn/article/237 在后面讲到了,可以通过binlog进行恢复数据,那么,具体步骤是怎 ...

  2. MySQL 二进制文件恢复

    先不说话  先来一段代码块 mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name ...

  3. MySql binlog恢复数据

    1. 直接导入数据库 mysqlbinlog --database=testdb mysql-bin. | mysql -uroot -f 2. 导出成SQL文 (1) 从binlog输出为SQL m ...

  4. mysql 恢复数据

    前提:保存了需要恢复数据库的文件 .frm 和 .ibd 文件 条件:InnoDB 类型的 恢复表结构1.新建一个数据库--新建一个表,表名和列数和需要恢复数据库相同2.停止mysql服务器 serv ...

  5. Mysql实战之数据备份

    author:JevonWei 版权声明:原创作品 blog:http://119.23.52.191/ --- 数据备份和恢复 mysqldump 冷备份单库(不会创建新库,需要手动创建并指定导入数 ...

  6. 从binlog恢复数据及Mysqlbinlog文件删除

    做了mysql主从也有一段时间了,这两天检查磁盘空间情况,发现放数据库的分区磁盘激增了40多G,一路查看下来,发现配置好主从复制以来到现在的binlog就有40多G,原来根源出在这里,查看了一下my. ...

  7. MySQL基础之 恢复数据和数据库迁移

    1.mysql命令或者source命令恢复数据 这两个命令在进行恢复数据的时候要检查是否创建数据库.如果数据库不存在,则恢复失败. 数据库迁移 1.相同版本的mysql数据库之间的迁移. mysqld ...

  8. MySQL切换版本踩坑记录(包括恢复数据方法)

    踩坑起因:在创建数据库时, 字段:create_time datetime DEFAULT CURRENT_TIMESTAMP, 报异常--Error Code: 1067 - Invalid def ...

  9. 重新装Mysql后原来数据的恢复办法:即一个版本的mysql的data文件夹下的所有的数据,怎么去加载到另一个安装的mysql中

    重新装Mysql后原来数据的恢复办法本博客:http://blog.csdn.net/xiaowu_zhu/article/details/71188955 不管是重新装系统或者数据库时,总会遇到怎么 ...

随机推荐

  1. linux----别名

    经常一些命令太长,输入太麻烦,给该命令起个别名,直接执行,简单又方便. 1.查看别名 alias 2.编辑别名 vi ~/.brashrc 3.添加自己的别名 例如:重启网卡 alias netres ...

  2. Python中msgpack库的使用

    msgpack用起来像json,但是却比json快,并且序列化以后的数据长度更小,言外之意,使用msgpack不仅序列化和反序列化的速度快,数据传输量也比json格式小,msgpack同样支持多种语言 ...

  3. python基础(五)——CGI编程

    使用python实现get方法和post方法传值,多选按钮,单选按钮.文本编辑区.下拉列表数据的传递,cookie的设置文件上传,文件下载.本文未经整理,仅供参考 #!/usr/bin/python ...

  4. SpringJDBC——jdbcTemplate模板

    一.定义 Spring框架对jdbc进行了封装,提供的一个JDBCTemplated对象简化jdbc开发. 使用步骤 1 导包spring-beans-5.0.0-RELEASE.jar,spring ...

  5. <---------------------装箱,拆箱的过程-------------------------->

    基本类型包装类: 程序界面用户输入的数据都是以字符串类型存储的,转换成基本数据类型. 八种基本类型对应的包装类: 装箱和拆箱: public class InterDemo { public stat ...

  6. 数学 它的内容,方法和意义 第一卷 (A. D. 亚历山大洛夫 著)

    第一章 数学概观 (已看) 1. 数学的特点 2. 算术 3. 几何 4. 算术和几何 5. 初等数学时代 6. 变量的数学 7. 现代数学 8. 数学的本质 9. 数学发展的规律性 第二章 数学分析 ...

  7. 学习笔记:Javascript 变量 包装对象

    学习笔记:Javascript 变量 包装对象 如下代码,可以输出字符的长度. var str = "Tony"; str.length; 这时再试试以下代码,返回是 undefi ...

  8. windows下搭建voip服务器

    软件: yate-6.0.0-1-setup.exe 服务端,里面也有个客户端 eyeBeam.exe 客户端 步骤: 失败....

  9. ES9新特性

    这篇文章主要介绍ES2018(ES9)的新特性, 以及使用方法 JS是一门跨平台的语言, ES6也就是ECMAScript 2015 花费了5年的时间敲定, 是一次非常大的改版, 之后每年都有一个小版 ...

  10. jQuery的ready()事件与js中的onload事件的区别

    出处:http://blog.csdn.net/yuanmei1986/article/details/50781453