MySQL Veritabanında Konfigürasyon Optimizasyonu
Varsayılan ayarlar ile kurulmuş MySQL veritabanının kullanım şekillerine göre ayarlanması gerekebilir. Yapılan konfigürasyon ayarlarıyla, veritabanının daha performanslı çalışması sağlanabilir. MySQL’in veritabanı optimizasyonu için kullanılan birçok araç vardır. Bu yazıda,
- MySQL Performance Tuning Primer
- MySQL Check
araçlarıyla optimizasyon yapacağız.
MySQL Performance Tuning Primer
Tuning Primer betiği ile MySQL veritabanı analizleri kolay bir şekilde yapılabilmektedir.
NOT: Bu aracı çalıştırmadan önce, MySQL veritabanının 48 saat boyunca kesintisiz çalışır durumda olması gerekmektedir.
Turning Primer ile veritabanı için aşağıdaki testler yapılmaktadır;
- Slow Query Log
- Max Connections
- Worker Threads
- Key Buffer
- Query Cache
- Sort Buffer
- Joins
- Temp Tables
- Table (Open & Definition) Cache
- Table Locking
- Table Scans (read_buffer)
- Innodb Status
https://launchpad.net/mysql-tuning-primer/trunk adresinde yer alan tuning-primer.sh betiğini indirip, çalıştırma yetkisi vererek, testlere başlayabiliriz.
# chmod u+x tuning-primer.sh
# ./tuning-primer.sh
1.Adım:
– MYSQL PERFORMANCE TUNING PRIMER — — By: Matthew Montgomery — MySQL Version 5.0.95 x86_64 Uptime = 6 days 1 hrs 19 min 4 sec Avg. qps = 39 Total Questions = 20796802Threads Connected = 9Server has been running for over 48hrs.It should be safe to follow these recommendationsTo find out more information on how each of theseruntime variables effects performance visit:http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.htmlVisit http://www.mysql.com/products/enterprise/advisors.htmlfor info about MySQL’s Enterprise Monitoring and Advisory ServiceSLOW QUERIESThe slow query log is NOT enabled.Current long_query_time = 10 sec.You have 3510 out of 20796847 that take longer than 10 sec. to completeYour long_query_time seems to be fineBINARY UPDATE LOGThe binary update log is NOT enabled.You will not be able to do point in time recoverySee http://dev.mysql.com/doc/refman/5.0/en/point-in-time-recovery.htmlWORKER THREADSCurrent thread_cache_size = 0Current threads_cached = 0Current threads_per_sec = 1Historic threads_per_sec = 1Your thread_cache_size is fineMAX CONNECTIONSCurrent max_connections = 100Current threads_connected = 9Historic max_used_connections = 101The number of used connections is 101% of the configured maximum.You should raise max_connections
Betik, bold olarak işaretli olan satırda, max_connections sayısının yetersiz kaldığını ve bu sayının artırılması gerektiğini belirtiyor. Yani test sonucunda, MySQL bağlantılarında bu parametreyle ilgili sıkıntı yaşanmış.
MySQL konfigürasyonunda max_connections sayısı artırılarak, çözüme ulaşılabilir.
Çözüm:
# vi /etc/my.cnf
…
..
.
# MySQL başlığı altına;
Current max_connections = 100
2.Adım:
INNODB STATUS Current InnoDB index space = 20 MCurrent InnoDB data space = 24 MCurrent InnoDB buffer pool free = 0 %Current innodb_buffer_pool_size = 8 MDepending on how much space your innodb indexes take up it may be safeto increase this value to up to 2 / 3 of total system memoryMEMORY USAGEMax Memory Ever Allocated : 295 MConfigured Max Per-thread Buffers : 274 MConfigured Max Global Buffers : 17 MConfigured Max Memory Limit : 292 MPhysical Memory : 3.83 GMax memory limit seem to be within acceptable normsKEY BUFFERCurrent MyISAM index space = 488 MCurrent key_buffer_size = 7 MKey cache miss rate is 1 : 11Key buffer free ratio = 80 %Your key_buffer_size seems to be fineQUERY CACHEQuery cache is supported but not enabledPerhaps you should set the query_cache_size
İkinci adım olarak query_cache_size alanından dolayı uyarı aldık. Bunun için; query_cache_size limitini varolan değer olarak 128 MB belirtilebilir ya da MySQL Tuner (http://mysqltuner.pl/mysqltuner.pl) aracı ile bu alan belirlenebilir.
Çözüm:
# vi /etc/my.cnf
…
..
.
# MySQL başlığı altına;
query_cache_size=128M
query_cache_limit=1M
3.Adım:
Betiği çalıştırmaya devam ederek, optimizasyona devam ediyoruz.
SORT OPERATIONS Current sort_buffer_size = 2 MCurrent read_rnd_buffer_size = 256 KSort buffer seems to be fineJOINSCurrent join_buffer_size = 132.00 KYou have had 7728 queries where a join could not use an index properlyYou have had 7212 joins without keys that check for key usage after each rowYou 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 yourjoin_buffer_size to accommodate larger joins in one pass.Note! This script will still suggest raising the join_buffer_size whenANY joins not using indexes are found.
Betik, log-slow-queries parametresinin eklenmesini tavsiye ediyor.
Çözüm:
# vi /etc/mysql/my.cnf
…
..
.
log-slow-queries = /var/log/mysql/slow.log
4.Adım:
OPEN FILES LIMIT Current open_files_limit = 1024 filesThe open_files_limit should typically be set to at least 2x-3xthat of table_cache if you have heavy MyISAM usage.Your open_files_limit value seems to be fine TABLE CACHECurrent table_cache value = 64 tablesYou have a total of 12326 tablesYou have 64 open tables.Current table_cache hit rate is 0%, while 100% of your table cache is in useYou should probably increase your table_cache
Çıkan uyarıda, table_cache alanının yetersiz olduğuna dair uyarı veriliyor.
Yaklaşık olarak;
12326 x 4 = 49304 → 2 ve katları olduğu için 64535 olarak tespid edilir.
NOT: table_cache alanının çok artırılması durumunda RAM kaybının olacağı göz önünde bulundurulmalıdır.
Çözüm:
# vi /etc/mysql/my.cnf
…
..
.
table_cache= 64535
5.Adım:
TEMP TABLES Current max_heap_table_size = 16 MCurrent tmp_table_size = 32 MOf 356862 temp tables, 22% were created on diskEffective in-memory tmp_table_size is limited to max_heap_table_size.Created disk tmp tables ratio seems fineTABLE SCANSCurrent read_buffer_size = 128 KCurrent table scan ratio = 851 : 1read_buffer_size seems to be fineTABLE LOCKINGCurrent Lock Wait ratio = 1 : 104You may benefit from selective use of InnoDB.If you have long running SELECT’s against MyISAM tables and performfrequent updates consider setting ‘low_priority_updates=1′If you have a high concurrency of inserts on Dynamic row-length tablesconsider setting ‘concurrent_insert=2′.
thread_concurrency = 8 — → CPU x 2 olarak girebilirsiniz.
# Bin dosyasının aktif edilmesi hatalı tabloların ileride tamir edilmesine yardımcı olacaktır.
log-bin=/var/lib/mysql/mysql-bin
# binary logging format — mixed recommended
binlog_format=mixed
# replikasyon yapılacak ise, karşı sunucu bilgileri girilip
# yorum alanlarının kaldırılması gerekiyor. Master için Id 1 Slave için 2
# server-id = 1
# CHANGE MASTER TO MASTER_HOST=’125.564.12.1′
# MASTER_PORT=3306
# MASTER_USER=’joe’
# MASTER_PASSWORD=’secret’
innodb_data_home_dir = /var/lib/mysql
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /var/lib/mysql
innodb_buffer_pool_size = 256M
innodb_additional_mem_pool_size = 20M
innodb_log_file_size = 64M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
Ayarların geçerli olabilmesi için MySQL yeniden başlatılır.
NOT: Herhangi bir hataya karşı, logları kontrol edilmesini tavsiye ediyorum.
MySQL Check
MySQL veritabanı performans testi için diğer bir araç olarak, MysqlCheck’i kullanacağız.
1. Veritabanı Kontrolü
# mysqlcheck -cA -u root -pEnter password: company.contacts OK
mysql.columns_priv OK
mysql.db OK
mysql.event OK
mysql.func OK
mysql.general_log
Error : You can't use locks with log tables.
status : OK
mysql.help_category OK
mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.host OK
mysql.ndb_binlog_index OK
mysql.plugin OK
mysql.proc OK
mysql.procs_priv OK
mysql.servers OK
mysql.slow_log
Error : You can't use locks with log tables.
status : OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK
mysql.time_zone_name OK
mysql.time_zone_transition OK
mysql.time_zone_transition_type OK
mysql.user OK
phpmyadmin.pma_bookmark OK
phpmyadmin.pma_column_info OK
phpmyadmin.pma_designer_coords OK
phpmyadmin.pma_history OK
phpmyadmin.pma_pdf_pages OK
phpmyadmin.pma_relation OK
phpmyadmin.pma_table_coords OK
phpmyadmin.pma_table_info OK
phpmyadmin.pma_tracking OK
phpmyadmin.pma_userconfig OK
2. Optimizasyon
$ mysqlcheck -oA -u root -p
(-o for optimize)
$ mysqlcheck -oA -u root -p
Enter password: company.contacts Table is already up to date
mysql.columns_priv Table is already up to date
mysql.db OK
mysql.event Table is already up to date
mysql.func Table is already up to date
mysql.general_log
note : The storage engine for the table doesn't support optimize
mysql.help_category OK
mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.host Table is already up to date
mysql.ndb_binlog_index Table is already up to date
mysql.plugin Table is already up to date
mysql.proc Table is already up to date
mysql.procs_priv OK
mysql.servers Table is already up to date
mysql.slow_log
note : The storage engine for the table doesn't support optimize
mysql.tables_priv OK
mysql.time_zone Table is already up to date
mysql.time_zone_leap_second Table is already up to date
mysql.time_zone_name Table is already up to date
mysql.time_zone_transition Table is already up to date
mysql.time_zone_transition_type Table is already up to date
mysql.user OK
phpmyadmin.pma_bookmark Table is already up to date
phpmyadmin.pma_column_info Table is already up to date
phpmyadmin.pma_designer_coords Table is already up to date
phpmyadmin.pma_history Table is already up to date
phpmyadmin.pma_pdf_pages Table is already up to date
phpmyadmin.pma_relation Table is already up to date
phpmyadmin.pma_table_coords Table is already up to date
phpmyadmin.pma_table_info Table is already up to date
phpmyadmin.pma_tracking Table is already up to date
phpmyadmin.pma_userconfig OK
3. Bozuk Veritabanlarının Onarımı
$ mysqlcheck -rA -u root -p
(-r for repair)
$ mysqlcheck -rA -u root -p
Enter password: company.contacts OK
mysql.columns_priv OK
mysql.db OK
mysql.event OK
mysql.func OK
mysql.general_log OK
mysql.help_category OK
mysql.help_keyword OK
mysql.help_relation OK
mysql.help_topic OK
mysql.host OK
mysql.ndb_binlog_index OK
mysql.plugin OK
mysql.proc OK
mysql.procs_priv OK
mysql.servers OK
mysql.slow_log OK
mysql.tables_priv OK
mysql.time_zone OK
mysql.time_zone_leap_second OK
mysql.time_zone_name OK
mysql.time_zone_transition OK
mysql.time_zone_transition_type OK
mysql.user OK
phpmyadmin.pma_bookmark OK
phpmyadmin.pma_column_info OK
phpmyadmin.pma_designer_coords OK
phpmyadmin.pma_history OK
phpmyadmin.pma_pdf_pages OK
phpmyadmin.pma_relation OK
phpmyadmin.pma_table_coords OK
phpmyadmin.pma_table_info OK
phpmyadmin.pma_tracking OK
phpmyadmin.pma_userconfig OK
(Orijinal Yayınlanma Tarihi: 12/08/2013)
Kaynaklar: