01.03.2013 Views

Optimizing MySQL Configuration

Optimizing MySQL Configuration

Optimizing MySQL Configuration

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Optimizing</strong> <strong>MySQL</strong> <strong>Configuration</strong><br />

Peter Zaitsev,CEO<br />

Technical Webinars Series<br />

March 2012


Agenda<br />

• <strong>MySQL</strong> <strong>Configuration</strong> Tuning Basics<br />

• Tools to Configure <strong>MySQL</strong><br />

• Looking at Most Important Options<br />

www.percona.com


Things to know about <strong>MySQL</strong><br />

configuration<br />

• Default configuration is poor<br />

– <strong>MySQL</strong> does not scale it with server size<br />

• Understand what you're changing<br />

– Google Copy/Paste without thinking can be bad<br />

• Avoid Obsessive Tuning Disorder<br />

– Setting 10 settings will give 95% of possible<br />

performance in 95% cases<br />

• Beware of “Sample Configs” In <strong>MySQL</strong><br />

distributions<br />

– They are pretty outdated<br />

– 2GB of memory is “huge” these days ?<br />

www.percona.com


Most Options do not Scale<br />

• Going to Server with 8x memory you can't just<br />

multiply all configuration variables 8x<br />

•16GB of memory to 128GB of memory<br />

– sort_buffer_size 4MB to 32MB is bad idea.<br />

www.percona.com


Know Scope and Unit<br />

• sort_buffer_size=16G<br />

– Wrong! sort_buffer_size is set per connection<br />

• table_cache_size=64M<br />

– Wrong! table_cache_size is set in elements<br />

not memory size.<br />

www.percona.com


Set Variables Locally<br />

• Many Variables are SESSION<br />

– Can be set for current session only<br />

• Set variable value for session doing complex<br />

queries instead of setting it globally:<br />

mysql> set session sort_buffer_size=16*1024*1024;<br />

Query OK, 0 rows affected (0.00 sec)<br />

www.percona.com


Avoid Basic Mistakes<br />

• Setting variables in wrong config file<br />

– /etc/mysql/my.cnf instead of /etc/my.cnf<br />

• These depend on Linux Distro, Beware<br />

• Duplicating Options<br />

– Last option will override previously set<br />

• Not knowing Synonyms<br />

– table_cache is same as table_open_cache<br />

• Using wrong section for options<br />

– Server reads [mysqld], client [mysql]<br />

www.percona.com


Config Management Practices<br />

• Keep Config Files in Sync on different servers<br />

– Out of Sync config files is frequent cause of<br />

mistakes and confusion<br />

• Keep Record of Changes<br />

– Config files under version control is great<br />

– At least keep your changes documented<br />

www.percona.com


Do not let <strong>MySQL</strong> Swap<br />

• Allocating too much memory and having <strong>MySQL</strong><br />

swapping is a lot worse than not using all memory<br />

• Monitor swapping (si/so from vmstat closely)<br />

• Start with safe buffer values and increase them<br />

gradually if a lot of memory stays free<br />

pz@ubuntu:~$ vmstat 5<br />

procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu---r<br />

b swpd free buff cache si so bi bo in cs us sy id wa<br />

1 0 0 2725708 253216 513572 0 0 1 1 20 22 0 0 100 0<br />

0 0 0 2725700 253216 513596 0 0 0 0 72 73 0 0 100 0<br />

0 0 0 2725700 253216 513596 0 0 0 3 70 74 0 0 99 1<br />

0 0 0 2725700 253216 513596 0 0 0 0 70 74 0 0 100 0<br />

0 0 0 2725700 253216 513596 0 0 0 0 70 74 0 0 100 0<br />

0 0 0 2725700 253216 513596 0 0 0 0 70 72 0 0 100 0<br />

www.percona.com


Agenda<br />

• <strong>MySQL</strong> <strong>Configuration</strong> Tuning Basics<br />

• Tools to Configure <strong>MySQL</strong><br />

• Looking at Most Important Options<br />

www.percona.com


Automated <strong>Configuration</strong> Tuning<br />

• <strong>Configuration</strong> Tuning Tools<br />

–Tools which claim to create best configuration by<br />

looking at status variables<br />

• Advisory Tools<br />

– Tools which check your config file for typical<br />

mistakes and omissions<br />

• Basic configuration creation tools<br />

– Do not claim to do magic but can get your<br />

started with better configuration than default<br />

www.percona.com


mysqtuner<br />

-------- General Statistics --------------------------------------------------<br />

[--] Skipped version check for <strong>MySQL</strong>Tuner script<br />

[OK] Currently running supported <strong>MySQL</strong> version 5.1.57-rel12.8-log<br />

[OK] Operating on 64-bit architecture<br />

-------- Storage Engine Statistics -------------------------------------------<br />

[--] Status: +Archive -BDB -Federated +InnoDB -ISAM -NDBCluster<br />

[--] Data in MyISAM tables: 73G (Tables: 1282)<br />

[--] Data in InnoDB tables: 1G (Tables: 338)<br />

[--] Data in MEMORY tables: 0B (Tables: 2)<br />

[!!] Total fragmented tables: 110<br />

-------- Security Recommendations -------------------------------------------<br />

[!!] User '@' has no password set.<br />

www.percona.com


Mysqltuner (2)<br />

-------- Performance Metrics -------------------------------------------------<br />

• [--] Up for: 157d 10h 0m 23s (533M q [39.219 qps], 8M conn, TX: 1202B, RX:<br />

146B)<br />

• [--] Reads / Writes: 97% / 3%<br />

• [--] Total buffers: 4.3G global + 2.7M per thread (200 max threads)<br />

• [OK] Maximum possible memory usage: 4.8G (40% of installed RAM)<br />

• [!!] Slow queries: 7% (41M/533M)<br />

• [OK] Highest usage of available connections: 54% (109/200)<br />

• [OK] Key buffer size / total MyISAM indexes: 4.0G/1.3G<br />

• [OK] Key buffer hit rate: 100.0% (45B cached / 6M reads)<br />

• [!!] Query cache is disabled<br />

• [OK] Sorts requiring temporary tables: 0% (44K temp sorts / 60M sorts)<br />

• [!!] Joins performed without indexes: 255685<br />

• [!!] Temporary tables created on disk: 41% (25M on disk / 61M total)<br />

• [OK] Thread cache hit rate: 91% (791K created / 8M connections)<br />

• [!!] Table cache hit rate: 2% (1K open / 59K opened)<br />

• [OK] Open file limit used: 32% (2K/8K)<br />

• [OK] Table locks acquired immediately: 99% (436M immediate / 436M locks)<br />

• [!!] InnoDB data size / buffer pool: 1.1G/256.0M<br />

•<br />

www.percona.com


mysqltuner(3)<br />

-------- Recommendations -----------------------------------------------------<br />

• General recommendations:<br />

• Run OPTIMIZE TABLE to defragment tables for better performance<br />

• Adjust your join queries to always utilize indexes<br />

• When making adjustments, make tmp_table_size/max_heap_table_size equal<br />

• Reduce your SELECT DISTINCT queries without LIMIT clauses<br />

• Increase table_cache gradually to avoid file descriptor limits<br />

• Variables to adjust:<br />

• query_cache_size (>= 8M)<br />

• join_buffer_size (> 128.0K, or always use indexes with joins)<br />

• tmp_table_size (> 16M)<br />

• max_heap_table_size (> 16M)<br />

• table_cache (> 4096)<br />

• innodb_buffer_pool_size (>= 1G)<br />

www.percona.com


pt-variable-advisor<br />

• # WARN innodb_flush_log_at_trx_commit-1: InnoDB is not configured in strictly ACID mode.<br />

•<br />

• # NOTE innodb_max_dirty_pages_pct: The innodb_max_dirty_pages_pct is lower than the default.<br />

•<br />

• # NOTE log_warnings-2: Log_warnings must be set greater than 1 to log unusual events such as<br />

aborted connections.<br />

•<br />

• # NOTE max_connect_errors: max_connect_errors should probably be set as large as your<br />

platform allows.<br />

•<br />

• # WARN old_passwords: Old-style passwords are insecure.<br />

•<br />

• # WARN slave_net_timeout: This variable is set too high.<br />

•<br />

• # NOTE innodb_data_file_path: Auto-extending InnoDB files can consume a lot of disk space that is<br />

very difficult to reclaim later.<br />

•<br />

• # WARN myisam_recover_options: myisam_recover_options should be set to some value such as<br />

BACKUP,FORCE to ensure that table corruption is noticed.<br />

•<br />

• # WARN sync_binlog: Binary logging is enabled, but sync_binlog isn't configured so that every<br />

transaction is flushed to the binary log for durability.<br />

www.percona.com


tools.percona.com<br />

www.percona.com


Tools.percona.com<br />

www.percona.com


Tools.percona.com<br />

www.percona.com


Agenda<br />

• <strong>MySQL</strong> <strong>Configuration</strong> Tuning Basics<br />

• Tools to Configure <strong>MySQL</strong><br />

• Looking at Most Important Options<br />

www.percona.com


Lets look at the options now<br />

• Different classes of options:<br />

– General Options<br />

– MyISAM<br />

– Innodb<br />

– Visibility and Logging<br />

www.percona.com


Getting Status Variables<br />

• We refer to SHOW GLOBAL STATUS output in<br />

many descriptions<br />

• Percona Toolkit too pt-mext is helpful<br />

• pt-mext -r -- mysqladmin ext -i100 -c4<br />

• Aborted_clients 128 0 0<br />

Aborted_connects 909 0 0<br />

Binlog_cache_disk_use 3 0 0<br />

Binlog_cache_use 262857 0 0<br />

Bytes_received 146518902681 580976 459113<br />

Bytes_sent 1202983049426 1417886 1018617<br />

www.percona.com


General Options<br />

• max_connections<br />

– How many connections to allow ? Watch<br />

max_used_connections status value<br />

• thread_cache<br />

– Cache to prevent excessive thread creation<br />

– 50-100 is good value. Watch threads_created<br />

• table_cache/table_open_cache<br />

– Cache of opened table instances<br />

– Single table may have multiple entries<br />

– Watch opened_tables status value<br />

– Start with 4096<br />

• <strong>MySQL</strong> will only use as needed anyway.<br />

www.percona.com


General Options<br />

• open_files_limit<br />

– MyISAM tables require up to 2 file handlers<br />

– Each connection is file handler too<br />

– Safe to set to 65535 in most systems<br />

• table_definition_cache<br />

– Cache table definitions (CREATE TABLE)<br />

– Only one entry per table<br />

– Watch Opened_table_definitions<br />

– Set to number of tables + 10% unless 50K+<br />

tables<br />

www.percona.com


General Options<br />

• back_log<br />

– Need adjustment if many connections/sec<br />

– 2048 is reasonable value<br />

• max_allowed_packet<br />

– Limits maximum size of query<br />

– Limits internal string variable size<br />

– 16MB is a good value<br />

• max_connect_errors<br />

– Prevent password brute force attack<br />

– Can cause “Host Blocked” error messages<br />

– Value around 1000000 is good<br />

www.percona.com


General Options<br />

• skip_name_resolve<br />

– Avoid DNS lookup on connection. Faster and<br />

Safer<br />

– Do not use host names in GRANTs<br />

• old_passwords<br />

– Should NOT be enabled. Will cause insecure<br />

password hash to be used.<br />

www.percona.com


General Options<br />

• log_bin<br />

– Enable for replication and point in time recovery<br />

– Set to “mysql-bin” to avoid default naming<br />

• sync_binlog<br />

– Make Binlog durable. Set to 1 if have RAID<br />

with BBU or Flash<br />

•Can be really performance killer with slow<br />

drives.<br />

• expire_log_days<br />

– Purge old binary logs after this number of days<br />

– 14 (2 weeks) is a good value with weekly<br />

backups.<br />

www.percona.com


General Options<br />

• tmp_table_size<br />

• max_heap_table_size<br />

– Typically set to same value (workload based)<br />

– Created_tmp_disk_tables status variable<br />

• Beware BLOB/TEXT fields cause on disk<br />

table with any size.<br />

• query_cache_size<br />

– Enable query cache only if it is tested to provide<br />

significant gains<br />

– Often causes stalls and contention<br />

– Do not set over 512MB<br />

www.percona.com


General Options<br />

• sort_buffer_size<br />

– In memory buffer used for sorting<br />

– Watch sort_merge_passes<br />

– Consider setting for session for large queries<br />

– Values up to 1MB are good default<br />

– Large values hurt performance of small queries<br />

• join_buffer_size<br />

– Helps performance of Joins with no indexes<br />

– Better get rid of such Joins !<br />

– 8MB can be reasonable value<br />

• default_storage_engine<br />

– Use this engine for tables if not specified<br />

www.percona.com


General Options<br />

• read_rnd_buffer_size<br />

– Buffer for reading rows in sorted offer<br />

– Specifies Maximum Value<br />

– Values around 16MB often make sense<br />

– Do not mix with read_buffer_size<br />

• Tmpdir<br />

– Specify location of temporary directory<br />

– Tmpfs often good choice unless very large<br />

temporary space is needed.<br />

•tmpdir=/dev/shm<br />

www.percona.com


MyISAM options<br />

• key_buffer_size<br />

– Cache MyISAM Indexes.<br />

– Does Not cache data.<br />

– Up to 30% of memory if using MyISAM only<br />

• myisam_recover<br />

– Automatically repair corrupted MyISAM tables<br />

after crash. BACKUP,FORCE is a good value.<br />

• myisam_sort_buffer_size<br />

– Buffer used for building MyISAM indexes by<br />

Sort. 8MB-256MB are good values.<br />

www.percona.com


MyISAM Options<br />

• low_priority_updates<br />

– Allow higher concurrency for SELECTs<br />

– May starve update queries<br />

• bulk_insert_buffer_size<br />

– Buffer to optimize Bulk Inserts<br />

– Values of ¼ of key_buffer_size make sense<br />

– Note it is per connection value<br />

www.percona.com


Innodb – Memory Settings<br />

• innodb_buffer_pool_size<br />

–The most important setting. Often 80%+ of<br />

memory is allocated here.<br />

• innodb_buffer_pool_instances<br />

– Reduce contention. Set to 4+ in <strong>MySQL</strong> 5.5+<br />

• innodb_log_buffer_size<br />

– Buffer for log files. Good Values 4MB-128MB<br />

– Not only reduce writes but help contention<br />

• innodb_ibuf_max_size<br />

– Control size of Insert buffer. Default is ½ of<br />

Buffer pool. Smaller values are good for SSD<br />

www.percona.com


Innodb IO Options<br />

• innodb_flush_log_at_trx_commit<br />

– Control Durability<br />

– 1=flush and sync; 2=flush; 0=neither<br />

• Innodb_flush_method<br />

– Controls how Innodb Performs IO<br />

– O_DIRECT good value for most servers<br />

• innodb_auto_lru_dump<br />

– Percona Server Feature to warmup quickly<br />

– 300 (seconds) is a good value<br />

• innodb_io_capacity<br />

– Controls Innodb Assumption about Disk<br />

Performance. Increase for faster drives.<br />

www.percona.com


Innodb IO Options<br />

• innodb_read_io_threads<br />

• innodb_write_io_threads<br />

–Control number of threads doing reads and<br />

writes<br />

– <strong>MySQL</strong> 5.5 has async IO so very high values<br />

might not be needed<br />

– 4 is good default. Higher for large IO systems.<br />

• innodb_flush_neighbor_pages<br />

– Percona Server feature to control how flushing<br />

works<br />

– Disable (set to 0) for SSD<br />

www.percona.com


Other Innodb Options<br />

• innodb_log_file_size<br />

– Size of redo log file. Larger logs better<br />

performance but longer recovery.<br />

• innodb_log_files_in_group<br />

– Leave at 2 which is default.<br />

• innodb_file_per_table<br />

– Store each Innodb table in separate file. Usually<br />

Good choice<br />

• innodb=force<br />

– Enable so <strong>MySQL</strong> does not start if Innodb could<br />

not initialize. Otherwise it might start but error on<br />

access to all Innodb tables.<br />

www.percona.com


Other Innodb Options<br />

• innodb_data_file_path<br />

– Settings for Innodb System Tablespace<br />

– Use one file. Limit growth as you can't shrink it<br />

– ibdata1:10M:autoextend:max:10G<br />

• innodb_lock_wait_timeout<br />

– How long to wait for row level locks before<br />

bailing out ?<br />

• innodb_old_blocks_time<br />

– Helps to make buffer pool scan resistant<br />

– Values around 1000 make sense<br />

www.percona.com


Other Innodb Options<br />

• innodb_file_format<br />

– Which file format Innodb will use<br />

– “Antelope” is default legacy format<br />

–“Barracuda” allows to use new features like<br />

compression<br />

• innodb_stats_on_metadata<br />

– Update statistics on meta data access<br />

– Such as Information_schema queries<br />

– Typically best disabled for more workloads<br />

•Set to 0<br />

– Innodb will still refresh stats when table<br />

changes significantly<br />

www.percona.com


Visibility Options<br />

• performance_schema<br />

– Enable Performance Schema in <strong>MySQL</strong> 5.5+<br />

– Watch potential overhead.<br />

• log_slow_queries<br />

– Enable Slow Query Log. Old but very helpful.<br />

• long_query_time<br />

– Especially with long_query_time set to 0<br />

periodically to get sample of the load<br />

• log_slow_verbosity=full<br />

– Get a lot more data about queries in Percona<br />

Server<br />

www.percona.com


Visibility Options<br />

• low_warnings=2<br />

– Get warnings about disconnects and other<br />

minor issues in error log.<br />

– More information but it can get spammy<br />

• userstat_running=1<br />

– Get advanced Table and Index usage statistics<br />

in Percona Server and MariaDB<br />

www.percona.com


Summary<br />

• Many Options to chose from !<br />

• Close to 400 variables available In latest versions<br />

• Remember in most cases you do not need to tune<br />

more than a few<br />

• Consider starting with config file generated by<br />

http://tools.percona.com<br />

– At least it will show you which options to pay<br />

attention to first.<br />

www.percona.com


Resources<br />

● High Performance <strong>MySQL</strong><br />

● Third edition in a matter of days!<br />

● http://www.highperfmysql.com/<br />

● Percona Training<br />

● http://www.percona.com/training/<br />

● Dallas,TX May14-17<br />

● Percona Webinars<br />

● http://www.percona.com/webinars/<br />

● Mar 28: Monitoring <strong>MySQL</strong> with Percona Monitoring Plugins<br />

● Apr 4: Percona XtraDB Cluster<br />

www.percona.com


● Percona Toolkit<br />

Resources<br />

● pt-visual-explain tool makes EXPLAIN readable<br />

● http://www.percona.com/software/<br />

● Percona Online Tools<br />

● http://tools.percona.com/<br />

● Generate my.cnf files easily<br />

● Get advice on SQL<br />

● Percona Career Center (Yes, We're Hiring!)<br />

● www.percona.com/about-us/careers/<br />

www.percona.com


<strong>MySQL</strong> Conference & Expo<br />

April 10-12, Santa Clara<br />

www.percona.com/live<br />

Use “Webinar-PL” code for 10% off<br />

Peter Zaitsev<br />

pz@percona.com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!