当前位置:网站首页>mysql ,binlog 日志查询
mysql ,binlog 日志查询
2022-04-23 04:18:00 【猎人在吃肉】
文章目录
一、开启binlog
在 mysql 配置文件 my.cnf 中,开启 log-bin 功能,log-bin=mysql-bin
确认是打开状态。
[mysqld]
log-bin=mysql-bin
说明,值 mysql-bin 是日志的基本名或前缀名。
二、通过mysql的变量配置表,查看二进制日志是否已开启
mysql> show variables like 'log%';
+----------------------------------------+--------------------------------------+
| Variable_name | Value |
+----------------------------------------+--------------------------------------+
| log_bin | ON | ---> ON,表示已经开启binlog日志
| log_bin_basename |/usr/local/mysql/data/mysql-bin | ---> binlog日志存储位置
| log_bin_index |/usr/local/mysql/data/mysql-bin.index | ---> binlog日志的格式
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| log_builtin_as_identified_by_password | OFF |
| log_error | /data1/mysql/logs/error.log |
| log_error_verbosity | 3 |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | ON |
| log_slow_admin_statements | OFF |
| log_slow_slave_statements | OFF |
| log_statements_unsafe_for_binlog | ON |
| log_syslog | OFF |
| log_syslog_facility | daemon |
| log_syslog_include_pid | ON |
| log_syslog_tag | |
| log_throttle_queries_not_using_indexes | 0 |
| log_timestamps | UTC |
| log_warnings | 2 |
+----------------------------------------+--------------------------------------+
三、binlog日志内容查看
1、mysql 查看 binlog 命令
mysql> show binlog events; # 只查看第一个 binlog 文件的内容
mysql> show binlog events in 'mysql-bin.000002'; # 查看指定 binlog 文件的内容
mysql> show binary logs; # 获取 binlog 文件列表
mysql> show master status; # 查看当前正在写入的 binlog 文件
2、找到 mysqlbinlog 命令
mysqlbinlog 是一个查看 mysql 二进制日志的工具,可以把 mysql 上面的所有操作记录从日志里导出,这个工具默认的安装路径为:/usr/local/mysql/bin/mysqlbinlog
。
可以通过命令查找 mysqlbinlog 的工具路径:
find / -name "mysqlbinlog"
3、使用 mysqlbinlog 命令
bin log
是二进制文件,二进制文件更省空间,写入速度更快,是无法直接打开来查看的。
(1) 如果 log-bin 文件比较小,可以直接查看日志:
/usr/local/mysql/bin/mysqlbinlog --no-defaults /usr/local/mysql/data/mysql-bin.0007
--no-defaults
可以省略,参考 下面的mysqlbinlog 异常
说明。
(2) 将log-bin解析后的日志转入到指定文件
/usr/local/mysql/bin/mysqlbinlog --no-defaults/usr/local/mysql/data/mysql-bin.0007 > /tmp/mysql_bin_07.sql
(3)基于开始和结束时间:
/usr/local/mysql/bin/mysqlbinlog --start-datetime="2021-10-01 00:00:00" --stop-datetime="2021-10-21 23:59:59" /usr/local/mysql/data/mysql-bin.0007 -r test2.sql
(4)增加过滤,只查询 insert、update、delete 的语句,使用用more来查看:
/usr/local/mysql/bin/mysqlbinlog --no-defaults --database=raceEnroll mysql-bin.000007 |grep update | more
4、mysqlbinlog 异常
直接 执行 /usr/local/mysql/bin/mysqlbinlog mysql-bin.000007
日志时,可能会报如下错误:
mysql> /usr/local/mysql/bin/mysqlbinlog mysql-bin.000007
mysqlbinlog: [ERROR] unknown variable 'default-character-set=utf8mb4'
mysqlbinlog: [ERROR] unknown variable 'default-character-set=utf8'
原因:
mysqlbinlog 这个工具无法识别 binlog 中的配置中的 default-character-set=utf8mb4
这个指令。
解决方法:
添加 --no-defaults
参数,即: /usr/local/mysql/bin/mysqlbinlog --no-defaults mysql-bin.000007
。
四、常用的Binlog操作命令
# 是否启用binlog日志
show variables like 'log_bin';
# 查看详细的日志配置信息
show global variables like '%log%';
# mysql数据存储目录
show variables like '%dir%';
# 查看binlog的目录
show global variables like "%log_bin%";
# 查看当前服务器使用的biglog文件及大小
show binary logs;
# 查看主服务器使用的biglog文件及大小
# 查看最新一个binlog日志文件名称和Position
show master status;
# 事件查询命令
# IN 'log_name' :指定要查询的binlog文件名(不指定就是第一个binlog文件)
# FROM pos :指定从哪个pos起始点开始查起(不指定就是从整个文件首个pos点开始算)
# LIMIT [offset,] :偏移量(不指定就是0)
# row_count :查询总条数(不指定就是所有行)
show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];
# 查看 binlog 内容
show binlog events;
# 查看具体一个binlog文件的内容 (in 后面为binlog的文件名)
show binlog events in 'master.000003';
# 设置binlog文件保存事件,过期删除,单位天
set global expire_log_days=3;
# 删除当前的binlog文件
reset master;
# 删除slave的中继日志
reset slave;
# 删除指定日期前的日志索引中binlog日志文件
purge master logs before '2019-03-09 14:00:00';
# 删除指定日志文件
purge master logs to 'master.000003';
版权声明
本文为[猎人在吃肉]所创,转载请带上原文链接,感谢
https://xiaojin21cen.blog.csdn.net/article/details/121471123
边栏推荐
- Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
- Go 语言中的 logger 和 zap 日志库
- 【Pytorch基础】torch.split()用法
- 单片机串口数据处理(2)——uCOSIII+循环队列接收数据
- [AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
- How Zotero quotes in word jump to references / hyperlink
- 智能电子秤全国产化电子元件推荐方案
- Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
- [echart] démarrer avec echart
- Shopping mall for transportation tools based on PHP
猜你喜欢
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
Bacterial infection and antibiotic use
【测绘程序设计】坐标方位角推算神器(C#版)
STM32F4单片机ADC采样及ARM-DSP库的FFT
Common string processing functions in C language
AI CC 2019 installation tutorial under win10 (super detailed - small white version)
Cuda11 is installed perfectly in win10 X + pytorch 1.9 (blood flowing into the river) cuda. is_ Available() becomes true!
[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
Does China Mobile earn 285 million a day? In fact, 5g is difficult to bring more profits, so where is the money?
VSCode配置之Matlab极简配置
随机推荐
【测绘程序设计】坐标方位角推算神器(C#版)
Cause analysis of incorrect time of AI traffic statistics of Dahua Equipment Development Bank
【Pytorch基础】torch.split()用法
Set经典小题目
Man's life
Iron and intestinal flora
Overview of knowledge map (II)
/etc/bash_completion.d目录作用(用户登录立刻执行该目录下脚本)
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
The difference between lists, tuples, dictionaries and collections
[AI vision · quick review of NLP natural language processing papers today, issue 29] Mon, 14 Feb 2022
无线充电全国产化电子元件推荐方案
MYSQL去重方法汇总
Nature medicine reveals individual risk factors of coronary artery disease
A function second kill 2sum 3sum 4sum problem
UDP protocol and TCP protocol
The whole process of connecting the newly created unbutu system virtual machine with xshell and xftp
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
MYSQL50道基础练习题
AI CC 2019 installation tutorial under win10 (super detailed - small white version)