mysql_slow_query
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| mysql_slow_query [2015/04/05 12:05] – created luke7858 | mysql_slow_query [2024/05/23 07:26] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | === Slow Query Logs === | ||
| + | Checking the slow query logs via mysql prompt. | ||
| + | \\ | ||
| + | \\Note: You CAN enable the slow query logging on runtime. A mysql restart will flush the changes made via runtime. If you wish for persistence then add it to the /etc/my.cnf file: | ||
| + | <sxh bash> | ||
| + | mysql> SHOW GLOBAL VARIABLES LIKE ' | ||
| + | +---------------------+-------------------------------+ | ||
| + | | Variable_name | ||
| + | +---------------------+-------------------------------+ | ||
| + | | slow_query_log | ||
| + | | slow_query_log_file | / | ||
| + | +---------------------+-------------------------------+ | ||
| + | </ | ||
| + | Set the logs to another path: | ||
| + | <sxh bash> | ||
| + | mysql> SET GLOBAL slow_query_log_file = '/ | ||
| + | </ | ||
| + | <sxh bash> | ||
| + | mysql> SET GLOBAL slow_query_log = ' | ||
| + | </ | ||
| + | \\ | ||
| + | Via the command line: | ||
| + | \\ | ||
| + | \\ | ||
| You can check to see if slow query logging is currently enabled. The following command assumes you have configured .my.cnf file: | You can check to see if slow query logging is currently enabled. The following command assumes you have configured .my.cnf file: | ||
| + | <sxh bash> | ||
| + | show variables like " | ||
| + | </ | ||
| + | Or you can run the following from the command line: | ||
| <sxh bash> | <sxh bash> | ||
| mysql -e 'show variables;' | mysql -e 'show variables;' | ||
| </ | </ | ||
| Output should show something similar to: | Output should show something similar to: | ||
| + | <sxh bash> | ||
| + | +---------------------+---------------------------------+ | ||
| + | | Variable_name | ||
| + | +---------------------+---------------------------------+ | ||
| + | | log_slow_queries | ||
| + | | slow_launch_time | ||
| + | | slow_query_log | ||
| + | | slow_query_log_file | / | ||
| + | +---------------------+---------------------------------+ | ||
| + | </ | ||
| + | Or: | ||
| <sxh bash> | <sxh bash> | ||
| slow_query_log | slow_query_log | ||
| slow_query_log_file | slow_query_log_file | ||
| </ | </ | ||
| + | \\ | ||
| + | === How many slow queries? === | ||
| + | You can also use the following command to view if you currently have any slow queries (if logging has been enabled): | ||
| + | <sxh bash> | ||
| + | show status like ' | ||
| + | </ | ||
| + | Or from the command line: | ||
| + | <sxh bash> | ||
| + | mysql -e 'show status;' | ||
| + | </ | ||
| + | This should produce a similar output: | ||
| + | <sxh bash> | ||
| + | Slow_launch_threads | ||
| + | Slow_queries | ||
| + | </ | ||
| + | \\ | ||
| + | ---------------------------------------------------------------- | ||
| + | === Configuring Slow Query logs === | ||
| + | \\ | ||
| + | There are 2 options for configuring slow query logs: | ||
| + | \\ | ||
| + | \\ | ||
| + | -** Run time** - this will configure the slow query logs in the current runtime environment, | ||
| + | \\ | ||
| + | \\ | ||
| + | - **Permanent** - editing the /etc/my.cnf will allow mysql to retain the values once the service has been restarted. Changing this file will NOT change the current runtime environment. | ||
| + | \\ | ||
| + | \\ | ||
| + | **__NOTE__**: | ||
| + | \\ | ||
| + | \\ | ||
| + | === Run-time Configuration === | ||
| + | \\ If run the following command it will set slow query logging without needing to restart mysql | ||
| + | <sxh bash> | ||
| + | mysql -e 'set global log_slow_queries = 1;' | ||
| + | </ | ||
| + | \\ You can now run the following command and it should produce an output saying that slow_query logging is enabled: | ||
| + | <sxh bash> | ||
| + | mysql -e 'show variables;' | ||
| + | </ | ||
| + | \\ | ||
| + | |||
| + | === /etc/my.cnf - Permanent Configuration === | ||
| + | <sxh bash> | ||
| + | slow_query_log = 1 | ||
| + | slow_query_log_file = / | ||
| + | long_query_time = 10 | ||
| + | log_queries_not_using_indexes = 1 | ||
| + | </ | ||
| + | **Note**: Change permissions so that mysqld can write to the specified log file. Giving write permissions to the ' | ||
mysql_slow_query.1428235556.txt.gz · Last modified: 2024/05/23 07:26 (external edit)
