In this article, we will cover the basics of MySQL server optimization. For server example, we will take VPS plan with 4 GHz CPU | 4 GB RAM | 50 GB storage | 4 TB bandwidth and configure MySQL for optimal resource usage.
Variables by formula
For MySQL tune, please open my.cnf file:
Example of some variables for VPS with 4 GHz CPU | 4 GB RAM | 50 GB storage | 4 TB bandwidth:
• query_cache_size=12.5% from 4096M=512M;
• key_buffer_size=12.5% from 4096M=512M;
• tmp_table_size=6.5% from 4096M=256M;
• max_heap_table_size=6.5% from 4096M=256M.
Example of full MySQL optimization
Below are complete my.cnf example for VPS with 4 GHz CPU | 4 GB RAM | 50 GB storage | 4 TB bandwidth:

After my.cnf modification, please restart MySQL server:
service mysqld restart
If the tmp_table_size and max_heap_table_size variables do not exist in your default configuation, you can insert them manually at the end of [mysqld] section.
Useful links:
MySQL Tuner
MySQL documentation
MySQL memory calculator
my.cnf file analyzer