Enabling MySQL slow query logs
Platform Notice: Data Center Only - This article only applies to Atlassian products on the Data Center platform.
Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Scenario
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.cnf
or 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
/etc/my.cnf
/etc/mysql/my.cnf
SYSCONFDIR
/my.cnf
$MYSQL_HOME/my.cnf
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 Mode
When MySQL is started using
mysqld_safe
, MySQL will automatically set
MYSQL_HOME
if no such variable exists already. If a
my.cnf
file 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
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
%PROGRAMDATA%
\MySQL\<MySQL Server Version>\my.cnf
%WINDIR%
\my.ini
%WINDIR%
\my.cnf
C:\my.ini
C:\my.cnf
INSTALLDIR
\my.ini
INSTALLDIR
\my.cnf
Enabling Slow Query Profiling
Always back up your data before performing any modifications to the database. If possible, test any alter, insert, update, or delete SQL commands on a staging server first.
Shut down your applications using MySQL
Shut down MySQL itself
Add the following configuration options to
my.ini
/my.cnf
:1 2 3 4 5 6 7 8
# 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.
Was this helpful?