8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 7 MySQL 服务器管理 / 7.4 MySQL 服务器日志 / 7.4.1 选择普通查询日志和慢查询日志输出目的地
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

7.4.1 选择通用查询日志和慢查询日志的输出目的地7.4.1 Selecting General Query Log and Slow Query Log Output Destinations

如果启用了这些日志,MySQL 服务器提供了对通用查询日志和慢查询日志的输出目的地的灵活控制。日志条目的可能目的地是日志文件或 mysql 系统数据库中的 general_log 和 slow_log 表。可以选择文件输出、表输出或两者同时输出。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. Possible destinations for log entries are log files or the general_log and slow_log tables in the mysql system database. File output, table output, or both can be selected.

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

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

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

  • 如果在启动时指定了 log_output,其值是一个或多个以逗号分隔的单词列表,这些单词可从 TABLE(记录到表)、FILE(记录到文件)或 NONE(不记录到表或文件)中选择。如果存在 NONE,它优先于任何其他指定项。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). NONE, if present, takes precedence over any other specifiers.

general_log 系统变量控制所选日志目标的一般查询日志记录。如果在服务器启动时指定,general_log 可接受 1 或 0 的可选参数来启用或禁用日志。要为文件记录指定非默认文件名,请设置 general_log_file 变量。同样,slow_query_log 变量控制所选目标的慢查询日志记录,设置 slow_query_log_file 可为文件记录指定文件名。如果启用了任一日志,服务器会打开相应的日志文件并将启动消息写入其中。然而,除非选择了 FILE 日志目标,否则不会将查询进一步记录到文件中。The general_log system variable controls logging to the general query log for the selected log destinations. If specified at server startup, general_log takes an optional argument of 1 or 0 to enable or disable the log. To specify a file name other than the default for file logging, set the general_log_file variable. 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. 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.

示例:Examples:

运行时日志控制Log Control at Runtime

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

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

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

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

  • 要为当前会话禁用或启用通用查询日志,请将会话 sql_log_off 变量设置为 ON 或 OFF。(这假设通用查询日志本身已启用。)To disable or enable general query logging for the current session, set the session sql_log_off variable to ON or OFF. (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;
    
  • 可以通过 SQL 语句访问日志内容。这使得可以使用查询选择仅满足特定条件的日志条目。例如,要选择与特定客户端相关的日志内容(这对于识别该客户端的有问题查询非常有用),使用日志表比使用日志文件更容易。Log contents are accessible through SQL statements. This enables the use of queries that select only those log entries that satisfy specific criteria. 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 和 DROP TABLE 是对日志表的有效操作。对于 ALTER TABLE 和 DROP TABLE,日志表不能处于使用中,必须禁用,如后文所述。CREATE TABLE, ALTER TABLE, and DROP TABLE are valid operations on a log table. For ALTER TABLE and DROP TABLE, the log table cannot be in use and must be disabled, as described later.

  • 默认情况下,日志表使用 CSV 存储引擎,该引擎以逗号分隔值的格式写入数据。对于能够访问包含日志表数据的 .CSV 文件的用户,这些文件可以轻松导入其他程序,例如可以处理 CSV 输入的电子表格。By default, the log tables use the CSV storage engine that writes data in comma-separated values format. 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.

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

    日志表和“打开的文件过多”错误。 如果您选择 TABLE 作为日志目标,并且日志表使用 CSV 存储引擎,您可能会发现,在运行时反复禁用和启用通用查询日志或慢查询日志,会导致 .CSV 文件的多个打开文件描述符,从而可能导致“打开的文件过多”错误。为解决此问题,请执行 FLUSH TABLES,或者确保 open_files_limit 的值大于 table_open_cache_instances 的值。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. 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.

  • 要禁用日志记录以便可以修改(或删除)日志表,你可以使用以下策略。示例使用常规查询日志;慢查询日志的操作类似,但使用的是 slow_log 表和 slow_query_log 系统变量。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.

    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 是对日志表的有效操作。它可以用来清除日志条目。TRUNCATE TABLE is a valid operation on a log table. It can be used to expire log entries.

  • RENAME TABLE 是对日志表的有效操作。你可以使用以下策略原子性地重命名日志表(例如,为了执行日志轮换):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 是对日志表的有效操作。CHECK TABLE is a valid operation on a log table.

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

  • INSERT、DELETE 和 UPDATE 不能用于日志表。这些操作仅允许服务器自身内部使用。INSERT, DELETE, and UPDATE cannot be used on a log table. These operations are permitted only internally to the server itself.

  • FLUSH TABLES WITH READ LOCK 和 read_only 系统变量的状态对日志表没有影响。服务器始终可以写入日志表。FLUSH TABLES WITH READ LOCK and the state of the read_only system variable have no effect on log tables. 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.

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

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

  • mysqldump 转储包括用于重新创建那些表的语句,以便在重新加载转储文件后这些表不会丢失。日志表内容不会被转储。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.