1、前言
对于平时不经常使用 MySQL 的人员来说,MySQL 的优化比较难入手,这里推荐 MySQLTuner-perl 工具,简单易用,能够帮助 MySQL 小白解决 80% 的优化问题。
2、MySQLTuner-perl 介绍
MySQLTuner是一个用Perl编写的脚本,允许您快速查看MySQL安装并进行调整以提高性能和稳定性。当前的配置变量和状态数据将被检索并以简短的格式显示沿着还有一些基本的性能建议。
在这个最新版本中,MySQLTuner支持MySQL/MariaDB/Percona Server的~300个指示器。
2.1 MySQLTuner 兼容性
- MySQL 8.0(部分支持,密码检查不起作用)
- Percona Server 8.0(部分支持,密码检查不起作用)
- MySQL 5.7(完全支持)
- Percona Server 5.7(完全支持)
- MariaDB 10.3—10.11(完整支持)
- Galera replication(完全支持)
- Percona XtraDB集群(完全支持)
- Mysql Replications(部分支持,无测试环境)
- MySQL 5.6及更早版本(不支持,弃用版本)
- Percona Server 5.6(不支持,弃用版本)
- MariaDB 5.5(不支持,弃用版本)
- MariaDB 10.2及更早版本(不支持,弃用版本)
2.2 Windows支持部分
- 支持Windows
- 在WSL2(Windows子系统Linux)上成功运行MySQLtuner
- https://docs.microsoft.com/en-us/windows/wsl/
2.3 运行环境要求
- perl5.6或更高版本(带perl-doc包)
- 基于Unix/Linux的操作系统(在Linux、BSD变体和Solaris变体上测试)
- 对MySQL服务器的无限制读取访问(建议MySQL 5.1使用操作系统root访问权限)
3、下载及使用
3.1 下载
# 脚本直接下载(最简单最短的方法)
wget http://mysqltuner.pl/ -O mysqltuner.pl
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/basic_passwords.txt -O basic_passwords.txt
wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/vulnerabilities.csv -O vulnerabilities.csv
# 使用git clone
git clone --depth 1 -b master https://github.com/major/MySQLTuner-perl.git
3.2 使用
cd MySQLTuner-perl
perl mysqltuner.pl --host targetDNS_IP --user admin_user --pass admin_password
# 输出信息,看最后一部分
-------- Recommendations ---------------------------------------------------------------------------
General recommendations:
58 CVE(s) found for your MySQL release. Consider upgrading your version !
Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=ON
We will suggest raising the 'join_buffer_size' until JOINs not using indexes are found.
See https://dev.mysql.com/doc/internals/en/join-buffer-size.html
(specially the conclusions at the bottom of the page).
Be careful, increasing innodb_redo_log_capacity means higher crash recovery mean time
Variables to adjust:
skip-name-resolve=ON
join_buffer_size (> 256.0K, or always use indexes with JOINs)
innodb_buffer_pool_size (>= 4.7G) if possible.
innodb_redo_log_capacity should be (=32M) if possible, so InnoDB Redo log Capacity equals 25% of buffer pool size.
# 可配置参数
-------- Recommendations ---------------------------------------------------------------------------
Variables to adjust:
skip-name-resolve=ON
join_buffer_size (> 256.0K, or always use indexes with JOINs)
innodb_buffer_pool_size (>= 4.7G) if possible.
innodb_redo_log_capacity should be (=32M) if possible, so InnoDB Redo log Capacity equals 25% of buffer pool size.
# 修改参数,MySQL Ver 8.0.30为例
vim /etc/my.cnf.d/mysql-server.cnf
# 增加, 具体值根据服务器配置
skip-name-resolve=ON
join_buffer_size=16M
innodb_buffer_pool_size=16G
innodb_redo_log_capacity=512M
# 重启MySQL
systemctl restart mysqld.service
4、参考
官网地址 :https://github.com/major/MySQLTuner-perl