Müşterilerimiz İletişim EN

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ği 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 = 20796802
Threads Connected = 9
Server has been running for over 48hrs.
It should be safe to follow these recommendations
To find out more information on how each of these
runtime variables effects performance visit:
http://dev.mysql.com/doc/refman/5.0/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 NOT enabled.
Current long_query_time = 10 sec.
You have 3510 out of 20796847 that take longer than 10 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.0/en/point-in-time-recovery.html
WORKER THREADS
Current thread_cache_size = 0
Current threads_cached = 0
Current threads_per_sec = 1
Historic threads_per_sec = 1
Your thread_cache_size is fine
MAX CONNECTIONS
Current max_connections = 100
Current threads_connected = 9
Historic max_used_connections = 101
The number of used connections is 101% of the configured maximum.
You should raise max_connections

Betik, sondan ikinci satırda, max_connection sayısının yetersiz kaldığını ve bu sayının artırılması gerektiğini belirtiyor. Yani test sonucu, MySQL baglantı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 safe
to increase this value to up to 2 / 3 of total system memory
MEMORY USAGE
Max Memory Ever Allocated : 295 M
Configured Max Per-thread Buffers : 274 M
Configured Max Global Buffers : 17 M
Configured Max Memory Limit : 292 M
Physical Memory : 3.83 G
Max memory limit seem to be within acceptable norms
KEY BUFFER
Current MyISAM index space = 488 M
Current key_buffer_size = 7 M
Key cache miss rate is 1 : 11
Key buffer free ratio = 80 %
Your key_buffer_size seems to be fine
QUERY CACHE
Query cache is supported but not enabled
Perhaps 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 K
You have had 7728 queries where a join could not use an index properly
You have had 7212 joins without keys that check for key usage after each row
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.

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 CACHE
Current table_cache value = 64 tables
You have a total of 12326 tables
You have 64 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

Çı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 tespit 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 disk
Effective in-memory tmp_table_size is limited to max_heap_table_size.
Created disk tmp tables ratio seems fine
TABLE SCANS
Current read_buffer_size = 128 K
Current table scan ratio = 851 : 1
read_buffer_size seems to be fine
TABLE LOCKING
Current Lock Wait ratio = 1 : 104
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=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 -p
Enter 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

Kaynaklar;


Yazan: Kartaca