This article explains how MySQL settings can be optimized following a set of predetermined steps. The results are pretty reliable and we keep using this with a very high degree of success. This is something regularly done at Leanservers.
Basically there are a couple of well-maintained shell-scripts that can examine MySQL settings and make recommendations based on historic performance data (which is gathered by the scripts as well, automatically).
At the moment we are using these two (there might be others out there, so please keep your eyes pealed):
Both scripts produce similar recommendations and we actually run both to “validate each other” and make sure we don’t do anything stupid.
Please keep in mind that we’re assuming you’re trying to do this on a machine running Linux. At least that’s how we do it.
How to set the scripts up
It’s very simple to set them up; usually all that’s needed is to download them. One is written in bash (so it just works) while the other uses Perl but with minimal dependencies. Most of the systems are able to run them.
These would be the commands to execute on a terminal:
wget --no-check-certificate https://raw.github.com/rackerhacker/MySQLTuner-perl/master/mysqltuner.pl chmod +x mysqltuner.pl wget http://launchpad.net/mysql-tuning-primer/trunk/1.6-r1/+download/tuning-primer.sh chmod +x tuning-primer.sh
Running the scripts
Now we are ready to run the scripts. It’s important to keep in mind that the MySQL server should have been running at least 48 hours continuously to get meaningful results.
You will need to have MySQL’s root password ready.
Sample run of MySQL Tuner (perl)
[unai@db1 mysql-scripts]$ /opt/mysql-scripts/mysqltuner.pl >> MySQLTuner 1.2.0 - Major Hayden <firstname.lastname@example.org> >> Bug reports, feature requests, and downloads at http://mysqltuner.com/ >> Run with '--help' for additional options and output filtering -------- General Statistics -------------------------------------------------- [--] Skipped version check for MySQLTuner script [OK] Currently running supported MySQL version 5.5.15-55-log [OK] Operating on 64-bit architecture -------- Storage Engine Statistics ------------------------------------------- [--] Status: +Archive -BDB -Federated +InnoDB -ISAM -NDBCluster [--] Data in MyISAM tables: 899M (Tables: 404) [--] Data in PERFORMANCE_SCHEMA tables: 0B (Tables: 17) [!!] InnoDB is enabled but isn't being used [!!] Total fragmented tables: 56 -------- Security Recommendations ------------------------------------------- [!!] User '@db1.wp.powertvonline.com' has no password set. [!!] User '@localhost' has no password set. [!!] User 'root@::1' has no password set. -------- Performance Metrics ------------------------------------------------- [--] Up for: 10h 0m 29s (32M q [900.706 qps], 139K conn, TX: 194B, RX: 12B) [--] Reads / Writes: 91% / 9% [--] Total buffers: 560.0M global + 12.4M per thread (100 max threads) [OK] Maximum possible memory usage: 1.8G (22% of installed RAM) [OK] Slow queries: 0% (147K/32M) [!!] Highest connection usage: 100% (101/100) [OK] Key buffer size / total MyISAM indexes: 256.0M/1.0G [OK] Key buffer hit rate: 99.9% (1B cached / 2M reads) [OK] Query cache efficiency: 85.6% (26M cached / 31M selects) [!!] Query cache prunes per day: 4647549 [OK] Sorts requiring temporary tables: 0% (16 temp sorts / 1M sorts) [!!] Joins performed without indexes: 195 [!!] Temporary tables created on disk: 46% (491K on disk / 1M total) [OK] Thread cache hit rate: 99% (514 created / 139K connections) [!!] Table cache hit rate: 0% (128 open / 38K opened) [OK] Open file limit used: 20% (212/1K) [OK] Table locks acquired immediately: 99% (4M immediate / 4M 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 Reduce or eliminate persistent connections to reduce connection usage Adjust your join queries to always utilize indexes When making adjustments, make tmp_table_size/max_heap_table_size equal Reduce your SELECT DISTINCT queries without LIMIT clauses Increase table_cache gradually to avoid file descriptor limits Variables to adjust: max_connections (> 100) wait_timeout (< 180) interactive_timeout (< 28800) query_cache_size (> 16M) join_buffer_size (> 128.0K, or always use indexes with joins) tmp_table_size (> 16M) max_heap_table_size (> 32M) table_cache (> 128)
Sample run of MySQL Tuning Primer Script
[unai@db1 mysql-scripts]$ /opt/mysql-scripts/tuning-primer.sh -- MYSQL PERFORMANCE TUNING PRIMER -- - By: Matthew Montgomery - MySQL Version 5.5.15-55-log x86_64 Uptime = 0 days 10 hrs 6 min 44 sec Avg. qps = 900 Total Questions = 32773750 Threads Connected = 7 Warning: Server has not been running for at least 48hrs. It may not be safe to use these recommendations To find out more information on how each of these runtime variables effects performance visit: http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html Visit http://www.mysql.com/products/enterprise/advisors.html for info about MySQL's Enterprise Monitoring and Advisory Service SLOW QUERIES The slow query log is enabled. Current long_query_time = 2.000000 sec. You have 148854 out of 32773895 that take longer than 2.000000 sec. to complete Your long_query_time seems to be fine BINARY UPDATE LOG The binary update log is NOT enabled. You will not be able to do point in time recovery See http://dev.mysql.com/doc/refman/5.5/en/point-in-time-recovery.html WORKER THREADS Current thread_cache_size = 32 Current threads_cached = 26 Current threads_per_sec = 0 Historic threads_per_sec = 0 Your thread_cache_size is fine MAX CONNECTIONS Current max_connections = 100 Current threads_connected = 6 Historic max_used_connections = 101 The number of used connections is 101% of the configured maximum. You should raise max_connections INNODB STATUS Current InnoDB index space = 0 bytes Current InnoDB data space = 0 bytes Current InnoDB buffer pool free = 97 % Current innodb_buffer_pool_size = 256 M Depending on how much space your innodb indexes take up it may be safe to increase this value to up to 2 / 3 of total system memory MEMORY USAGE Max Memory Ever Allocated : 1.75 G Configured Max Per-thread Buffers : 1.20 G Configured Max Global Buffers : 544 M Configured Max Memory Limit : 1.73 G Physical Memory : 7.78 G Max memory limit seem to be within acceptable norms KEY BUFFER Current MyISAM index space = 1.01 G Current key_buffer_size = 256 M Key cache miss rate is 1 : 670 Key buffer free ratio = 81 % Your key_buffer_size seems to be fine QUERY CACHE Query cache is enabled Current query_cache_size = 16 M Current query_cache_used = 13 M Current query_cache_limit = 1 M Current Query cache Memory fill ratio = 87.04 % Current query_cache_min_res_unit = 4 K However, 1959680 queries have been removed from the query cache due to lack of memory Perhaps you should raise query_cache_size MySQL won't cache query results that are larger than query_cache_limit in size SORT OPERATIONS Current sort_buffer_size = 2 M Current read_rnd_buffer_size = 8 M Sort buffer seems to be fine JOINS Current join_buffer_size = 132.00 K You have had 195 queries where a join could not use an index properly You should enable "log-queries-not-using-indexes" Then look for non indexed joins in the slow query log. If you are unable to optimize your queries you may want to increase your join_buffer_size to accommodate larger joins in one pass. Note! This script will still suggest raising the join_buffer_size when ANY joins not using indexes are found. OPEN FILES LIMIT Current open_files_limit = 1024 files The open_files_limit should typically be set to at least 2x-3x that of table_cache if you have heavy MyISAM usage. Your open_files_limit value seems to be fine TABLE CACHE Current table_open_cache = 128 tables Current table_definition_cache = 400 tables You have a total of 445 tables You have 128 open tables. Current table_cache hit rate is 0% , while 100% of your table cache is in use You should probably increase your table_cache You should probably increase your table_definition_cache value. TEMP TABLES Current max_heap_table_size = 32 M Current tmp_table_size = 16 M Of 580078 temp tables, 46% were created on disk Perhaps you should increase your tmp_table_size and/or max_heap_table_size to reduce the number of disk-based temporary tables Note! BLOB and TEXT columns are not allow in memory tables. If you are using these columns raising these values might not impact your ratio of on disk temp tables. TABLE SCANS Current read_buffer_size = 2 M Current table scan ratio = 100 : 1 read_buffer_size seems to be fine TABLE LOCKING Current Lock Wait ratio = 1 : 1825 You may benefit from selective use of InnoDB. If you have long running SELECT's against MyISAM tables and perform frequent updates consider setting 'low_priority_updates=1' If you have a high concurrency of inserts on Dynamic row-length tables consider setting 'concurrent_insert=ALWAYS'.
Understanding and collating results
Although the results are generally safe, please observe caution while applying changes. I wouldn’t recommend modifying any parameter that doesn’t sound familiar to you; you don’t have to be a guru but at least keep reading the parameters’ explanations to start becoming familiar with them.
You could check the output of MySQL tuner which provides a nice summary and for each action item double check it against tuning primer’s longer output. Then write down all the action items that are similar on both outputs.
Read the list that you have and make sure you’re a bit familiar with what each of the variables mean. If unsure, read on MySQL’s official documentation. It’s easy to find, just Google “mysql <variable_name>”.
Putting the suggestions in place
99% of the modifications will require a MySQL restart. It’s best to advise the client and ask for a time when the server is not so busy so you could revert the changes back in case MySQL doesn’t start anymore (that would be your worst case scenario).
Estimating how long MySQL will take is a bit tricky but depends on the size of the database(s). The bigger the databases, the longer a MySQL restart will take and it’s usually up to dozens of seconds (10 seconds to 1 minute). This blog has some more information about it – How long Innodb Shutdown may take.
Always do a backup of
my.cnf before doing any changes. You might need to revert back to it.
Good luck and happy tuning! If you like this post please consider stopping by our NGINX based shared hosting: Leanservers. We have cool things that might interest you.
- MySQL Tuner (perl)
- MySQL Tuning Primer Script
- How long Innodb Shutdown may take
- Tuning Server Parameters