mos login mysql_你会分析 MySQL 的 Binlog 日志吗,看完这篇文章你就可以了如指掌呢!...

公众号关注 「运维之美」设为「星标」,每天带你玩转 Linux !

ce7b88eb784aea1ea92eea4a500144c6.png

需求:某业务 MySQL 迁移,但是迁移前需要做如下准备工作。

统计各个业务表的 DML 操作情况。

统计各个业务表的最后访问时间。

条件:

60 min 一个 1GB 的 Binlog。

Binlog 保留一个月。

如果你遇到这么个需求,你该如何着手分析呢?反正我面对这个需求的时候第一个想到的就是写脚本,让脚本自动分析。今天我们就来讲讲如何用 analysis_binlog 来快速进行 MySQL 的 Binlog 分析。

analysis_binlog 介绍

一个可以快速分析 MySQL Binlog 的工具,现有如下功能:

基于业务表分析统计各个表的 DML 的次数。

统计各个业务表的最后访问时间。

统计各表 DML 数量和总的 DML数量。

统计该 Binlog 的事务总数。

基于业务表的 Binlog to SQL (功能已实现,但还在测试阶段)。

其他功能敬请期待。

DML (Data Manipulation Language) 数据操纵语言,主用来操作数据库中的表对象,主要包括的操作有:SELECT、INSERT、UPDATE、DELETE。

项目地址:https://gitee.com/mo-shan/analysis_binlog

analysis_binlog 安装# 克隆项目

$ git clone https://gitee.com/mo-shan/analysis_binlog.git

$ cd analysis_binlog

# 配置 mysqlbinlog 路径(第一次使用需要配置)

# 更改 mysqlbinlog 命令所在位置

# 将这里的 mysqlbinlog_path 改成 mysqlbinlog 工具的绝对路径,否则可能会因版本太低导致错误。

$  sed -i 's/mysqlbinlog=.*/mysqlbinlog=\"/mysqlbinlog_path\"/g' bin/analysis_binlog

# 更改 analysis_binlog 命令所在位置

# 将这里的 analysis_binlog_path 改成 analysis_binlog 的所在目录的绝对路径。

$  sed -i 's/work_dir=.*/work_dir=\"/analysis_binlog_path\"/g' bin/analysis_binlog

# 为 analysis_binlog 配置环境变量(可选)

$  chmod +x bin/analysis_binlog

$ echo "export PATH=$(pwd)/bin:${PATH}" >> ${HOME}/.bashrc

一个完整的安装示例$ git clone https://gitee.com/mo-shan/analysis_binlog.git

$ cd analysis_binlog

$ sed -i 's#mysqlbinlog=.*#mysqlbinlog=\"/usr/local/mysql/bin/mysqlbinlog\"#g' bin/analysis_binlog #配置 mysqlbinlog 工具的路径,否则可能会因版本太低导致错误。

$ sed -i 's#work_dir=.*#work_dir=\"/home/moshan/analysis_binlog\"#g' bin/analysis_binlog              #配置 analysis_binlog 所在目录。

$ chmod +x bin/analysis_binlog #添加可执行权限。

$ echo "export PATH=/home/moshan/analysis_binlog/bin:${PATH}" >> ${HOME}/.bashrc #配置环境变量。

analysis_binlog 语法介绍$ analysis_binlog -h

Usage: bash analysis_binlog [OPTION]...

--type=value or -t=value The value=detail | simple

For example: --type=detail,-t=detail,-t=simple,-t=simple,

The "detail": The results displayed are more detailed, but also take more time.

The "simple": The results shown are simple, but save time

The default value is "simple".

--binlog-dir or -bdir Specify a directory for the binlog dir.

For example: --binlog-dir=/mysql_binlog_dir,-bdir=/mysql_binlog_dir

If the input is a relative path, it will be automatically modified to an absolute path.

The default value is "Current path".

--binlog-file or -bfile Specify a file for the binlog file, multiple files separated by ",".

For example: --binlog-file=/path/mysql_binlog_file,-bfile=/path/mysql_binlog_file

--b-file=/path/mysql_binlog_file1,/path/mysql_binlog_file1

If the input is a relative path, it will be automatically modified to an absolute path.

If this parameter is used, the "--binlog-dir or -bdir" parameter will be invalid.

--sort or -s Sort the results for "INSERT | UPDATE | DELETE | Total"

The value=insert | update | delete | total

The default value is "insert".

--threads or -w Decompress/compress the number of concurrent. For example:--threads=8

This parameter works only when there are multiple files.

If you use this parameter, specify a valid integer, and the default value is "1".

--help or -h Display this help and exit.

主要参数说明:

-bfile: 指定 binlog 文件, 支持多个文件并行分析, 多个文件用逗号相隔, 需要并行分析时请结合 -w 参数使用。

-w : 指定并行数, 当需要分析多个 binlog 文件时该参数有效, 默认是 1。

-t : 指定显示结果的格式/内容, 可供使用的选项有 "detail|simple". 当指定 detail的时候结果较为详细, 会打印详细的分析过程, 消耗时间也不直观, simple只做了统计工作。

-s : 指定排序规则, 可供使用的选项有 "insert|update|delete". 默认会把统计结果做一个排序, 按照表的维度统计出 insert update delete 的次数, 并按照次数大小排序(默认 insert)。

analysis_binlog 使用实例

已配置环境变量$ analysis_binlog -bfile=/data/mysql/binlog/3306/mysql-bin.000798,/data/mysql/binlog/3306/mysql-bin.000799 -w=2 -t=simple -s=update

未配置环境变量$ bash bin/analysis_binlog -bfile=/data/mysql/binlog/3306/mysql-bin.000798,/data/mysql/binlog/3306/mysql-bin.000799 -w=2 -t=simple -s=update

分析结果查询

分析完毕会在 analysis_binlog 家目录下的 res 目录下保存一个 [binlog_file_name.res] 文件,使用文本工具打开即可, 建议使用 cat, tail, more, 如下结果展示, 会按照表的维度做个统计, 然后按照 update 的次数排序, Last Time 表示该表的最后一次操作。

$ cat mysql-bin.000798.res

Table Last Time Insert(s) Update(s) Delete(s)

moshan.flush_ 190311 9:28:54 0 3475 0

ultrax.dis_common_syscache 190312 11:31:53 0 231 0

ultrax.dis_common_cron 190312 11:31:53 0 194 0

ultrax.dis_common_session 190312 10:38:56 6 170 5

ultrax.dis_forum_forum 190312 9:19:10 0 129 0

moshan.money 190311 9:28:37 29 80 0

ultrax.dis_common_onlinetime 190312 10:38:42 0 48 0

ultrax.dis_forum_thread 190312 10:38:56 4 47 0

ultrax.dis_common_member_count 190312 10:38:53 0 47 0

ultrax.dis_common_credit_rule_log 190312 10:38:53 0 38 0

ultrax.dis_forum_post 190312 9:24:30 4 34 0

ultrax.dis_common_member_status 190312 9:04:42 0 20 0

moshan.history_ 190308 9:28:25 0 10 0

ice_db.server_setting_tmp 190304 10:34:19 564 8 0

ultrax.dis_common_process 190312 11:31:53 201 7 201

ultrax.dis_common_setting 190312 9:04:42 0 7 0

moshan.tmp_table 190304 17:17:21 0 7 0

ultrax.dis_ucenter_failedlogins 190306 10:07:11 0 4 0

ultrax.dis_common_member_field_home 190311 14:54:47 0 4 0

ultrax.dis_forum_threadcalendar 190312 9:09:56 2 2 0

ultrax.dis_forum_attachment 190306 11:46:56 2 2 0

moshan.use_date 190304 17:12:22 0 1 0

ultrax.dis_forum_threadhot 190312 9:09:56 4 0 0

ultrax.dis_forum_threaddisablepos 190311 14:54:47 1 0 0

ultrax.dis_forum_statlog 190312 9:04:42 304 0 0

ultrax.dis_forum_sofa 190311 14:54:47 4 0 0

ultrax.dis_forum_post_tableid 190311 14:54:47 4 0 0

ultrax.dis_forum_newthread 190311 14:54:47 4 0 6

ultrax.dis_forum_attachment_unused 190306 11:46:56 2 0 2

ultrax.dis_forum_attachment_8 190306 11:46:56 1 0 0

ultrax.dis_forum_attachment_0 190306 11:46:29 1 0 0

ultrax.dis_common_statuser 190311 11:40:44 4 0 4

ultrax.dis_common_searchindex 190312 10:38:53 28 0 0

ultrax.dis_common_member_action_log 190311 14:54:47 4 0 4

test.ttt 190303 11:43:36 2 0 0

test.t_test 190308 16:52:35 4 0 0

test.t_message_list 190313 9:30:16 307544 0 0

test.t_message_content_lately 190313 9:30:16 307544 0 0

test.admin_user 190308 11:51:50 3 0 3

Trans(total) Insert(s) Update(s) Delete(s)

312619 616270 4565 225

root /data/git/analysis_binlog/res >>

测试过程发现,一个 1GB 大小的 Binlog,大概需要 2min 左右即可。来源:gitee原文:http://t.cn/Ear3EHu题图:来自谷歌图片搜索版权:本文版权归原作者所有投稿:欢迎投稿,投稿邮箱: editor@hi-linux.com

2970510832274d37ea8855310cfb39e5.png

a7203eccb046b6b933d3aa81c37383a8.png

你可能还喜欢

点击下方图片即可阅读

ed4618aaef96757cc1b800af1d39f7d0.png

假如服务器上没有 Docker 环境,你还能愉快的拉取容器镜像吗?

f36f954489b98d3cc2531226972ac713.png

点击上方图片,打开小程序,加入「玩转 Linux」圈子

d7a440aa79ba2ed7a4023a193fd62198.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值