工具分享-通过开源工具 tuning-primer快速巡检MySQL5.7
<h1 id="工具分享-通过开源工具-tuning-primer快速巡检mysql57">工具分享-通过开源工具 tuning-primer快速巡检MySQL5.7</h1><h2 id="简介">简介</h2>
<p>今天推荐一个开源的调优脚本tuning-primer.sh,这是一个非常实用的性能调优工具,能够快速检查 MySQL 的运行状态,生成详细的性能报告,并提供优化建议。通过这个脚本,可以更高效地完成调优工作,节省大量的时间和精力。</p>
<h2 id="使用场景">使用场景</h2>
<ol>
<li>快速识别 MySQL服务器的性能瓶颈,如缓存大小、连接数、查询缓存等,并根据建议进行调整</li>
<li>集成到定期任务中(如使用 cron 作业),定期生成性能报告,帮助持续监控和优化 MySQL服务器的性能</li>
</ol>
<h2 id="tuning-primersh功能">tuning-primer.sh功能</h2>
<p>性能分析:检查 MySQL 服务器的运行状态,包括查询缓存、慢查询日志、InnoDB 配置等。</p>
<p>优化建议:根据分析结果,提供调整服务器配置等建议</p>
<p>自动检测:自动读取 MySQL配置文件,并尝试使用其中的登录信息</p>
<p>目前,支持检测和优化建议的内容:</p>
<table>
<thead>
<tr>
<th>建议类别</th>
<th>描述</th>
<th>备注</th>
</tr>
</thead>
<tbody>
<tr>
<td>慢查询日志 (Slow Query Log)</td>
<td>处理有关慢查询日志的建议。</td>
<td>确保日志记录有效查询,优化性能。</td>
</tr>
<tr>
<td>最大连接数 (Max Connections)</td>
<td>处理有关数据库最大连接数的建议。</td>
<td>避免过多连接导致资源耗尽。</td>
</tr>
<tr>
<td>工作线程 (Worker Threads)</td>
<td>处理有关数据库工作线程的建议。</td>
<td>优化线程数提高并发处理能力。</td>
</tr>
<tr>
<td>键缓冲区 (Key Buffer )</td>
<td>提供有关 MyISAM 引擎键缓冲区大小的建议。</td>
<td>仅适用于 MyISAM 引擎。</td>
</tr>
<tr>
<td>查询缓存 (Query Cache)</td>
<td>处理有关查询缓存大小和效率的建议。</td>
<td>提高缓存效率减少查询延迟。</td>
</tr>
<tr>
<td>排序缓冲区 (Sort Buffer)</td>
<td>提供有关排序操作使用的缓冲区大小的建议。</td>
<td>优化排序操作性能。</td>
</tr>
<tr>
<td>连接 (Joins)</td>
<td>提供有关 JOIN 操作和连接缓冲区大小的建议。</td>
<td>提高连接效率。</td>
</tr>
<tr>
<td>临时表 (Temp Tables)</td>
<td>处理有关临时表的使用和配置的建议。</td>
<td>优化临时表性能。</td>
</tr>
<tr>
<td>表缓存 (Table (Open & Definition) Cache)</td>
<td>提供有关表的打开缓存和定义缓存的建议。</td>
<td>提高缓存效率减少磁盘 I/O。</td>
</tr>
<tr>
<td>表锁定 (Table Locking)</td>
<td>处理有关表锁定和并发访问控制的建议。</td>
<td>确保数据一致性。</td>
</tr>
<tr>
<td>表扫描 (Table Scans)</td>
<td>提供有关全表扫描操作和读取缓冲区的建议,仅限 MyISAM。</td>
<td>优化全表扫描性能。</td>
</tr>
<tr>
<td>InnoDB 状态 (InnoDB Status)</td>
<td>提供有关 InnoDB 存储引擎的状态和配置的建议。</td>
<td>优化 InnoDB 性能。</td>
</tr>
</tbody>
</table>
<h2 id="使用步骤">使用步骤</h2>
<h3 id="安装与准备">安装与准备</h3>
<p>脚本的安装十分简单,只需要在下面项目地址中下载即可使用。</p>
<p>访问:tuning-primer开源项目地址</p>
<p>下载完成之后就可以使用,使用前需要对脚本进行赋权。</p>
<pre><code class="language-Bash">$ chmod +x tuning-primer.sh
</code></pre>
<p>查看该脚本,能够看到需要配置的MySQL的socket路径。</p>
<pre><code class="language-Bash">$ cat tuning-primer.sh
#!/bin/bash
# vim: sw=2:et
#########################################################################
# #
# MySQL performance tuning primer script #
# Written by: Matthew Montgomery and Dan Reif #
# Report bugs to: https://github.com/BMDan/tuning-primer.sh/issues#
# Inspired by: MySQLARd (http://gert.sos.be/demo/greatdbar/) #
# Version: 1.99 Released: 2018-06-10 #
# Licenced under GPLv2 #
# #
#########################################################################
#########################################################################
# #
# Usage: ./tuning-primer.sh [ mode ] #
# #
# Available Modes: #
# all : perform all checks (default) #
# prompt : prompt for login credentials and socket #
# and execution mode #
# mem, memory : run checks for tunable options which #
# affect memory usage #
# disk, file : run checks for options which affect #
# i/o performance or file handle limits #
# innodb : run InnoDB checks /* to be improved */#
# misc : run checks that don't fit categories #
# well Slow Queries, Binary logs, #
# Used Connections and Worker Threads #
#########################################################################
# #
# Set this socket variable ONLY if you have multiple instances running#
# or we are unable to find your socket, and you don't want to to be #
# prompted for input each time you run this script. #
# #
#########################################################################
socket=/MySQL/dbdata/1100/data/MySQL.sock
</code></pre>
<p>先用./tuning-primaer.sh help 查看下帮助。</p>
<pre><code class="language-Bash">$./tuning-primer.sh help
usage: ./tuning-primer.sh [ all | banner | file | innodb | memory | misc | prompt ]
</code></pre>
<p>可以看到支持以上集中检查。</p>
<p>运行脚本: 直接执行脚本进行调优分析。</p>
<pre><code class="language-Bash">$./tuning-primer.sh
</code></pre>
<h3 id="运行结果">运行结果</h3>
<p>运行结果包含多项指标如缓存,日志等方面信息,以下为部分结果:</p>
<h4 id="slow-queries">SLOW QUERIES</h4>
<p>判断当前慢查询日志启用状态,获取当前设置的阈值,统计系统慢查询的总次数以及给出慢查询阈值的优化建议。</p>
<pre><code class="language-Bash">SLOW QUERIES
The slow query log is enabled.
Current long_query_time = 10.000000 sec.
Since startup, 14 out of 5155 queries have taken longer than <long_query_time-when-they-were-executed> to complete.
Your long_query_time may be too high, I typically set this under 5 sec.
</code></pre>
<h4 id="binary-update-log">BINARY UPDATE LOG</h4>
<p>检查Binlog相关参数,根据当前的MySQL给出相应的优化建议。</p>
<pre><code class="language-Bash">BINARY UPDATE LOG
The binary update log is enabled
The expire_logs_days is not set.
The mysqld will retain the entire binary log until RESET MASTER or PURGE MASTER LOGS commands are run manually
Setting expire_logs_days will allow you to remove old binary logs automatically
</code></pre>
<p>可以看到,MySQL未设置expire_logs_days,给出提示使用 RESET MASTER or PURGE MASTER LOGS来清理旧的日志。</p>
<h4 id="max-connections">MAX CONNECTIONS</h4>
<p>总结MySQL数据库的连接使用情况的摘要。给出服务器的优化建议,由于当前使用率低于10%,可以考虑降低<code>max_connections</code>的值,以避免内存的过度分配。</p>
<pre><code class="language-Bash">Current max_connections = 151
Current threads_connected = 3
Historic max_used_connections = 8
The number of used connections is 5% of the configured maximum.
You are using less than 10% of your configured max_connections.
Lowering max_connections could help to avoid an over-allocation of memory
See "MEMORY USAGE" section to make sure you are not over-allocating
</code></pre>
<h4 id="open-files-limit">OPEN FILES LIMIT</h4>
<p>对打开文件限制进行巡检调优。</p>
<pre><code class="language-Bash">Current open_files_limit = 1024000 files
The open_files_limit should typically be set to at least 2x-3x
that of table_cache if you have heavy MyISAM usage.
Your open_files_limit value seems to be fine
</code></pre>
<h4 id="innodb-status">INNODB STATUS</h4>
<p>以下巡检关于 InnoDB 存储引擎的索引和数据空间使用情况的摘要和建议。包括当前索引空间,InnoDB数据空间以及缓冲池配置等。</p>
<pre><code class="language-Bash">Current InnoDB index space = 202 M
Current InnoDB data space = 1.20 G
Current InnoDB buffer pool free = 96 %
Current innodb_buffer_pool_size = 9.00 G
Depending on how much space your innodb indexes take up it may be safe
to increase this value to up to 2 / 3 of total system memory
</code></pre>
<h3 id="其他检查项">其他检查项</h3>
<p>如MEMORY USAGE、TABLE CACHE、TEMP TABLES、TABLE SCANS、TABLE LOCKING等</p>
<pre><code class="language-Bash">MEMORY USAGE
Max Memory Ever Allocated : 9.52 G
Configured Max Per-thread Buffers : 287 M
Configured Max Global Buffers : 9.50 G
Configured Max Memory Limit : 9.78 G
Physical Memory : 15.51 G
Max memory limit seem to be within acceptable norms
TABLE CACHE
Current table_open_cache = 4000 tables
Current table_definition_cache = 2000 tables
You have a total of 171 tables
You have 728 open tables.
The table_cache value seems to be fine
TEMP TABLES
Current max_heap_table_size = 16 M
Current tmp_table_size = 16 M
Of 748 temp tables, 3% were created on disk
Created disk tmp tables ratio seems fine
TABLE SCANS
Current read_buffer_size = 128 K
Current table scan ratio = 172 : 1
read_buffer_size seems to be fine
TABLE LOCKING
Current Lock Wait ratio = 0 : 5419
Your table locking seems to be fine
</code></pre>
<h2 id="总结">总结</h2>
<p>通过这个开源脚本工具,在 MySQL 服务器上运行此程序能够快速进行性能方面的检查以及给出一定的参考建议,帮助找出并修正潜在的问题点。</p>
<p>tuning-primer.sh特点</p>
<ol>
<li>易于使用:脚本设计简单,用户通过一条命令即可启动整个调优流程,无论是经验丰富的数据库管理员还是新手都能轻松使用。</li>
<li>定制化:该脚本提供了一定程度的自定义选项,允许用户根据自己的具体需求调整脚本的默认设置。</li>
<li>全面:脚本覆盖了数据库调优的多个方面,包括内核参数、文件系统、安全性设置和性能监控等。</li>
<li>社区支持:作为一个开源项目,拥有一个活跃的开发者社区,不断进行维护和更新,确保脚本的功能持续完善和更新。</li>
<li>本地运行:目前该脚本仅支持在本地运行,无法远程连接到数据库服务器进行巡检调优。</li>
</ol>
<hr>
<p>Enjoy GreatSQL 😃</p>
<h2 id="关于-greatsql">关于 GreatSQL</h2>
<p>GreatSQL是适用于金融级应用的国内自主开源数据库,具备高性能、高可靠、高易用性、高安全等多个核心特性,可以作为MySQL或Percona Server的可选替换,用于线上生产环境,且完全免费并兼容MySQL或Percona Server。</p>
<p>相关链接: GreatSQL社区 Gitee GitHub Bilibili</p>
<h2 id="greatsql社区">GreatSQL社区:</h2>
<blockquote>
<p>社区博客有奖征稿详情:https://greatsql.cn/thread-100-1-1.html</p>
</blockquote>
<p><img src="https://img2024.cnblogs.com/other/2630741/202508/2630741-20250801100129284-1144527602.png"></p>
<h2 id="技术交流群">技术交流群:</h2>
<blockquote>
<p>微信:扫码添加<code>GreatSQL社区助手</code>微信好友,发送验证信息<code>加群</code>。</p>
</blockquote>
<p><img src="https://img2024.cnblogs.com/other/2630741/202508/2630741-20250801100129516-1747889959.png"></p><br><br>
来源:https://www.cnblogs.com/greatsql/p/19016378
頁:
[1]