借助MySQLTuner提高MySQL性能及稳定性

MySQLTuner是一个用Perl编写的脚本,帮助你提高MySQL性能及稳定性。它通过检索当前配置变量和状态数据,提供一些基本性能建议。

安装使用MySQLTuner

1.下载MySQLTuner

  1. wget http://mysqltuner.pl/ -O mysqltuner.pl

2.运行程序

  1. perl mysqltuner.pl

3.输出如下

  1. >>  MySQLTuner 1.4.0 – Major Hayden <major@mhtx.net>
  2.  >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
  3.  >>  Run with ‘–help’ for additional options and output filtering
  4. Please enter your MySQL administrative login: root
  5. Please enter your MySQL administrative password:
  6. [OK] Currently running supported MySQL version 5.5.41-0+wheezy1
  7. [OK] Operating on 64-bit architecture
  8.  
  9. ——– Storage Engine Statistics ——————————————-
  10. [–] Status: +ARCHIVE +BLACKHOLE +CSV -FEDERATED +InnoDB +MRG_MYISAM
  11. [–] Data in InnoDB tables: 1M (Tables: 11)
  12. [–] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17)
  13. [!!] Total fragmented tables: 11
  14.  
  15. ——– Security Recommendations  ——————————————-
  16. [OK] All database users have passwords assigned
  17.  
  18. ——– Performance Metrics ————————————————-
  19. [–] Up for: 47s (113 q [2.404 qps], 42 conn, TX: 19K, RX: 7K)
  20. [–] Reads / Writes: 100% / 0%
  21. [–] Total buffers: 192.0M global + 2.7M per thread (151 max threads)
  22. [OK] Maximum possible memory usage: 597.8M (60% of installed RAM)
  23. [OK] Slow queries: 0% (0/113)
  24. [OK] Highest usage of available connections: 0% (1/151)
  25. [OK] Key buffer size / total MyISAM indexes: 16.0M/99.0K
  26. [!!] Query cache efficiency: 0.0% (0 cached / 71 selects)
  27. [OK] Query cache prunes per day: 0
  28. [OK] Temporary tables created on disk: 25% (54 on disk / 213 total)
  29. [OK] Thread cache hit rate: 97% (1 created / 42 connections)
  30. [OK] Table cache hit rate: 24% (52 open / 215 opened)
  31. [OK] Open file limit used: 4% (48/1K)
  32. [OK] Table locks acquired immediately: 100% (62 immediate / 62 locks)
  33. [OK] InnoDB buffer pool / data size: 128.0M/1.2M
  34. [OK] InnoDB log waits: 0
  35. ——– Recommendations —————————————————–
  36. General recommendations:
  37.     Run OPTIMIZE TABLE to defragment tables for better performance
  38.     Enable the slow query log to troubleshoot bad queries
  39. Variables to adjust:
  40.     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值之间有一些缓冲区。