5.4.2.1 Error Log Configuration错误日志配置

In MySQL 8.0, error logging uses the MySQL component architecture described at Section 5.5, “MySQL Components”. 在MySQL 8.0中,错误日志记录使用第5.5节,“MySQL组件”中描述的MySQL组件体系结构。The error log subsystem consists of components that perform log event filtering and writing, as well as a system variable that configures which components to enable to achieve the desired logging result.错误日志子系统由执行日志事件筛选和写入的组件以及配置哪些组件以实现所需日志记录结果的系统变量组成。

This section discusses how to select components for error logging. 本节讨论如何选择用于错误日志记录的组件。For instructions specific to log filters, see Section 5.4.2.4, “Types of Error Log Filtering”. 有关日志筛选器的具体说明,请参阅第5.4.2.4节,“错误日志过滤的类型”For instructions specific to the JSON and system log sinks, see Section 5.4.2.7, “Error Logging in JSON Format”, and Section 5.4.2.8, “Error Logging to the System Log”. 有关JSON和系统日志接收器的具体说明,请参阅第5.4.2.7节,“JSON格式的错误日志记录”第5.4.2.8节,“系统日志的错误记录”For additional details about all available log components, see Section 5.5.3, “Error Log Components”.有关所有可用日志组件的更多详细信息,请参阅第5.5.3节,“错误日志组件”

Component-based error logging offers these features:基于组件的错误日志记录提供了以下功能:

The log_error_services system variable controls which log components to enable for error logging. log_error_services系统变量控制要启用哪些日志组件进行错误日志记录。The variable may contain a list with 0, 1, or many elements. In the latter case, elements may be delimited by semicolon or (as of MySQL 8.0.12) comma, optionally followed by space. 变量可以包含一个包含0、1或多个元素的列表。在后一种情况下,元素可以用分号或(从MySQL 8.0.12开始)逗号分隔,可以选择后跟空格。A given setting cannot use both semicolon and comma separators. Component order is significant because the server executes components in the order listed.给定的设置不能同时使用分号和逗号分隔符。组件顺序很重要,因为服务器按列出的顺序执行组件。

By default, log_error_services has this value:默认情况下,log_error_services具有以下值:

mysql> SELECT @@GLOBAL.log_error_services;
+----------------------------------------+
| @@GLOBAL.log_error_services            |
+----------------------------------------+
| log_filter_internal; log_sink_internal |
+----------------------------------------+

That value indicates that log events first pass through the log_filter_internal filter component, then through the log_sink_internal sink component, both of which are built in. 该值表示日志事件首先通过log_filter_internal筛选器组件,然后通过log_sink_internal sink组件,这两个组件都是内置的。A filter modifies log events seen by components named later in the log_error_services value. 筛选器会修改稍后在log_error_services值中命名的组件所看到的日志事件。A sink is a destination for log events. 接收器是日志事件的目标。Typically, a sink processes log events into log messages that have a particular format and writes these messages to its associated output, such as a file or the system log.通常,接收器将日志事件处理为具有特定格式的日志消息,并将这些消息写入其相关输出,如文件或系统日志。

Note注意

The final component in the log_error_services value cannot be a filter. This is an error because any changes it has on events would have no effect on output:log_error_services值中的最后一个组件不能是筛选器。这是一个错误,因为它对事件所做的任何更改都不会对输出产生影响:

mysql> SET GLOBAL log_error_services = 'log_filter_internal';
ERROR 1231 (42000): Variable 'log_error_services' can't be set to the value
of 'log_filter_internal'

To correct the problem, include a sink at the end of the value:要更正此问题,请在值的末尾包含一个接收器:

mysql> SET GLOBAL log_error_services = 'log_filter_internal; log_sink_internal';
Query OK, 0 rows affected (0.00 sec)

The combination of log_filter_internal and log_sink_internal implements the default error log filtering and output behavior. log_filter_internallog_sink_internal的组合实现了默认的错误日志筛选和输出行为。The action of these components is affected by other server options and system variables:这些组件的操作会受到其他服务器选项和系统变量的影响:

To change the set of log components used for error logging, load components as necessary, perform any component-specific configuration, and modify the log_error_services value. Adding or removing log components is subject to these constraints:要更改用于错误日志记录的日志组件集,请根据需要加载组件,执行任何特定于组件的配置,并修改log_error_services值。添加或删除日志组件受以下约束:

For example, to use the system log sink (log_sink_syseventlog) instead of the default sink (log_sink_internal), first load the sink component, then modify the log_error_services value:例如,要使用系统日志接收器(log_sink_syseventlog)而不是默认接收器(log_sink_internal),请首先加载接收器组件,然后修改log_error_services值:

INSTALL COMPONENT 'file://component_log_sink_syseventlog';
SET GLOBAL log_error_services = 'log_filter_internal; log_sink_syseventlog';
Note注意

The URN to use for loading a log component with INSTALL COMPONENT is the component name prefixed with file://component_. 用于加载带有INSTALL COMPONENT的日志组件的URN是前缀为file://component_For example, for the log_sink_syseventlog component, the corresponding URN is file://component_log_sink_syseventlog.例如,对于log_sink_syseventlog组件,对应的URN为file://component_log_sink_syseventlog

It is possible to configure multiple log sinks, which enables sending output to multiple destinations. 可以配置多个日志接收器,从而可以将输出发送到多个目的地。To enable the system log sink in addition to (rather than instead of) the default sink, set the log_error_services value like this:要在默认接收器之外启用系统日志接收器(而不是),请设置log_error_services值,如下所示:

SET GLOBAL log_error_services = 'log_filter_internal; log_sink_internal; log_sink_syseventlog';

To revert to using only the default sink and unload the system log sink, execute these statements:要恢复到仅使用默认接收器并卸载系统日志接收器,请执行以下语句:

SET GLOBAL log_error_services = 'log_filter_internal; log_sink_internal;
UNINSTALL COMPONENT 'file://component_log_sink_syseventlog';

To configure a log component to be enabled at each server startup, use this procedure:要将日志组件配置为在每次服务器启动时启用,请使用以下过程:

  1. If the component is loadable, load it at runtime using INSTALL COMPONENT. 如果组件是可加载的,请在运行时使用INSTALL COMPONENT加载它。Loading the component registers it in the mysql.component system table so that the server loads it automatically for subsequent startups.加载该组件会将其注册到mysql.component系统表中,以便服务器自动加载该组件以供后续启动。

  2. Set the log_error_services value at startup to include the component name. 在启动时设置log_error_services值以包括组件名称。Set the value either in the server my.cnf file, or use SET PERSIST, which sets the value for the running MySQL instance and also saves the value to be used for subsequent server restarts; see Section 13.7.6.1, “SET Syntax for Variable Assignment”. 在服务器my.cnf文件中设置值,或者使用SET PERSIST,它为正在运行的MySQL实例设置值,并保存该值以用于后续服务器重新启动;参见第13.7.6.1节,“变量赋值的SET语法”A value set in my.cnf takes effect at the next restart. my.cnf中设置的值将在下次重新启动时生效。A value set using SET PERSIST takes effect immediately, and for subsequent restarts.使用SET PERSIST设置的值将立即生效,并用于随后的重新启动。

Suppose that you want to configure, for every server startup, use of the JSON log sink (log_sink_json) in addition to the built-in log filter and sink (log_filter_internal, log_sink_internal). 假设您想为每次服务器启动配置JSON日志接收器(log_sink_json),以及内置的日志筛选器和接收器(log_filter_internallog_sink_internal)。First load the JSON sink if it is not loaded:如果JSON接收器未加载,请首先加载:

INSTALL COMPONENT 'file://component_log_sink_json';

Then set log_error_services to take effect at server startup. You can set it in my.cnf:然后将log_error_services设置为在服务器启动时生效。您可以在my.cnf中设置它:

[mysqld]
log_error_services='log_filter_internal; log_sink_internal; log_sink_json'

Or you can set it using SET PERSIST:或者可以使用SET PERSIST进行设置:

SET PERSIST log_error_services = 'log_filter_internal; log_sink_internal; log_sink_json';

The order of components named in log_error_services is significant, particularly with respect to the relative order of filters and sinks. log_error_services中命名的组件的顺序非常重要,尤其是在筛选器和接收器的相对顺序方面。Consider this log_error_services value:考虑以下log_error_services值:

log_filter_internal; log_sink_1; log_sink_2

In this case, log events pass to the built-in filter, then to the first sink, then to the second sink. Both sinks receive the filtered log events.在这种情况下,日志事件会传递到内置筛选器,然后传递到第一个接收器,然后再传递到第二个接收器。两个接收器都接收筛选的日志事件。

Compare that to this log_error_services value:将其与此log_error_services值进行比较:

log_sink_1; log_filter_internal; log_sink_2

In this case, log events pass to the first sink, then to the built-in filter, then to the second sink. The first sink receives unfiltered events. The second sink receives filtered events. 在这种情况下,日志事件会传递到第一个接收器,然后传递到内置筛选器,然后再传递到第二个接收器。第一个接收器接收未经过滤的事件。第二个接收器接收经过过滤的事件。You might configure error logging this way if you want one log that contains messages for all log events, and another log that contains messages only for a subset of log events.如果您希望一个日志包含所有日志事件的消息,而另一个日志仅包含日志事件子集的消息,则可以通过这种方式配置错误日志记录。

Note注意

If the enabled log components include a sink that provides Performance Schema support, events written to the error log are also written to the Performance Schema error_log table. 如果启用的日志组件包括提供性能架构支持的接收器,则写入错误日志的事件也会写入性能架构error_log表。This enables examining error log contents using SQL queries. Currently, the traditional-format log_sink_internal and JSON-format log_sink_json sinks support this capability. 这允许使用SQL查询检查错误日志内容。目前,传统格式的log_sink_internal和JSON格式的log_sink_json接收器都支持这一功能。See Section 27.12.21.1, “The error_log Table”.请参阅第27.12.21.1节,“错误日志表”