8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

7.4.3 常规查询日志7.4.3 The General Query Log

常规查询日志是记录 mysqld 所做操作的一般记录。服务器在客户端连接或断开连接时将信息写入此日志,并记录从客户端收到的每条 SQL 语句。当你怀疑客户端有错误并想准确知道客户端发送给 mysqld 的内容时,常规查询日志非常有用。The general query log is a general record of what mysqld is doing. The server writes information to this log when clients connect or disconnect, and it logs each SQL statement received from clients. The general query log can be very useful when you suspect an error in a client and want to know exactly what the client sent to mysqld.

每当客户端连接时显示的每一行还包括 using connection_type 来指示用于建立连接的协议。connection_type 可以是 TCP/IP(未使用 SSL 建立的 TCP/IP 连接)、SSL/TLS(使用 SSL 建立的 TCP/IP 连接)、Socket(Unix 套接字文件连接)、Named Pipe(Windows 命名管道连接)或 Shared Memory(Windows 共享内存连接)。Each line that shows when a client connects also includes using connection_type to indicate the protocol used to establish the connection. connection_type is one of TCP/IP (TCP/IP connection established without SSL), SSL/TLS (TCP/IP connection established with SSL), Socket (Unix socket file connection), Named Pipe (Windows named pipe connection), or Shared Memory (Windows shared memory connection).

mysqld 按接收顺序将语句写入查询日志,这可能与它们执行的顺序不同。这种记录顺序与二进制日志的顺序形成对比,二进制日志在语句执行后但在释放任何锁之前写入语句。此外,查询日志可能包含仅用于选择数据的语句,而此类语句从不写入二进制日志。mysqld writes statements to the query log in the order that it receives them, which might differ from the order in which they are executed. This logging order is in contrast with that of the binary log, for which statements are written after they are executed but before any locks are released. In addition, the query log may contain statements that only select data while such statements are never written to the binary log.

在复制源服务器上使用基于语句的二进制日志记录时,副本接收到的语句会写入每个副本的查询日志。如果客户端使用 mysqlbinlog 工具读取事件并将其传递给服务器,这些语句会写入源服务器的查询日志。When using statement-based binary logging on a replication source server, statements received by its replicas are written to the query log of each replica. Statements are written to the query log of the source if a client reads events with the mysqlbinlog utility and passes them to the server.

然而,在使用基于行的二进制日志时,更新是以行更改的形式发送的,而不是 SQL 语句,因此当 binlog_format 设置为 ROW 时,这些语句永远不会被写入查询日志。当该变量设置为 MIXED 时,是否将特定的更新写入查询日志取决于使用的语句。更多信息请参见 第 19.2.1.1 节,“基于语句和基于行复制的优缺点”。However, when using row-based binary logging, updates are sent as row changes rather than SQL statements, and thus these statements are never written to the query log when binlog_format is ROW. A given update also might not be written to the query log when this variable is set to MIXED, depending on the statement used. See Section 19.2.1.1, “Advantages and Disadvantages of Statement-Based and Row-Based Replication”, for more information.

默认情况下,一般查询日志是禁用的。要明确指定初始的一般查询日志状态,请使用 --general_log[={0|1}]。如果没有参数或参数为 1,--general_log 会启用日志。参数为 0 时,此选项会禁用日志。要指定日志文件名,请使用 --general_log_file=file_name。要指定日志输出位置,请使用 log_output 系统变量(如 第 7.4.1 节,“选择一般查询日志和慢查询日志输出位置” 中所述)。By default, the general query log is disabled. To specify the initial general query log state explicitly, use --general_log[={0|1}]. With no argument or an argument of 1, --general_log enables the log. With an argument of 0, this option disables the log. To specify a log file name, use --general_log_file=file_name. To specify the log destination, use the log_output system variable (as described in Section 7.4.1, “Selecting General Query Log and Slow Query Log Output Destinations”).

Note

如果您指定 TABLE 日志目标,请参阅 日志表和 “打开的文件过多”” 错误。If you specify the TABLE log destination, see Log Tables and “Too many open files” Errors.

如果您没有为通用查询日志文件指定名称,默认名称为 host_name.log。服务器会在数据目录中创建该文件,除非提供绝对路径名以指定不同的目录。If you specify no name for the general query log file, the default name is host_name.log. The server creates the file in the data directory unless an absolute path name is given to specify a different directory.

要在运行时禁用或启用通用查询日志,或更改日志文件名,请使用全局 general_log 和 general_log_file 系统变量。将 general_log 设置为 0(或 OFF)以禁用日志,或设置为 1(或 ON)以启用日志。设置 general_log_file 来指定日志文件的名称。如果日志文件已打开,则会先关闭它,然后打开新文件。To disable or enable the general query log or change the log file name at runtime, use the global general_log and general_log_file system variables. Set general_log to 0 (or OFF) to disable the log or to 1 (or ON) to enable it. Set general_log_file to specify the name of the log file. If a log file already is open, it is closed and the new file is opened.

当启用通用查询日志时,服务器会将输出写入由 log_output 系统变量指定的任何目标。如果启用日志,服务器会打开日志文件并将启动消息写入其中。然而,除非选择了 FILE 日志目标,否则不会将查询进一步记录到文件中。如果目标是 NONE,即使启用了通用日志,服务器也不会写入任何查询。如果日志目标值不包含 FILE,设置日志文件名对日志记录没有影响。When the general query log is enabled, the server writes output to any destinations specified by the log_output system variable. If you enable the log, the server opens the 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. If the destination is NONE, the server writes no queries even if the general log is enabled. Setting the log file name has no effect on logging if the log destination value does not contain FILE.

服务器重启和日志刷新不会导致生成新的通用查询日志文件(尽管刷新会关闭并重新打开它)。要重命名文件并创建一个新的文件,请使用以下命令:Server restarts and log flushing do not cause a new general query log file to be generated (although flushing closes and reopens it). To rename the file and create a new one, use the following commands:

$> mv host_name.log host_name-old.log
$> mysqladmin flush-logs general
$> mv host_name-old.log backup-directory

在 Windows 上,使用 rename 而不是 mv。On Windows, use rename rather than mv.

您也可以通过禁用日志在运行时重命名通用查询日志文件:You can also rename the general query log file at runtime by disabling the log:

SET GLOBAL general_log = 'OFF';

在禁用日志的情况下,从外部重命名日志文件(例如,通过命令行)。然后再次启用日志:With the log disabled, rename the log file externally (for example, from the command line). Then enable the log again:

SET GLOBAL general_log = 'ON';

这种方法适用于任何平台,并且不需要重启服务器。This method works on any platform and does not require a server restart.

要为当前会话禁用或启用通用查询日志,请将会话 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-raw 选项启动服务器,可以抑制通用查询日志的密码重写。此选项可能在诊断目的下有用,可查看服务器接收到的语句的精确文本,但出于安全原因,不建议在生产环境中使用。另请参阅 第 8.1.2.3 节,“密码与日志”。Passwords in statements written to the general query log are rewritten by the server not to occur literally in plain text. Password rewriting can be suppressed for the general query log by starting the server with the --log-raw option. This option may be useful for diagnostic purposes, to see the exact text of statements as received by the server, but for security reasons is not recommended for production use. See also Section 8.1.2.3, “Passwords and Logging”.

密码重写的一个含义是,无法解析的语句(例如,由于语法错误)不会被写入通用查询日志,因为无法确定这些语句是否不包含密码。需要记录所有语句(包括有错误的语句)的用例应使用--log-raw 选项,但要注意,这也会绕过密码重写。An implication of password rewriting is that statements that cannot be parsed (due, for example, to syntax errors) are not written to the general query log because they cannot be known to be password free. Use cases that require logging of all statements including those with errors should use the --log-raw option, bearing in mind that this also bypasses password rewriting.

只有在预期使用明文密码时才会发生密码重写。对于语法上期望密码哈希值的语句,不会进行重写。如果错误地为此类语法提供了明文密码,则该密码将按提供的方式记录,不会重写。Password rewriting occurs only when plain text passwords are expected. For statements with syntax that expect a password hash value, no rewriting occurs. If a plain text password is supplied erroneously for such syntax, the password is logged as given, without rewriting.

log_timestamps 系统变量控制写入通用查询日志文件(以及慢查询日志文件和错误日志)的消息时间戳的时区。它不会影响写入日志表的通用查询日志和慢查询日志消息的时区,但从这些表中检索的行可以使用 CONVERT_TZ() 转换为任何所需的时区,或者通过设置会话 time_zone 系统变量进行转换。The log_timestamps system variable controls the time zone of timestamps in messages written to the general query log file (as well as to the slow query log file and the error log). It does not affect the time zone of general query log and slow query log messages written to log tables, but rows retrieved from those tables can be converted from the local system time zone to any desired time zone with CONVERT_TZ() or by setting the session time_zone system variable.