MySQL09-binlog日志

binlog日志

查看有效的binlog日志(正在用和历史的)
show binary logs;

查看当前正在使用的binlog日志文件
show master status;

查看是否开启binlog,查看binlog的存储路径
show variables like 'log_bin%';

查看binlog中的全部事件
show binlog events in 'mysql-bin.000001';

查看指定偏移量之后的binlog事件
show binlog events in 'mysql-bin.00001' from 244;

查看binlog日志的内容
mysqlbinlog /var/log/mysql/mysql-bin.000001

查看指定数据库的binlog日志的内容
mysqlbinlog -d=mydatabase mysql-bin.000001

查看当前binlog日志的过期时间
show variables like '%binlog_expire_logs_seconds';

生成新的bin_log日志(重启mysql也能重新生成)
flush logs;

删除指定日志编号之前的日志文件
purge master logs to 'mysql-bin.000001';

删除日志为'yyyy-mm-dd hh24:mi:ss'之前产生的所有日志
purge master logs before 'yyyy-mm-dd hh:mm:ss';

删除所有binlog日志,重建新日志
reset master;

配置binlog日志

cat /etc/my.cnf
[mysqld]
# 开启binlog日志
server-id=1                          # 服务器id,要求id唯一
log-bin=/var/log/mysql/mysql-bin     # 开启并指定日志路径和名称,路径的所有者和所有组为mysql,需手动创建

# 指定binlog日志文件的大小
max_binlog_size=100m

# 设置binlog日志的过期时间
expire_logs_days=15 (命令行设置 set global expire_logs_days=15

binlog日志文件记录格式

statement:基于sql语句的日志记录,记录修改数据的sql语句
row:基于行的日志记录,记录被修改的行和修改后的结果(默认格式)
mixed:记录以上两种格式


# 修改binlong日志文件的记录格式
vim /etc/my.cnf
binlog_format=mixed

# 查看当前binlog日志的格式
show variables like 'binlog_format';

mysqlbinlog工具

mysqlbinog [选项] binlog日志文件名

mysqlbinlog --no-defaults \
--database=db \
--base64-output=decode-rows -v \
--start-datetime='2019-04-11 00:00:00' \
--stop-datetime='2019-04-11 15:00:00'  mysql-bin.000007
> /tmp/binlog.sql

--start-position=偏移量     # 指定起始偏移量选项
--stop-position=偏移量      # 指定结束偏移量选项
--start-time='yyyy-mm-dd hh:mm:ss'   # 指定起始时间选项
--stop-time='yyyy-mm-dd hh:mm:ss'    # 指定结束时间选项
--base64-output=decode-rows -v       # 转换日志中的乱码

查看binlog日志文件

show binlog events in 'mysql-bin.000001';   # 查询所有数据
posted @   立勋  阅读(20)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示