How to Optimize MySQL by Editing the my.cnf File on Linux Servers

Diego Cortés
Diego Cortés
September 17, 2025
How to Optimize MySQL by Editing the my.cnf File on Linux Servers

Database optimization is an essential practice to ensure adequate performance in high-load environments. MySQL, one of the most popular database management systems, is frequently used in web applications built with PHP. Since Oracle Corporation acquired MySQL following the purchase of Sun Microsystems in 2010, various debates have arisen about its future as open-source software. Despite this, the community has also embraced alternatives like MariaDB, a fork that maintains the open-source approach and often provides advantages in terms of performance in high-demand situations.

This article will focus on how to optimize MySQL by editing the my.cnf configuration file on Linux servers, thus providing a valuable tool for both system administrators and developers looking to improve their database performance.

Why is it important to optimize MySQL?

The goal of optimization is to enable the MySQL server to handle requests more efficiently, using CPU, RAM, and disk I/O resources more effectively. This not only helps to improve response speeds when accessing data but also contributes to the overall stability of the system.

Location of the my.cnf File

Most MySQL configuration is done through a single file: my.cnf. On Linux systems, this file is commonly located in the /etc directory. To edit it using the nano text editor, the command would be:

nano /etc/my.cnf

When opening this file, you may not find all the necessary parameters, as they do not always appear by default. Therefore, some parameters will need to be added manually.

It is vital to exercise caution when modifying this file, as improper configurations can consume all system resources or even prevent the MySQL service from starting, especially in production environments.

Key Parameters for MySQL Optimization

Here are some important parameters that can be adjusted in the my.cnf file to improve the performance and stability of the MySQL server:

1. query_cache_type

Controls whether query caching is enabled or disabled. A recommended value is 1, which activates caching. The value 0 disables it, while 2 enables it on demand.

2. max_allowed_packet

Determines the maximum size of a packet that the server can handle. It is advisable to increase this value when importing large databases.

3. query_cache_size

Defines the size of the query cache, typically set to 64 MB for every 1 GB of RAM available on the server.

4. key_buffer_size

Sets the size of the index cache, affecting the execution speed of SQL queries. A typical size is 32 MB for every 1 GB of usable physical RAM.

5. table_cache

Specifies the maximum number of tables that can be opened simultaneously. A common value is 64, although it can be adjusted based on recommendations from tools like MySQLTuner.

6. Other Important Parameters

  • sort_buffer_size: 1 MB for every 1 GB of RAM.
  • read_buffer_size, read_rnd_buffer_size, join_buffer_size: It is also recommended to have 1 MB of cache for every 1 GB of RAM.
  • thread_cache_size: Set between 32 and 64 for standard usage.
  • tmp_table_size: Sets the maximum size of a temporary table in RAM.
  • max_connections: Defines the maximum number of simultaneous connections accepted by the server.
  • innodb_buffer_pool_size: It is suggested to be 70-80% of total RAM, optimizing the performance of InnoDB tables.

Using MySQLTuner for Optimization

MySQLTuner is a Perl script that helps identify optimal configurations for the my.cnf file after analyzing the performance and usage of the MySQL service over a period of time. It is compatible with MySQL versions from 3.23 to 5.7 and with MariaDB from 5.5 to 10.1, and runs on Linux and BSD operating systems.

It is advisable to run MySQLTuner after the server has been running continuously for at least 24 hours, although a period of 48-72 hours may provide more representative data. The generated report will indicate recommended adjustments and parameters that may need changes.

To run MySQLTuner, you can first download the script using the following command:

wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl -O mysqltuner.pl

Then, to execute it, use:

perl mysqltuner.pl

Importance of SSDs in MySQL Performance

The type of hardware also plays a crucial role in MySQL performance, especially in large databases or in systems with a high workload. SSDs have been shown to significantly reduce latencies in read and write operations.

In various performance tests, SSDs have been found to outperform HDDs notably. For example, a benchmark in MS SQL Server showed that:

  • SSD: 0.917 seconds for 5000 inserts.
  • HDD: 2.12 seconds for the same operation.

The difference in response times is even more pronounced in environments where HDD fragmentation affects performance.

Conclusion

Optimizing MySQL through adjustments in the my.cnf file is a critical process to maximize performance and ensure server stability. The combination of well-planned configurations and suitable hardware, such as SSDs, can lead to significant improvements in database management.

For more information on database optimization and other technology topics, readers are invited to explore more on the blog.

Article information

Published: September 17, 2025
Category: MYSQL Tutorials
Reading time: 5-8 minutes
Difficulty: Intermediate

Key tips

1

Take your time to understand each concept before moving on to the next one.

2

Practice the examples in your own development environment for better understanding.

3

Don't hesitate to review the additional resources mentioned in the article.

Diego Cortés
Diego Cortés
Full Stack Developer, SEO Specialist with Expertise in Laravel & Vue.js and 3D Generalist

Frequently Asked Questions

Categories

Page loaded in 25.96 ms