5.4.1 Selecting General Query Log and Slow Query Log Output Destinations选择常规查询日志和慢速查询日志输出目的地

MySQL Server provides flexible control over the destination of output written to the general query log and the slow query log, if those logs are enabled. MySQL Server提供了对写入常规查询日志和慢速查询日志的输出目的地的灵活控制(如果启用了这些日志的话)。Possible destinations for log entries are log files or the general_log and slow_log tables in the mysql system database. 日志条目的可能目的地是日志文件或mysql系统数据库中的general_logslow_log表。File output, table output, or both can be selected.可以选择文件输出、表输出,也可以同时选择两者。

Log Control at Server Startup服务器启动时的日志控制

The log_output system variable specifies the destination for log output. log_output系统变量指定日志输出的目标。Setting this variable does not in itself enable the logs; they must be enabled separately.设置此变量本身不会启用日志;它们必须单独启用。

  • If log_output is not specified at startup, the default logging destination is FILE.如果在启动时未指定log_output,则默认的日志记录目标为FILE

  • If log_output is specified at startup, its value is a list one or more comma-separated words chosen from TABLE (log to tables), FILE (log to files), or NONE (do not log to tables or files). 如果在启动时指定了log_output,则其值是从TABLE(记录到表)、FILE(记录到文件)或NONE(不记录到表或文件)中选择的一个或多个逗号分隔单词的列表。NONE, if present, takes precedence over any other specifiers.NONE(如果存在)优先于任何其他说明符。

The general_log system variable controls logging to the general query log for the selected log destinations. general_log系统变量控制所选日志目的地的常规查询日志的日志记录。If specified at server startup, general_log takes an optional argument of 1 or 0 to enable or disable the log. 如果在服务器启动时指定,general_log将采用可选参数1或0来启用或禁用日志。To specify a file name other than the default for file logging, set the general_log_file variable. 要指定文件日志记录的默认文件名以外的文件名,请设置general_log_file变量。Similarly, the slow_query_log variable controls logging to the slow query log for the selected destinations and setting slow_query_log_file specifies a file name for file logging. 类似地,slow_query_log变量控制所选目的地的慢速查询日志记录,设置slow_query_log_file指定文件记录的文件名。If either log is enabled, the server opens the corresponding log file and writes startup messages to it. 如果启用了任一日志,服务器将打开相应的日志文件并向其中写入启动消息。However, further logging of queries to the file does not occur unless the FILE log destination is selected.但是,除非选择了FILE日志目标,否则不会将查询进一步记录到文件中。

Examples:

  • To write general query log entries to the log table and the log file, use --log_output=TABLE,FILE to select both log destinations and --general_log to enable the general query log.要将常规查询日志条目写入日志表和日志文件,请使用--log_output=TABLE,FILE选择日志目标,并使用--general_log启用常规查询日志。

  • To write general and slow query log entries only to the log tables, use --log_output=TABLE to select tables as the log destination and --general_log and --slow_query_log to enable both logs.要仅将常规和慢速查询日志条目写入日志表,请使用--log_output=TABLE选择表作为日志目标,并使用--general_log--slow_query_log启用这两个日志。

  • To write slow query log entries only to the log file, use --log_output=FILE to select files as the log destination and --slow_query_log to enable the slow query log. 要仅将慢速查询日志条目写入日志文件,请使用--log_output=FILE选择文件作为日志目标,并使用--slow_query_log启用慢速查询日志。In this case, because the default log destination is FILE, you could omit the log_output setting.在这种情况下,因为默认的日志目标是FILE,所以可以省略log_output设置。

Log Control at Runtime运行时的日志控制

The system variables associated with log tables and files enable runtime control over logging:与日志表和文件相关联的系统变量使运行时能够控制日志记录:

  • The log_output variable indicates the current logging destination. It can be modified at runtime to change the destination.log_output变量指示当前日志记录目标。可以在运行时对其进行修改以更改目的地。

  • The general_log and slow_query_log variables indicate whether the general query log and slow query log are enabled (ON) or disabled (OFF). general_logslow_query_log变量指示启用(ON)还是禁用(OFF)常规查询日志和慢速查询日志。You can set these variables at runtime to control whether the logs are enabled.您可以在运行时设置这些变量,以控制是否启用日志。

  • The general_log_file and slow_query_log_file variables indicate the names of the general query log and slow query log files. general_log_fileslow_query_log_file变量表示常规查询日志和慢速查询日志文件的名称。You can set these variables at server startup or at runtime to change the names of the log files.您可以在服务器启动时或运行时设置这些变量,以更改日志文件的名称。

  • To disable or enable general query logging for the current session, set the session sql_log_off variable to ON or OFF. 要禁用或启用当前会话的常规查询日志记录,请将会话sql_log_off变量设置为ONOFF(This assumes that the general query log itself is enabled.)(这假设启用了常规查询日志本身。)

Log Table Benefits and Characteristics日志表的优点和特点

The use of tables for log output offers the following benefits:使用表进行日志输出提供了以下好处:

  • Log entries have a standard format. To display the current structure of the log tables, use these statements:日志条目具有标准格式。要显示日志表的当前结构,请使用以下语句:

    SHOW CREATE TABLE mysql.general_log;
    SHOW CREATE TABLE mysql.slow_log;
  • Log contents are accessible through SQL statements. This enables the use of queries that select only those log entries that satisfy specific criteria. 可以通过SQL语句访问日志内容。这允许使用仅选择满足特定条件的日志条目的查询。For example, to select log contents associated with a particular client (which can be useful for identifying problematic queries from that client), it is easier to do this using a log table than a log file.例如,要选择与特定客户端相关联的日志内容(这对于识别来自该客户端的有问题的查询非常有用),使用日志表比使用日志文件更容易做到这一点。

  • Logs are accessible remotely through any client that can connect to the server and issue queries (if the client has the appropriate log table privileges). 可以通过任何可以连接到服务器并发出查询的客户端远程访问日志(如果客户端具有适当的日志表权限)。It is not necessary to log in to the server host and directly access the file system.不需要登录到服务器主机并直接访问文件系统。

The log table implementation has the following characteristics:日志表实现具有以下特点:

  • In general, the primary purpose of log tables is to provide an interface for users to observe the runtime execution of the server, not to interfere with its runtime execution.通常,日志表的主要目的是为用户提供一个界面,以观察服务器的运行时执行情况,而不是干扰其运行时执行。

  • CREATE TABLE, ALTER TABLE, and DROP TABLE are valid operations on a log table. CREATE TABLEALTER TABLEDROP TABLE是对日志表的有效操作。For ALTER TABLE and DROP TABLE, the log table cannot be in use and must be disabled, as described later.对于ALTER TABLEDROP TABLE,日志表不能使用,必须禁用,如稍后所述。

  • By default, the log tables use the CSV storage engine that writes data in comma-separated values format. 默认情况下,日志表使用CSV存储引擎,该引擎以逗号分隔的值格式写入数据。For users who have access to the .CSV files that contain log table data, the files are easy to import into other programs such as spreadsheets that can process CSV input.对于有权访问包含日志表数据的.CSV文件的用户来说,这些文件很容易导入到其他程序中,例如可以处理CSV输入的电子表格。

    The log tables can be altered to use the MyISAM storage engine. 可以更改日志表以使用MyISAM存储引擎。You cannot use ALTER TABLE to alter a log table that is in use. The log must be disabled first. 不能使用ALTER TABLE更改正在使用的日志表。必须先禁用日志。No engines other than CSV or MyISAM are legal for the log tables.除了CSVMyISAM之外,没有其他引擎对日志表是合法的。

    Log Tables and Too many open files Errors. 日志表和“打开的文件太多”错误。 If you select TABLE as a log destination and the log tables use the CSV storage engine, you may find that disabling and enabling the general query log or slow query log repeatedly at runtime results in a number of open file descriptors for the .CSV file, possibly resulting in a Too many open files error. 如果选择TABLE作为日志目标,并且日志表使用CSV存储引擎,则可能会发现在运行时重复禁用和启用常规查询日志或慢速查询日志会导致.CSV文件出现许多打开的文件描述符,可能会导致“打开的文件太多”错误。To work around this issue, execute FLUSH TABLES or ensure that the value of open_files_limit is greater than the value of table_open_cache_instances.要解决此问题,请执行FLUSH TABLES或确保open_files_limit的值大于table_open_cache_instances的值。

  • To disable logging so that you can alter (or drop) a log table, you can use the following strategy. The example uses the general query log; the procedure for the slow query log is similar but uses the slow_log table and slow_query_log system variable.要禁用日志记录以便更改(或删除)日志表,可以使用以下策略。该示例使用常规查询日志;慢速查询日志的过程类似,但使用了slow_log表和slow_query_log系统变量。

    SET @old_log_state = @@GLOBAL.general_log;
    SET GLOBAL general_log = 'OFF';
    ALTER TABLE mysql.general_log ENGINE = MyISAM;
    SET GLOBAL general_log = @old_log_state;
  • TRUNCATE TABLE is a valid operation on a log table. It can be used to expire log entries.是对日志表的有效操作。它可用于使日志条目过期。

  • RENAME TABLE is a valid operation on a log table. 是对日志表的有效操作。You can atomically rename a log table (to perform log rotation, for example) using the following strategy:您可以使用以下策略对日志表进行原子重命名(例如,执行日志旋转):

    USE mysql;
    DROP TABLE IF EXISTS general_log2;
    CREATE TABLE general_log2 LIKE general_log;
    RENAME TABLE general_log TO general_log_backup, general_log2 TO general_log;
  • CHECK TABLE is a valid operation on a log table.是对日志表的有效操作。

  • LOCK TABLES cannot be used on a log table.不能在日志表上使用。

  • INSERT, DELETE, and UPDATE cannot be used on a log table. These operations are permitted only internally to the server itself.INSERTDELETEUPDATE不能用于日志表。这些操作只能在服务器本身内部进行。

  • FLUSH TABLES WITH READ LOCK and the state of the read_only system variable have no effect on log tables. FLUSH TABLES WITH READ LOCKread_only系统变量的状态对日志表没有影响。The server can always write to the log tables.服务器始终可以写入日志表。

  • Entries written to the log tables are not written to the binary log and thus are not replicated to replicas.写入日志表的条目不会写入二进制日志,因此不会复制到副本。

  • To flush the log tables or log files, use FLUSH TABLES or FLUSH LOGS, respectively.要刷新日志表或日志文件,请分别使用FLUSH TABLESFLUSH LOGS

  • Partitioning of log tables is not permitted.不允许对日志表进行分区。

  • A mysqldump dump includes statements to recreate those tables so that they are not missing after reloading the dump file. Log table contents are not dumped.mysqldump转储包含用于重新创建这些表的语句,以便在重新加载转储文件后不会丢失这些表。日志表内容不会转储。