如果启用了这些日志,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_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。Iflog_outputis not specified at startup, the default logging destination isFILE.如果在启动时指定了
log_output,其值是一个或多个以逗号分隔的单词列表,这些单词可从TABLE(记录到表)、FILE(记录到文件)或NONE(不记录到表或文件)中选择。如果存在NONE,它优先于任何其他指定项。Iflog_outputis specified at startup, its value is a list one or more comma-separated words chosen fromTABLE(log to tables),FILE(log to files), orNONE(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_output=TABLE,FILE选择两个日志目标,并使用--general_log启用常规查询日志。To write general query log entries to the log table and the log file, use--log_output=TABLE,FILEto select both log destinations and--general_logto enable the general query log.要仅将常规和慢查询日志条目写入日志表,请使用
--log_output=TABLE选择表作为日志目标,并使用--general_log和--slow_query_log启用两个日志。To write general and slow query log entries only to the log tables, use--log_output=TABLEto select tables as the log destination and--general_logand--slow_query_logto enable both logs.要将慢查询日志条目仅写入日志文件,请使用
--log_output=FILE选择文件作为日志目标,并使用--slow_query_log启用慢查询日志。在这种情况下,因为默认的日志目标是FILE,所以可以省略log_output设置。To write slow query log entries only to the log file, use--log_output=FILEto select files as the log destination and--slow_query_logto enable the slow query log. In this case, because the default log destination isFILE, you could omit thelog_outputsetting.
与日志表和文件相关的系统变量使得可以在运行时控制日志记录:The system variables associated with log tables and files enable runtime control over logging:
log_output变量表示当前的日志记录目标。它可以在运行时修改以更改目标。Thelog_outputvariable indicates the current logging destination. It can be modified at runtime to change the destination.general_log和slow_query_log变量表示是否启用通用查询日志和慢查询日志(ON)或禁用它们(OFF)。您可以在运行时设置这些变量以控制日志是否启用。Thegeneral_logandslow_query_logvariables 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变量表示通用查询日志和慢查询日志文件的名称。您可以在服务器启动时或运行时设置这些变量以更改日志文件的名称。Thegeneral_log_fileandslow_query_log_filevariables 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 sessionsql_log_offvariable toONorOFF. (This assumes that the general query log itself is enabled.)
使用表格进行日志输出具有以下好处: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, andDROP TABLEare valid operations on a log table. ForALTER TABLEandDROP TABLE, the log table cannot be in use and must be disabled, as described later.默认情况下,日志表使用
CSV存储引擎,该引擎以逗号分隔值的格式写入数据。对于能够访问包含日志表数据的.CSV文件的用户,这些文件可以轻松导入其他程序,例如可以处理 CSV 输入的电子表格。By default, the log tables use theCSVstorage engine that writes data in comma-separated values format. For users who have access to the.CSVfiles 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 theMyISAMstorage engine. You cannot useALTER TABLEto alter a log table that is in use. The log must be disabled first. No engines other thanCSVorMyISAMare 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 selectTABLEas a log destination and the log tables use theCSVstorage 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.CSVfile, possibly resulting in a “Too many open files” error. To work around this issue, executeFLUSH TABLESor ensure that the value ofopen_files_limitis greater than the value oftable_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 theslow_logtable andslow_query_logsystem 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 TABLEis a valid operation on a log table. It can be used to expire log entries.RENAME TABLE是对日志表的有效操作。你可以使用以下策略原子性地重命名日志表(例如,为了执行日志轮换):RENAME TABLEis 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 TABLEis a valid operation on a log table.LOCK TABLES不能用于日志表。LOCK TABLEScannot be used on a log table.INSERT、DELETE和UPDATE不能用于日志表。这些操作仅允许服务器自身内部使用。INSERT,DELETE, andUPDATEcannot 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 LOCKand the state of theread_onlysystem 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, useFLUSH TABLESorFLUSH 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.