Enabling MySQL slow query logs
During performance troubleshooting, it can be helpful to know which queries are taking a long time to return a result. MySQL provides a way to log slow running queries that meet certain criteria. This option is disabled by default.
To avoid false positives, you should set the threshold for time to a value high enough that you won't capture query sent to the database, but just the long running ones. The default of 0.5 seconds should be enough to begin, however you may need to increment this upwards if you find too much noise in your slow query log file.
As this procedure requires a restart of MySQL (and thus a restart of your applications) you may wish to schedule some downtime for this to occur with minimal disruption to your user base.
Where is the MySQL Configuration file located?
The location of the MySQL configuration file (either
my.ini depends on your operating system). MySQL will look at each location in order, and use the first file that it comes across. Often, if the file does not exist, it must be created first.
INSTALLDIR refers to the location that MySQL was installed to.
Linux based systems
Linux based MySQL systems will use configuration files in the following order of precedence
SYSCONFDIR refers to the directory specified when MySQL was built; and typically reverse to the
etc directory located under the compiled-in installation directory.
MYSQL_HOME is an environment variable referring to the path where
my.cnf can be found.
Regarding MySQL Safe ModeWhen MySQL is started using
mysqld_safe, MySQL will automatically set
MYSQL_HOMEif no such variable exists already. If a
my.cnffile exists in the data directory (typically
/usr/local/mysql/data), then that file will be used. Otherwise, the installation directory will be used (typically
/usr/local/var). See the MySQL Documentation for more information on how to configure option files.
Windows based systems
Windows based MySQL systems will use the configuration files in the following order of precedence
%PROGRAMDATA%\MySQL\<MySQL Server Version>\my.ini
<MySQL Server Version>\my.cnf
Enabling Slow Query Profiling
- Shut down your applications using MySQL
- Shut down MySQL itself
Add the following configuration options to
# What's the threshold for a slow query to be logged? long_query_time=0.5 # Where should the queries be logged to? slow_query_log_file=/path/to/logfile # Enable slow query logging - note the dashes rather than underscores slow-query-log=1
- Save the file, and restart MySQL
- Restart your connected applications
Once you have the appropriate logs, follow the instructions at: Troubleshooting slow MySQL performance to analyze them.