工具分享-通过开源工具 tuning-primer快速巡检MySQL5.7

工具分享-通过开源工具 tuning-primer快速巡检MySQL5.7

简介

今天推荐一个开源的调优脚本tuning-primer.sh,这是一个非常实用的性能调优工具,能够快速检查 MySQL 的运行状态,生成详细的性能报告,并提供优化建议。通过这个脚本,可以更高效地完成调优工作,节省大量的时间和精力。

使用场景

  1. 快速识别 MySQL服务器的性能瓶颈,如缓存大小、连接数、查询缓存等,并根据建议进行调整
  2. 集成到定期任务中(如使用 cron 作业),定期生成性能报告,帮助持续监控和优化 MySQL服务器的性能

tuning-primer.sh功能

性能分析:检查 MySQL 服务器的运行状态,包括查询缓存、慢查询日志、InnoDB 配置等。

优化建议:根据分析结果,提供调整服务器配置等建议

自动检测:自动读取 MySQL配置文件,并尝试使用其中的登录信息

目前,支持检测和优化建议的内容:

建议类别 描述 备注
慢查询日志 (Slow Query Log) 处理有关慢查询日志的建议。 确保日志记录有效查询,优化性能。
最大连接数 (Max Connections) 处理有关数据库最大连接数的建议。 避免过多连接导致资源耗尽。
工作线程 (Worker Threads) 处理有关数据库工作线程的建议。 优化线程数提高并发处理能力。
键缓冲区 MyISAM 仅限 (Key Buffer MyISAM only) 提供有关 MyISAM 引擎键缓冲区大小的建议。 仅适用于 MyISAM 引擎。
查询缓存 (Query Cache) 处理有关查询缓存大小和效率的建议。 提高缓存效率减少查询延迟。
排序缓冲区 (Sort Buffer) 提供有关排序操作使用的缓冲区大小的建议。 优化排序操作性能。
连接 (Joins) 提供有关 JOIN 操作和连接缓冲区大小的建议。 提高连接效率。
临时表 (Temp Tables) 处理有关临时表的使用和配置的建议。 优化临时表性能。
表缓存 (Table (Open & Definition) Cache) 提供有关表的打开缓存和定义缓存的建议。 提高缓存效率减少磁盘 I/O。
表锁定 (Table Locking) 处理有关表锁定和并发访问控制的建议。 确保数据一致性。
表扫描 (Table Scans) 提供有关全表扫描操作和读取缓冲区的建议,仅限 MyISAM。 优化全表扫描性能。
InnoDB 状态 (InnoDB Status) 提供有关 InnoDB 存储引擎的状态和配置的建议。 优化 InnoDB 性能。

使用步骤

安装与准备

脚本的安装十分简单,只需要在下面项目地址中下载即可使用。

访问:tuning-primer开源项目地址

下载完成之后就可以使用,使用前需要对脚本进行赋权。

Bash 复制代码
$ chmod +x tuning-primer.sh

查看该脚本,能够看到需要配置的MySQL的socket路径。

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

先用./tuning-primaer.sh help 查看下帮助。

Bash 复制代码
$./tuning-primer.sh help
usage: ./tuning-primer.sh [ all | banner | file | innodb | memory | misc | prompt ]

可以看到支持以上集中检查。

运行脚本: 直接执行脚本进行调优分析。

Bash 复制代码
$./tuning-primer.sh

运行结果

运行结果包含多项指标如缓存,日志等方面信息,以下为部分结果:

SLOW QUERIES

判断当前慢查询日志启用状态,获取当前设置的阈值,统计系统慢查询的总次数以及给出慢查询阈值的优化建议。

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.
BINARY UPDATE LOG

检查Binlog相关参数,根据当前的MySQL给出相应的优化建议。

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

可以看到,MySQL未设置expire_logs_days,给出提示使用 RESET MASTER or PURGE MASTER LOGS来清理旧的日志。

MAX CONNECTIONS

总结MySQL数据库的连接使用情况的摘要。给出服务器的优化建议,由于当前使用率低于10%,可以考虑降低max_connections的值,以避免内存的过度分配。

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
OPEN FILES LIMIT

对打开文件限制进行巡检调优。

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
INNODB STATUS

以下巡检关于 InnoDB 存储引擎的索引和数据空间使用情况的摘要和建议。包括当前索引空间,InnoDB数据空间以及缓冲池配置等。

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

其他检查项

如MEMORY USAGE、TABLE CACHE、TEMP TABLES、TABLE SCANS、TABLE LOCKING等

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

总结

通过这个开源脚本工具,在 MySQL 服务器上运行此程序能够快速进行性能方面的检查以及给出一定的参考建议,帮助找出并修正潜在的问题点。

tuning-primer.sh特点

  1. 易于使用:脚本设计简单,用户通过一条命令即可启动整个调优流程,无论是经验丰富的数据库管理员还是新手都能轻松使用。
  2. 定制化:该脚本提供了一定程度的自定义选项,允许用户根据自己的具体需求调整脚本的默认设置。
  3. 全面:脚本覆盖了数据库调优的多个方面,包括内核参数、文件系统、安全性设置和性能监控等。
  4. 社区支持:作为一个开源项目,拥有一个活跃的开发者社区,不断进行维护和更新,确保脚本的功能持续完善和更新。
  5. 本地运行:目前该脚本仅支持在本地运行,无法远程连接到数据库服务器进行巡检调优。

Enjoy GreatSQL :)

关于 GreatSQL

GreatSQL是适用于金融级应用的国内自主开源数据库,具备高性能、高可靠、高易用性、高安全等多个核心特性,可以作为MySQL或Percona Server的可选替换,用于线上生产环境,且完全免费并兼容MySQL或Percona Server。

相关链接: GreatSQL社区 Gitee GitHub Bilibili

GreatSQL社区:

社区有奖建议反馈: greatsql.cn/thread-54-1...

社区博客有奖征稿详情: greatsql.cn/thread-100-...

(对文章有疑问或者有独到见解都可以去社区官网提出或分享哦~)

技术交流群:

微信&QQ群:

QQ群:533341697

微信群:添加GreatSQL社区助手(微信号:wanlidbc )好友,待社区助手拉您进群。

相关推荐
一 乐17 分钟前
汽车租赁|基于SprinBoot+vue的汽车租赁管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·汽车·论文·毕设·汽车租赁管理系统
zandy101130 分钟前
衡石科技 NL2Metrics 技术深度解析(2026):ChatBI 准确度破局的关键路径
数据库·科技·oracle
Elastic 中国社区官方博客32 分钟前
Elasticsearch 如何通过 synthetic _id 和 Bloom filters 将时序存储降低 34%
大数据·数据库·elasticsearch·搜索引擎·serverless·全文检索·时序数据库
一只鹿鹿鹿1 小时前
信息化项目管理规范(参考Word文件)
java·大数据·运维·开发语言·数据库
这个DBA有点耶1 小时前
多模融合数据库深度解析:关系、文档、向量、图如何统一?
数据库·自然语言处理·aigc·dba·改行学it
anew___1 小时前
《数据库原理》精要解读(三)—— SQL:与数据库对话的艺术
数据库·sql·oracle
KaiwuDB1 小时前
KWDB 3.2.0 版本发布,数据管理查询增强,安装部署体验全面升级
数据库
暴躁小师兄数据学院1 小时前
【AI大数据工程师特训笔记】第10讲:数据库用户、权限管理、数据库约束
大数据·数据库·笔记·sql·postgresql
凤山老林1 小时前
DDD(领域驱动设计)在复杂业务系统中的落地指南
java·开发语言·数据库·ddd·领域驱动