我们继续性能调优系列。
从过前两天的努力,再加上Cache和其他优化,系统的负载由原来的动不动10+下降到了1左右。这应当是巨大的变化,但是一段时间后发现,系统还是有不稳定的情况,CPU有时会莫名的高涨,虽然之前对Apache和PHP的限制使得系统有一点的稳定性,在大多数情况下可以在一段时间后恢复。不过凡事都应有个结局,对吧,找出问题是怪黍蜀的天性……
轮到了MySql, 现代系统,数据库基本上是应用的核心。系统后期再去调优基本上已经是木已成舟司马当作活马医,不过验尸为以后积攒经验还是必要的。再次多余一句,请尽可能优雅的设计数据库。
设计到达一定阶段,就是应用了。这个时候可以进行SQL的Debug, 善用执行计划,调整索引和存储策略,参看Wiki 查询计划
好了,如果这些都没问题了,或者你没办法做这些事情了,就剩下数据库系统的调优了。Database Tuning 这是个多少有点复杂的主题,不过今天介绍的东西只要理解相关概念就可以了。
用于MySql调优的诊断脚本
MySqlTuner 是一个Perl脚本,简单好用。
下载脚本
修改脚本,增加执行权限
chmod +x mysqltuner-1.0.0.pl
运行
./mysqltuner-1.0.0.pl
程序输出
>> MySQLTuner 1.0.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:——– General Statistics ————————————————–
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.0.45-log
[OK] Operating on 32-bit architecture with less than 2GB RAM——– Storage Engine Statistics ——————————————-
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in MyISAM tables: 89M (Tables: 147)
[--] Data in InnoDB tables: 27M (Tables: 157)
[--] Data in MEMORY tables: 126K (Tables: 2)
[!!] Total fragmented tables: 12——– Performance Metrics ————————————————-
[--] Up for: 16h 33m 50s (480K q [8.050 qps], 28K conn, TX: 94M, RX: 95M)
[--] Reads / Writes: 75% / 25%
[--] Total buffers: 88.0M global + 2.7M per thread (60 max threads)
[OK] Maximum possible memory usage: 249.2M (49% of installed RAM)
[OK] Slow queries: 0% (18/480K)
[OK] Highest usage of available connections: 33% (20/60)
[OK] Key buffer size / total MyISAM indexes: 32.0M/17.2M
[OK] Key buffer hit rate: 99.6% (3M cached / 15K reads)
[OK] Query cache efficiency: 72.3% (248K cached / 344K selects)
[!!] Query cache prunes per day: 36459
[OK] Sorts requiring temporary tables: 0% (3 temp sorts / 26K sorts)
[OK] Temporary tables created on disk: 21% (3K on disk / 14K total)
[OK] Thread cache hit rate: 97% (765 created / 28K connections)
[OK] Table cache hit rate: 28% (351 open / 1K opened)
[OK] Open file limit used: 31% (343/1K)
[OK] Table locks acquired immediately: 99% (193K immediate / 193K locks)
[OK] InnoDB data size / buffer pool: 27.1M/30.0M——– Recommendations —————————————————–
General recommendations:
Run OPTIMIZE TABLE to defragment tables for better performance
MySQL started within last 24 hours – recommendations may be inaccurate
Variables to adjust:
query_cache_size (> 8M)
注意标注!!!的提示,同时脚本给出了若干优化意见。根据经验分析和修改就可以了。
顺带定时检查和优化表的cron
mysqlcheck -Aao -auto-repair -u root –password=*password* >/dev/null
同时给出 /etc/my.cnf参考配置
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Default to using old password format for compatibility with mysql 3.x # clients (those using the mysqlclient10 compatibility package). old_passwords=1 #skip-locking skip-bdb # MySQL 4.x has query caching available. # Enable it for vast improvement and it may be all you need to tweak. query_cache_type=1 query_cache_limit=1M query_cache_size=8M # max_connections=500 # Reduced to 200 as memory will not be enough for 500 connections. # memory=key_buffer+(sort_buffer_size+read_buffer_size)*max_connections # which is now: 64 + (1 + 1) * 200 = 464 MB # max_connections = approx. MaxClients setting in httpd.conf file # Default set to 100. max_connections=60 #interactive_timeout=180 interactive_timeout=100 #wait_timeout=180 #wait_timeout=100 # Reduced wait_timeout to prevent idle clients holding connections. #wait_timeout=30 wait_timeout=15 connect_timeout=10 # max_connect_errors is set to 10 by default #max_connect_errors=10 #table_cache=512 # Checked opened tables and adjusted accordingly after running for a while. table_cache=512 #tmp_table_size=32M #max_heap_table_size=32M #thread_cache=128 # Reduced it to 32 to prevent memory hogging. Also, see notes below. thread_cache=4 thread_cache_size=4 # key_buffer=258M # Reduced it by checking current size of *.MYI files, see notes below. key_buffer=32M # Commented out the buffer sizes and keeping the default. # sort_buffer_size=2M by default. #sort_buffer_size=1M # read_buffer_size=128K by default. #read_buffer_size=1M # 1Mb of read_rnd_buffer_size for 1GB RAM -- see notes below. # read_rnd_buffer_size=256K by default. #read_rnd_buffer_size=1M # myisam_sort_buffer_size used for ALTER, OPTIMIZE, REPAIR TABLE commands. # myisam_sort_buffer_size=8M by default. #myisam_sort_buffer_size=64M # thread_concurrency = 2 * (no. of CPU) thread_concurrency=2 innodb_buffer_pool_size=30M # log slow queries is a must. Many queries that take more than 2 seconds. # If so, then your tables need enhancement. log_slow_queries=/var/log/mysqld.slow.log long_query_time=2
您可能有兴趣的文章:

This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
After the day
One comment
Pingback: 继续说Apache的优化 | Himmel