29 julio 2014

Optimizando MySQL para VPS

Desde hace algunos años la tecnología de virtualización cada vez toma más auge. Muchas personas contratan servicios de VPS porque pueden tener todas las ventajas de administración de un root por un precio muy bajo. No todo es bueno, ya que con la tecnología de virtualización se pierden recursos, ya que estos están limitados (Memoria, CPU, Disco), ya que el servidor físico puede ser compartido por innumerables servidores virtuales (y compiten recursos de la misma, como es el caso de OpenVZ).
La configuración que viene por defecto del MySQL en la mayoría de las distribuciones de GNU/Linux trae parámetros por defecto para servidores físicos, al instalarse en un VPS, el mismo, si no se le hace una buena afinación podría colapsar. Me sucedió recientemente que el MySQL que trae por defecto Debian Wheezy si se generan varias instancias cuelga a un VPS de 512 MB de RAM. Para poder generar los ajustes, conseguí una herramienta que aliaza el rendimiento llamada Tuning-Primer , de Matthew Montgomery, un pequeño script en bash que te da consejos para establecer algunas variables de la configuración, basándose en los parametros que proporciona el servidor.
Su instalación es sumamente sencillo, solo requiere que tengamos un usuario/password en la base de datos y el programa bc instalado:

# wget http://www.day32.com/MySQL/tuning-primer.sh
# chmod u+x tuning-primer.sh
# ./tuning-primer.sh


Un ejemplo del reporte de esta herramienta es la siguiente:

– MYSQL PERFORMANCE TUNING PRIMER –
- By: Matthew Montgomery -
MySQL Version 5.5.24-4 x86_64
Uptime = 0 days 12 hrs 17 min 4 sec
Avg. qps = 0
Total Questions = 22445
Threads Connected = 1
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 NOT enabled.
Current long_query_time = 10.000000 sec.
You have 0 out of 22466 that take longer than 10.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 = 8
Current threads_cached = 1
Current threads_per_sec = 0
Historic threads_per_sec = 0
Your thread_cache_size is fine
MAX CONNECTIONS
Current max_connections = 128
Current threads_connected = 1
Historic max_used_connections = 2
The number of used connections is 1% of the configured maximum.
You are using less than 10% of your configured max_connections.
Lowering max_connections could help to avoid an over-allocation of memory
See “MEMORY USAGE” section to make sure you are not over-allocating
INNODB STATUS
Current InnoDB index space = 0 bytes
Current InnoDB data space = 0 bytes
Current InnoDB buffer pool free = 98 %
Current innodb_buffer_pool_size = 128 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 : 173 M
Configured Max Per-thread Buffers : 344 M
Configured Max Global Buffers : 168 M
Configured Max Memory Limit : 512 M
Physical Memory : 496 M
Max memory limit exceeds 90% of physical memory
KEY BUFFER
Current MyISAM index space = 9 M
Current key_buffer_size = 16 M
Key cache miss rate is 1 : 158
Key buffer free ratio = 67 %
Your key_buffer_size seems to be fine
QUERY CACHE
Query cache is enabled
Current query_cache_size = 8 M
Current query_cache_used = 3 M
Current query_cache_limit = 1 M
Current Query cache Memory fill ratio = 48.41 %
Current query_cache_min_res_unit = 4 K
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 = 256 K
Sort buffer seems to be fine
JOINS
Current join_buffer_size = 132.00 K
You have had 0 queries where a join could not use an index properly
Your joins seem to be using indexes properly
OPEN FILES LIMIT
Current open_files_limit = 65562 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 = 32712 tables
Current table_definition_cache = 400 tables
You have a total of 101 tables
You have 101 open tables.
The table_cache value seems to be fine
TEMP TABLES
Current max_heap_table_size = 16 M
Current tmp_table_size = 32 M
Of 1117 temp tables, 30% were created on disk
Effective in-memory tmp_table_size is limited to max_heap_table_size.
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 = 128 K
Current table scan ratio = 17 : 1
read_buffer_size seems to be fine
TABLE LOCKING
Current Lock Wait ratio = 0 : 22724
Your table locking seems to be fine

Por ejemplo, yo para un VPS con 512 de RAM (que corre únicamente MySQL) usé la siguiente configuración en el /etc/mysql/my.cnf

max_connections=128
wait_timeout=30
thread_cache_size=8
table_cache=32712
key_buffer_size=16M
query-cache-type=1
query_cache_size=8M
tmp_table_size=32M


Esta herramienta es más efectiva si se correo luego de tener más de 48 horas prestando servicio. Si se logra optimizar el MySQL en un VPS se puede lograr aprovechar el máximo de los recursos sin temer que en algún momento pueda dejar de prestar servicios.

Tomado de: http://apostols.net/?p=291

No hay comentarios: