Monitor MySQL query performance with general and slow query logs in Lightsail
The general and slow query logs are disabled by default for MySQL databases in Amazon Lightsail. You can enable these logs, and begin collecting data, by updating a few database parameters. Update the database parameters by using the Lightsail API, AWS Command Line Interface (AWS CLI), or SDKs. In this guide, we show you how to use the AWS CLI to update your database parameters and enable the general and slow query logs. We also provide additional options for controlling the general and slow query logs, and how log data retention is handled.
Prerequisite
If you haven't done so already, install and configure the AWS CLI. For more information, see Configure the AWS Command Line Interface to work with Amazon Lightsail.
Enable the general and slow query logs in the Lightsail console
To enable the general and slow query logs in the Lightsail console, you must update the
general_log and slow_query_log database parameters with a value of
1, and the log_output parameter with a value of
FILE.
To enable the general and slow query logs in the Lightsail console
-
Open a Terminal or Command Prompt window.
-
Enter the following command to update the
general_logparameter to a value of1, which is true, or enabled.aws lightsail update-relational-database-parameters --regionRegion--relational-database-nameDatabaseName--parameters "parameterName=general_log,parameterValue=1,applyMethod=pending-reboot"In the command, replace:
-
DatabaseNamewith the name of your database. -
Regionwith the AWS Region of your database.
-
-
Enter the following command to update the
slow_query_logparameter to a value of1, which is true, or enabled.aws lightsail update-relational-database-parameters --regionRegion--relational-database-nameDatabaseName--parameters "parameterName=slow_query_log,parameterValue=1,applyMethod=pending-reboot"In the command, replace:
-
DatabaseNamewith the name of your database. -
Regionwith the AWS Region of your database.
-
-
Enter the following command to update the
log_outputparameter to a value ofFILE, which writes the log data to a system file and enables it to be displayed in the Lightsail console.aws lightsail update-relational-database-parameters --regionRegion--relational-database-nameDatabaseName--parameters "parameterName=log_output,parameterValue=FILE,applyMethod=pending-reboot"In the command, replace:
-
DatabaseNamewith the name of your database. -
Regionwith the AWS Region of your database.
-
-
Enter the following command to reboot the database and make the changes effective.
aws lightsail reboot-relational-database --regionRegion--relational-database-nameDatabaseNameIn the command, replace:
-
DatabaseNamewith the name of your database. -
Regionwith the AWS Region of your database.
At this point, your database becomes unavailable while it reboots. Wait a few minutes, then sign in to the Lightsail console
to view the general and slow query logs for your database. For more information, see Viewing your database logs and history in Amazon Lightsail. Note
For more information about updating database parameters, see Updating database parameters in Amazon Lightsail.
-
Control additional database log options
To control additional options for the MySQL general and slow query logs, update the following parameters:
-
log_output— Set this parameter toTABLE. This writes general queries to themysql.general_logtable, and slow queries to themysql.slow_logtable. You can also set thelog_outputparameter toNONEto disable logging.Note
Setting the
log_outputparameter toTABLEdisables the general and slow query log data from displaying in the Lightsail console. Instead, you must refer to themysql.general_logandmysql.slow_logtables on your database to view the log data. -
long_query_time— To prevent fast-running queries from being logged in the slow query log, specify a value for the shortest query execution time to be logged, in seconds. The default is 10 seconds, and the minimum is 0. If thelog_outputparameter is set toFILE, you can specify a floating point value that goes to microsecond resolution. If thelog_outputparameter is set toTABLE, you must specify an integer value with second resolution. Only queries whose execution time exceeds thelong_query_timeparameter value are logged. For example, settinglong_query_timeto 0.1 prevents any query that runs for less than 100 milliseconds from being logged. -
log_queries_not_using_indexes— To log all queries that do not use an index to the slow query log, set to 1. The default is 0. Queries that do not use an index are logged even if their execution time is less than the value of thelong_query_timeparameter.
Log data retention
When logging is enabled, table logs are rotated, or log files are deleted, at regular
intervals. This measure is a precaution to reduce the possibility of a large log file either
blocking database use or affecting performance. When the log_output parameter is
set to FILE or TABLE, logging is handled as follows:
-
When
FILElogging is enabled, log files are examined every hour and log files older than 24 hours are deleted. In some cases, the remaining combined log file size after the deletion might exceed the threshold of 2 percent of a database's allocated space. In these cases, the largest log files are deleted until the log file size no longer exceeds the threshold. -
When
TABLElogging is enabled, log tables are rotated every 24 hours in some cases.This rotation occurs if the space used by the table logs is more than 20 percent of the allocated storage space or the size of all logs combined is greater than 10 GB.
If the amount of space used for a database is greater than 90 percent of the database's allocated storage space, then the thresholds for log rotation are reduced.
Log tables are then rotated if the space used by the table logs is more than 10 percent of the allocated storage space or the size of all logs combined is greater than 5 GB.
You can subscribe to the
low_free_storageevent to be notified when log tables are rotated to free up space.-
When log tables are rotated, the current log table is copied to a backup log table and the entries in the current log table are removed. If the backup log table already exists, then it is deleted before the current log table is copied to the backup. You can query the backup log table. The backup log table for the
mysql.general_logtable is namedmysql.general_log_backup. The backup log table for themysql.slow_logtable is namedmysql.slow_log_backup. -
You can rotate the
mysql.general_logtable by calling themysql.rds_rotate_general_logprocedure. You can rotate themysql.slow_logtable by calling themysql.rds_rotate_slow_logprocedure. -
Table logs are rotated during a database version upgrade.
-