诗子王 發表於 2025-10-29 15:38:00

MySQL统计分析binlog的数量与大小

<h1 id="脚本介绍">脚本介绍</h1>
<p>脚本mysql_binlog_gen_sum的功能与用途如下所示:</p>
<ol>
<li>按天统计MySQL的binlog的生成数量(个数)与大小(Size)信息.用于数据分析.</li>
<li>按小时统计MySQL的binlog的生成数量(个数)与大小(Size)信息.用于数据分析.</li>
</ol>
<p>写这个脚本的初衷是想实现类似Oracle数据库中统计分析归档日志生成的大小与数量的功能,奈何MySQL数据库中没有这些系统视图和系统表.无法用SQL实现,所以写了这个小脚本实现这个功能.</p>
<h1 id="版本说明">版本说明</h1>
<pre><code class="language-bash">#***************************************************************************************#
# Version      Modified Date            Description                                 #
#***************************************************************************************#
# V.1.0          2024-05-10            创建此脚本                                        #
# V.1.1          2025-10-11            完善脚本,改进部分功能                            #
# V.1.2          2025-10-22            Fix掉统计输出部分数据没有按天排序的问题          #
#########################################################################################
</code></pre>
<h1 id="参数说明">参数说明</h1>
<table>
<thead>
<tr>
<th>参数</th>
<th>长参数</th>
<th>是否强制参数</th>
<th>参数说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>-p</td>
<td>--path</td>
<td>yes</td>
<td>MySQL binlog的路径.</td>
</tr>
<tr>
<td>-b</td>
<td>--base_name</td>
<td>yes</td>
<td>MySQL binlog的basename</td>
</tr>
<tr>
<td>-t</td>
<td>--type</td>
<td>no</td>
<td>按天/小时/All统计输出,默认按天统计</td>
</tr>
<tr>
<td>-u</td>
<td>--unit</td>
<td>no</td>
<td>MySQL binlog的大小按GB或MB或KB方式输出,默认按MB输出</td>
</tr>
<tr>
<td>-v</td>
<td>--version</td>
<td>no</td>
<td>查看脚本版本信息</td>
</tr>
<tr>
<td>-h</td>
<td>--help</td>
<td>no</td>
<td>查看脚本帮助信息</td>
</tr>
</tbody>
</table>
<h1 id="脚本使用">脚本使用</h1>
<h2 id="脚本授权">脚本授权</h2>
<p>脚本授权</p>
<pre><code class="language-bash">chmod +x mysql_binlog_gen_sum
</code></pre>
<h2 id="脚本示例">脚本示例</h2>
<p>例子如下所示:</p>
<ol>
<li>查看帮助信息</li>
</ol>
<pre><code class="language-bash">$ ./mysql_binlog_gen_sum --help
Usage: mysql_binlog_gen_sum -p PATH_NAME-bBASE_NAME[-t OUT_TYPE] [-u UNIT_TYPE] [-v] [-h]
       -p = Mandatory parameters. The path of the mysql binlog
       -b = Mandatory parameters. The basename of binlog
       -t = Optional parameter. the report out by day|hour|all
       -u = Optional parameter. the binlog size output unit(GB|MB|KB)
       -h = Optional parameter.help mode.
       -v = Optional parameter.shell version. Display the version information of the current script.
</code></pre>
<ol start="2">
<li>按天统计MySQL的binlog的生成个数与大小信息.用于数据分析.</li>
</ol>
<pre><code class="language-bash"># ./mysql_binlog_gen_sum -p /data/mysql/logs-b mysql-bin
-----------------------------------------------------------------------------
下面数据是按天统计MySQL的binlog产生的数量,大小,以及每小时平均产生binlog的大小
-----------------------------------------------------------------------------
Date         Files    Total (MB)      PerHour (MB)
------------------------------------------------------------
2025-10-22   15       4658.74         194.11   
2025-10-23   29       8701.05         362.54   
2025-10-24   28       8401.67         350.07   
2025-10-25   28       8401.66         350.07   
2025-10-26   28       8401.75         350.07   
2025-10-27   27       8101.22         337.55   
2025-10-28   28       8401.90         350.08   
2025-10-29   18       5328.11         222.00
</code></pre>
<ol start="3">
<li>按天统计MySQL的binlog的生成个数与大小信息(文件大小按GB统计).用于数据分析.</li>
</ol>
<pre><code># ./mysql_binlog_gen_sum -p /data/mysql/logs-b mysql-bin -u GB
-----------------------------------------------------------------------------
下面数据是按天统计MySQL的binlog产生的数量,大小,以及每小时平均产生binlog的大小
-----------------------------------------------------------------------------
Date         Files    Total (GB)      PerHour (GB)
------------------------------------------------------------
2025-10-22   15       4.55            0.19      
2025-10-23   29       8.50            0.35      
2025-10-24   28       8.20            0.34      
2025-10-25   28       8.20            0.34      
2025-10-26   28       8.20            0.34      
2025-10-27   27       7.91            0.33      
2025-10-28   28       8.20            0.34      
2025-10-29   18       5.21            0.22
</code></pre>


</div>
<div id="MySignature" role="contentinfo">
    <div id="KerryCodeSignature">
<div>
<img src="https://images.cnblogs.com/cnblogs_com/kerrycode/1913302/o_240731062102_kerrycode.png" height="120" width="500">
</div>
<div>
<b>扫描上面二维码关注我</b>
</div>
<div>如果你真心觉得文章写得不错,而且对你有所帮助,那就不妨帮忙“推荐"一下,您的“推荐”和”打赏“将是我最大的写作动力!</div>
<div>本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.</div>
</div><br><br>
来源:https://www.cnblogs.com/kerrycode/p/19174590
頁: [1]
查看完整版本: MySQL统计分析binlog的数量与大小