MySQL AB provides a wealth of information regarding the tuning of server parameters, much of which the average user will never need to use. So, as not to completely overwhelm you with information, this section will contain a few of the more common startup options for a finely tuned MySQL server.
When you start MySQL, a configuration file called my.cnf is loaded. This file contains information ranging from port number to buffer sizes, but can be overruled by command-line startup options. At installation time, my.cnf is placed in the /etc directory, but you can also specify an alternate location for this file during start-up.
In the support-files subdirectory of your MySQL installation directory, you'll find four sample configuration files, each tuned for a specific range of installed memory:
my-small.cnf? For systems with less than 64MB of RAM, where MySQL is used occasionally.
my-medium.cnf? For systems with less than 64MB of RAM, where MySQL is the primary activity on the system, or for systems with up to 128MB of RAM, where MySQL shares the box with other processes. This is the most common configuration, where MySQL is installed on the same box as a Web server and receives a moderate amount of traffic.
my-large.cnf? For a system with 128MB to 512MB of RAM, where MySQL is the primary activity.
my-huge.cnf? For a system with 1GB to 2GB of RAM, where MySQL is the primary activity.
To use any of these as the base configuration file, simply copy the file of your choice to /etc/my.cnf (or wherever my.cnf is on your system) and change any system-specific information, such as port or file locations.
There are two primary start-up parameters that will affect your system the most: key_buffer_size and table_cache. If you get only two server parameters correctly tuned, make sure they're these two!
The value of key_buffer_size is the size of the buffer used with indexes. The larger the buffer, the faster the SQL command will finish and a result will be returned. Try to find the fine line between finely tuned and over-optimized; you might have a key_buffer_size of 256MB on a system with 512MB of RAM, but any more than 256MB could cause degraded server performance.
A simple way to check the actual performance of the buffer is to examine four additional variables: key_read_requests, key_reads, key_write_requests, and key_writes. You can find the values of these variables by issuing the SHOW STATUS command:
mysql> show status;
A long list of variables and values will be returned, listed in alphabetical order. Find the rows that look something like this (your values will differ):
| Key_read_requests | 602843 | | Key_reads | 151 | | Key_write_requests | 1773 | | Key_writes | 805 |
If you divide the value of key_read by the value of key_reads_requests, the result should be less than 0.01. Also, if you divide the value of key_write by the value of key_writes_requests, the result should be less than 1. Using the previous values, we have results of 0.000250479809834401 and 0.454032712915962, respectively, well within the acceptable parameters. To try to get these numbers even smaller, more tuning could occur by increasing the value of key_buffer_size, but these numbers would be fine to leave as they are.
The other important server parameter is table_cache, which is the number of open tables for all threads. The default is 64, but you might need to adjust this number. Using the SHOW STATUS command, look for a variable called open_tables in the output. If this number is large, the value of table_cache should be increased.
The sample configuration files use various combinations of key_buffer_size and table_cache, which you can use as a baseline for any modifications you need to make. Whenever you modify your configuration, you'll be restarting your server for changes to take effect, sometimes with no knowledge of the consequences of your changes. In this case, be sure to try your modifications in a development environment before rolling the changes into production.