个人博客:mysql数据恢复,binlog详解

binlog日志恢复数据,是挽救错误操作和数据损坏一根救命稻草,所以认识和使用binglog对于技术人员还是很有必要的

binlog一般用于

  • 主从复制 中 master节点开启binlog把二进制日志传递给slave节点达到主从数据一致
  • 第二点自然是用于数据恢复了,使用mysqlbinlog工具来恢复数据

因为我自己的网站遇到过mysql表被我误删操作,drop table后当时我还是挺淡定的,虽然我不清楚我能不能把数据拯救回来,对于个人网站而言可能都没有主从,也没有把mysql的各项配置设置完美,这种情形下最担心的当然是binlog是否开启了,如果没有开启binlog并且也没有做数据备份,我感觉基本上就GG了

因为我mysql是通过docker容器安装的,所以具体 my.cnf 配置文件的放在哪也忘记了

  1. 使用 find / -name my.cnf 找到文件在哪
root@0d5861775029:/# find / -name my.cnf

find: '/proc/1/map_files': Operation not permitted
find: '/proc/182/map_files': Operation not permitted
find: '/proc/187/map_files': Operation not permitted
find: '/proc/1601/map_files': Operation not permitted
find: '/proc/1731/map_files': Operation not permitted
find: '/proc/1741/map_files': Operation not permitted
/etc/alternatives/my.cnf
/etc/mysql/my.cnf
/var/lib/dpkg/alternatives/my.cnf root@0d5861775029:/#
  • 查看my.cnf配置找到binlog以及mysql数据存储的位置
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0 # Custom config should go here
!includedir /etc/mysql/conf.d/
  • 可以看到mysql数据存储的目录是 datadir = /var/lib/mysql 目录中
root@0d5861775029:/etc/mysql# cd /var/lib/mysql/
root@0d5861775029:/var/lib/mysql# ls
#innodb_temp binlog.index client-key.pem ib_logfile1 mysql.ibd server-cert.pem undo_002
auto.cnf ca-key.pem db_blog ibdata1 performance_schema server-key.pem
binlog.000001 ca.pem ib_buffer_pool ibtmp1 private_key.pem sys
binlog.000002 client-cert.pem ib_logfile0 mysql public_key.pem undo_001

上面的前戏都看完了,这其实并非binlog具体使用,而是我个人发现数据目录方式 接下来我将详细介绍binlog的使用

一、开启binlog日志

  • 查看binlog是否开启

    • ON 表示已经开启
    • 查看更多内容可以这样 show variables like 'log_%';
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> show variables like 'log_%';
+----------------------------------------+----------------------------------------+
| Variable_name | Value |
+----------------------------------------+----------------------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/binlog |
| log_bin_index | /var/lib/mysql/binlog.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| log_error | stderr |
| log_error_services | log_filter_internal; log_sink_internal |
| log_error_suppression_list | |
| log_error_verbosity | 2 |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | ON |
| log_slow_admin_statements | OFF |
| log_slow_extra | OFF |
| log_slow_slave_statements | OFF |
| log_statements_unsafe_for_binlog | ON |
| log_throttle_queries_not_using_indexes | 0 |
| log_timestamps | UTC |
+----------------------------------------+----------------------------------------+
18 rows in set (0.00 sec)
  • 编辑my.cnf开启binlog
在[mysqld] 区块
设置/添加 log-bin=mysql-bin 确认是打开状态(值 mysql-bin 是日志的基本名或前缀名);

然后重启mysql

二、查看binlog日志操作命令

  1. 查看所有binlog日志列表
mysql> show logs;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'logs' at line 1
mysql> show master logs;
+---------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000001 | 3091158 | No |
| binlog.000002 | 141156437 | No |
+---------------+-----------+-----------+
2 rows in set (0.17 sec)
  1. 查看master状态,也就是最新一个binlog日志编号名称和最后一个操作事件pos结束位置
mysql> show master status;
+---------------+-----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+-----------+--------------+------------------+-------------------+
| binlog.000002 | 141156437 | | | |
+---------------+-----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  1. 刷新log日志,将会产生一个新编号的binlog日志文件
mysql> flush logs;
  1. 重置(清空)所有binlog日志
mysql> reset master;

三、查看binlog日志内容

  1. 使用mysqlbinlog命令查看

因为binlog是二进制文件,普通文件查看器都无法打开,必须使用自带的mysqlbinlog命令查看

  • mysqlbinlog binlog.000002 使用mysqlbinlog不好观察
  1. 在mysql中查看binlog日志
mysql> show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];

 选项解析:
IN 'log_name' 指定要查询的binlog文件名(不指定就是第一个binlog文件)
FROM pos 指定从哪个pos起始点开始查起(不指定就是从整个文件首个pos点开始算)
LIMIT [offset,] 偏移量(不指定就是0)
row_count 查询总条数(不指定就是所有行) 截取部分查询结果:
*************************** 20. row ***************************
Log_name: mysql-bin.000021 ----------------------------------------------> 查询的binlog日志文件名
Pos: 11197 ----------------------------------------------------------> pos起始点:
Event_type: Query ----------------------------------------------------------> 事件类型:Query
Server_id: 1 --------------------------------------------------------------> 标识是由哪台服务器执行的
End_log_pos: 11308 ----------------------------------------------------------> pos结束点:11308(即:下行的pos起始点)
Info: use `zyyshop`; INSERT INTO `team2` VALUES (0,345,'asdf8er5') ---> 执行的sql语句
*************************** 21. row ***************************
Log_name: mysql-bin.000021
Pos: 11308 ----------------------------------------------------------> pos起始点:11308(即:上行的pos结束点)
Event_type: Query
Server_id: 1
End_log_pos: 11417
Info: use `zyyshop`; /*!40000 ALTER TABLE `team2` ENABLE KEYS */
*************************** 22. row ***************************
Log_name: mysql-bin.000021
Pos: 11417
Event_type: Query
Server_id: 1
End_log_pos: 11510
Info: use `zyyshop`; DROP TABLE IF EXISTS `type`
  1. 指定查询 binlog.000002 日志
mysql> show binlog events in 'binlog.000002' limit 10;
+---------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+---------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| binlog.000002 | 4 | Format_desc | 1 | 124 | Server ver: 8.0.16, Binlog ver: 4 |
| binlog.000002 | 124 | Previous_gtids | 1 | 155 | |
| binlog.000002 | 155 | Anonymous_Gtid | 1 | 234 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000002 | 234 | Query | 1 | 482 | CREATE USER 'schwarzeni'@'localhost' IDENTIFIED WITH 'caching_sha2_password' AS '$A$005$H{;gmzB@[}K1i\nBcce80ezg8j3o0qDdYocc1OxBkShlQyzmOV/c4rGP69' /* xid=7 */ |
| binlog.000002 | 482 | Anonymous_Gtid | 1 | 561 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000002 | 561 | Query | 1 | 801 | CREATE USER 'cuishifeng'@'%' IDENTIFIED WITH 'caching_sha2_password' AS '$A$005$f8Zs\ZhY(9]HPTcaN83yCTNmHs/LQsa2DerCX.ZVgd4InrYiCpj75mA' /* xid=8 */ |
| binlog.000002 | 801 | Anonymous_Gtid | 1 | 878 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000002 | 878 | Query | 1 | 968 | FLUSH PRIVILEGES |
| binlog.000002 | 968 | Anonymous_Gtid | 1 | 1047 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000002 | 1047 | Query | 1 | 1256 | ALTER USER 'cuishifeng'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*10320381F36BE49A18F09B06A4BC005223975101' /* xid=12 */ |
+---------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)
  1. 指定查询 binlog.000002 这个文件,从pos点:968开始查起
mysql> show binlog events in 'binlog.000002' from 968 limit 10;
+---------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+---------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
| binlog.000002 | 968 | Anonymous_Gtid | 1 | 1047 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000002 | 1047 | Query | 1 | 1256 | ALTER USER 'cuishifeng'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*10320381F36BE49A18F09B06A4BC005223975101' /* xid=12 */ |
| binlog.000002 | 1256 | Anonymous_Gtid | 1 | 1333 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000002 | 1333 | Query | 1 | 1423 | flush privileges |
| binlog.000002 | 1423 | Anonymous_Gtid | 1 | 1500 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000002 | 1500 | Query | 1 | 1646 | GRANT ALL PRIVILEGES ON *.* TO 'cuishifeng'@'%' /* xid=70 */ |
| binlog.000002 | 1646 | Anonymous_Gtid | 1 | 1723 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000002 | 1723 | Query | 1 | 1813 | flush privileges |
| binlog.000002 | 1813 | Anonymous_Gtid | 1 | 1890 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| binlog.000002 | 1890 | Query | 1 | 1968 | FLUSH TABLES |
+---------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
10 rows in set (0.00 sec)

从日志中可以看出执行的mysql命令 并且有起始位置,对于恢复数据非常有用

  1. 查询第一个(最早)的binlog日志

mysql> show binlog events;

四、现在从binlog日志恢复数据

  1. 常用命令
恢复语法格式:
# mysqlbinlog mysql-bin.0000xx | mysql -u用户名 -p密码 数据库名
常用选项:
--start-position=953 起始pos点
--stop-position=1437 结束pos点
--start-datetime="2013-11-29 13:18:54" 起始时间点
--stop-datetime="2013-11-29 13:21:53" 结束时间点
--database=zyyshop 指定只恢复zyyshop数据库(一台主机上往往有多个数据库,只限本地log日志) 不常用选项:
-u --user=name Connect to the remote server as username.连接到远程主机的用户名
-p --password[=name] Password to connect to remote server.连接到远程主机的密码
-h --host=name Get the binlog from server.从远程主机上获取binlog日志
--read-from-remote-server Read binary logs from a MySQL server.从某个MySQL服务器上读取binlog日志 小结:实际是将读出的binlog日志内容,通过管道符传递给mysql命令。这些命令、文件尽量写成绝对路径;

日志恢复 相当于执行当时DDL语句,如果日志恢复的语句例如你库中存在某个表 日志又执行创建这个表 肯定是走不通的 所以最好指定具体位置恢复

  1. 查看binlog日志 确定从哪恢复
 mysql> show binlog events in 'mysql-bin.000023';

以下为末尾片段:
+------------------+------+------------+-----------+-------------+------------------------------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+------+------------+-----------+-------------+------------------------------------------------------------+
| mysql-bin.000023 | 922 | Xid | 1 | 953 | COMMIT /* xid=3820 */ |
| mysql-bin.000023 | 953 | Query | 1 | 1038 | BEGIN |
| mysql-bin.000023 | 1038 | Query | 1 | 1164 | use `zyyshop`; update zyyshop.tt set name='李四' where id=4|
| mysql-bin.000023 | 1164 | Xid | 1 | 1195 | COMMIT /* xid=3822 */ |
| mysql-bin.000023 | 1195 | Query | 1 | 1280 | BEGIN |
| mysql-bin.000023 | 1280 | Query | 1 | 1406 | use `zyyshop`; update zyyshop.tt set name='小二' where id=2|
| mysql-bin.000023 | 1406 | Xid | 1 | 1437 | COMMIT /* xid=3823 */ |
| mysql-bin.000023 | 1437 | Query | 1 | 1538 | drop database zyyshop |
+------------------+------+------------+-----------+-------------+------------------------------------------------------------+ 通过分析,造成数据库破坏的pos点区间是介于 1437--1538 之间,只要恢复到1437前就可。
mysqlbinlog  --start-position=953  --stop-position=1538 --database=zyyshop binlog.000002 | mysql -uroot -p123456 -v zyyshop
  1. 指定时间恢复
  • 知道自己在哪个时间段误操作了
mysql> drop table tt;

@ --start-datetime="2013-11-29 13:18:54"  起始时间点
@ --stop-datetime="2013-11-29 13:21:53" 结束时间点 # mysqlbinlog --start-datetime="2013-11-29 13:18:54" --stop-datetime="2013-11-29 13:21:53" --database=zyyshop binlog.000002 | mysql -uroot -p123456 -v zyyshop

希望这篇文章能够帮助误删操作的朋友顺利恢复数据

mysql数据恢复,binlog详解的更多相关文章

  1. mysql的binlog详解

    什么是binlogbinlog日志用于记录所有更新了数据或者已经潜在更新了数据(例如,没有匹配任何行的一个DELETE)的所有语句.语句以“事件”的形式保存,它描述数据更改. binlog作用因为有了 ...

  2. MySQL Binlog详解

    MySQL Binlog详解 Mysql的binlog日志作用是用来记录mysql内部增删改查等对mysql数据库有更新的内容的记录(对数据库的改动),对数据库的查询select或show等不会被bi ...

  3. MySQL数据库备份详解

    原文:MySQL数据库备份详解 对于任何数据库来说,备份都是非常重要的 数据库复制不能取代备份的作用 比如我们由于误操作,在主数据库上删除了一些数据,由于主从复制的时间很短,在发现时,从数据库上的数据 ...

  4. Mysql加锁过程详解(5)-innodb 多版本并发控制原理详解

    Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...

  5. MySQL日志功能详解

    MySQL日志功能详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.查询日志 它是用来保存所有跟查询相关的日志,这种日志类型默认是关闭状态的,因为MySQL的用户有很多,如果 ...

  6. MySQL数据库优化详解(收藏)

    MySQL数据库优化详解 mysql表复制 复制表结构+复制表数据mysql> create table t3 like t1;mysql> insert into t3 select * ...

  7. (转)MySQL备份原理详解

    MySQL备份原理详解 原文:http://www.cnblogs.com/cchust/p/5452557.html 备份是数据安全的最后一道防线,对于任何数据丢失的场景,备份虽然不一定能恢复百分之 ...

  8. 如何查看mysql数据库的引擎/MySQL数据库引擎详解

    一般情况下,mysql会默认提供多种存储引擎,你可以通过下面的查看: 看你的mysql现在已提供什么存储引擎:mysql> show engines; 看你的mysql当前默认的存储引擎:mys ...

  9. Mysql 三大特性详解

    Mysql 三大特性详解 Mysql Innodb后台线程 工作方式 首先Mysql进程模型是单进程多线程的.所以我们通过ps查找mysqld进程是只有一个. 体系架构 InnoDB存储引擎的架构如下 ...

随机推荐

  1. Hdu CRB and Queries(整体二分)

    CRB and Queries Time Limit: 6000 MS Memory Limit: 131072 K Problem Description There are N boys in C ...

  2. 04_Logstash安装

    Logstash部署 1.部署JDK环境 2.下载Logstash源码包 $ wget https://artifacts.elastic.co/downloads/logstash/logstash ...

  3. CRNN网络结构详解

    目录 一. CRNN概论 简介 网络 二. CRNN局部之特征提取 三. CRNN局部之BLSTM 四. CRNN局部之CTC 关于CTC是什么东西? CTC理论基础 五. 参考文献 一. CRNN概 ...

  4. WallpaperEngine 导入 mp4 文件出现 failed opening video

    WallpaperEngine 导入 mp4 文件出现 failed opening video 如图 下载这个插件 链接:点击打开链接 密码:dkf8

  5. commit 没有提交图片,但是出现了commit的修改

    .gitignore里面写上 image/cache/  就好了

  6. 正确处理listview的position

    当ListView包含有HeaderView或FooterView时,传入getView或者onItemClick的position是怎样的,这是个值得探讨的问题 先列出错误的用法 定义: priva ...

  7. Git 工作流

    一.分类 1.集中式工作流 像 SVN 一样,集中式工作流以中央仓库作为项目所有修改的单点实体.所有修改都提交到 Master 这个分支上. 这种方式与 SVN 的主要区别就是开发人员有本地库.Git ...

  8. LeetCode31 Next Permutation and LeetCode60 Permutation Sequence

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. Java--常用API介绍

    Scanner类--键盘输入,室友起来三个步骤: 第一,导包:import java.util.Scanner 第二,创建:Scanner sc = new Scanner(System.in) 第三 ...

  10. createElement与createDocumentFragment的一些小区别

    在DOM操作里,createElement是创建一个新的节点,createDocumentFragment是创建一个文档片段. 网上可以搜到的大部分都是说使用createDocumentFragmen ...