使用MySQLTuner调优MySQL

MySQLTuner是一个用Perl编写的脚本,它可以根据当前配置信息和数据使用状态来提供一些建议,以此提高MySQL的效率和稳定性。

MySQLTuner是一个只读脚本,不会往操作系统内写入任何配置文件,或更改任何守护进程。

site: http://mysqltuner.com/
githut: https://github.com/major/MySQLTuner-perl

下载、加执行权限、运行

wget https://raw.github.com/major/MySQLTuner-perl/master/mysqltuner.pl
chmod +x mysqltuner.pl
./mysqltuner.pl

以下是在我的VPS上执行结果

 >>  MySQLTuner 1.2.0 - Major Hayden 
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering
Please enter your MySQL administrative login: root
Please enter your MySQL administrative password: 

-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.1.71-log
[OK] Operating on 64-bit architecture

-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster 
[--] Data in MyISAM tables: 57M (Tables: 297)
[--] Data in MEMORY tables: 0B (Tables: 1)
[!!] InnoDB is enabled but isn't being used
[!!] Total fragmented tables: 10

-------- Security Recommendations  -------------------------------------------
[!!] User [email protected]' has no password set.
[!!] User [email protected]' has no password set.

-------- Performance Metrics -------------------------------------------------
[--] Up for: 1h 3m 37s (279K q [73.100 qps], 7K conn, TX: 564M, RX: 32M)
[--] Reads / Writes: 93% / 7%
[--] Total buffers: 298.0M global + 6.4M per thread (151 max threads)
[OK] Maximum possible memory usage: 1.2G (63% of installed RAM)
[OK] Slow queries: 0% (8/279K)
[OK] Highest usage of available connections: 24% (37/151)
[OK] Key buffer size / total MyISAM indexes: 256.0M/13.9M
[OK] Key buffer hit rate: 99.9% (6M cached / 4K reads)
[OK] Query cache efficiency: 75.1% (182K cached / 243K selects)
[!!] Query cache prunes per day: 77753
[OK] Sorts requiring temporary tables: 0% (13 temp sorts / 13K sorts)
[!!] Temporary tables created on disk: 34% (6K on disk / 19K total)
[OK] Thread cache hit rate: 98% (79 created / 7K connections)
[OK] Table cache hit rate: 95% (152 open / 159 opened)
[OK] Open file limit used: 23% (236/1K)
[OK] Table locks acquired immediately: 99% (77K immediate / 77K locks)

-------- Recommendations -----------------------------------------------------
General recommendations:
    Add skip-innodb to MySQL configuration to disable InnoDB
    Run OPTIMIZE TABLE to defragment tables for better performance
    MySQL started within last 24 hours - recommendations may be inaccurate
    Enable the slow query log to troubleshoot bad queries
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries without LIMIT clauses
Variables to adjust:
    query_cache_size (> 16M)
    tmp_table_size (> 16M)
    max_heap_table_size (> 16M)

根据Recommendations中的信息,需要去修改一下my.cnf。修改完毕并重启mysql,然后在超过24小时后再跑一次,据说时间间隔太短数据不准。
需要注意的是:这个脚本已经有三年未更新了,对于MySQL≥5.4的版本只有部分功能支持。

参考链接:http://www.chenjunlu.com/2012/06/how-to-optimise-mysql-performance/