MySQLTuner是一个用Perl编写的脚本,帮助你提高MySQL性能及稳定性。它通过检索当前配置变量和状态数据,提供一些基本性能建议。
安装使用MySQLTuner
1.下载MySQLTuner
- wget http://mysqltuner.pl/ -O mysqltuner.pl
2.运行程序
3.输出如下
- >> MySQLTuner 1.4.0 - Major Hayden <major@mhtx.net>
- >> 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:
- [OK] Currently running supported MySQL version 5.5.41-0+wheezy1
- [OK] Operating on 64-bit architecture
-
- -------- Storage Engine Statistics -------------------------------------------
- [--] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MRG_MYISAM
- [--] Data in InnoDB tables: 1M (Tables: 11)
- [--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
- [!!] Total fragmented tables: 11
-
- -------- Security Recommendations -------------------------------------------
- [OK] All database users have passwords assigned
-
- -------- Performance Metrics -------------------------------------------------
- [--] Up for: 47s (113 q [2.404 qps], 42 conn, TX: 19K, RX: 7K)
- [--] Reads / Writes: 100% / 0%
- [--] Total buffers: 192.0M global + 2.7M per thread (151 max threads)
- [OK] Maximum possible memory usage: 597.8M (60% of installed RAM)
- [OK] Slow queries: 0% (0/113)
- [OK] Highest usage of available connections: 0% (1/151)
- [OK] Key buffer size / total MyISAM indexes: 16.0M/99.0K
- [!!] Query cache efficiency: 0.0% (0 cached / 71 selects)
- [OK] Query cache prunes per day: 0
- [OK] Temporary tables created on disk: 25% (54 on disk / 213 total)
- [OK] Thread cache hit rate: 97% (1 created / 42 connections)
- [OK] Table cache hit rate: 24% (52 open / 215 opened)
- [OK] Open file limit used: 4% (48/1K)
- [OK] Table locks acquired immediately: 100% (62 immediate / 62 locks)
- [OK] InnoDB buffer pool / data size: 128.0M/1.2M
- [OK] InnoDB log waits: 0
- -------- Recommendations -----------------------------------------------------
- General recommendations:
- Run OPTIMIZE TABLE to defragment tables for better performance
- Enable the slow query log to troubleshoot bad queries
- Variables to adjust:
- query_cache_limit (> 1M, or use smaller result sets)
我们可以根据Recommendations下面建议修改MySQL的配置来的调优性能。
MySQL主要性能参数
key_buffer:
更改key_buffer为MySQL分配更多的内存,这可以大大加快你的数据库。 当使用MyISAM表引擎时,key_buffer大小通常不会超过系统内存的25%,而InnoDB最多可占70%。 如果该值设置得太高,则资源被浪费。 根据MySQL的文档,对于256MB RAM(或更多)具有许多表的服务器,建议使用64M的设置,而具有128MB RAM和较少表的服务器可以设置为16M(默认值)。
max_allowed_packet:
允许发送的数据包的最大大小。 数据包是单个SQL状态,单个行发送到客户端,或日志从主机发送到从机。 如果你知道你的MySQL服务器将要处理大数据包,最好提高到你最大的数据包的大小。 如果此值设置得太小,您将在错误日志中收到错误。
thread_stack:
此值包含每个线程的堆栈大小。 MySQL认为thread_stack变量的默认值足以正常使用; 但是,如果出现了与thread_stack相关的错误,则可以调大此值。
thread_cache_size:
如果thread_cache_size是“turned off”(设置为0),则所有新建的连接都需要为它们创建一个新的线程,当连接断开时,线程被销毁。 否则,此值设置要存储在缓存中的未使用线程的数量,直到它们需要用于连接。 通常,此设置对性能影响不大,除非您每分钟接收数百个连接,此时应该刷新该值,以便大多数连接使用到缓存线程。
max_connections:
设置并发连接的最大数量。 最好考虑过去的最大连接数,然后设置它,以便在该数字和max_connections值之间有一些缓冲区。