You must understand the MySQL error log to be the leader in solving bugs!

Preface

When we use MySQL, we often encounter various errors. The MySQL error log can help us record these errors. It is very important for us to solve the problem by looking at the error date. Let's take a look at how to open and use the error log.

Error Log

The MySQL error log contains records of when mysqld was started and shut down. It also contains diagnostic messages such as errors, warnings, and cautions, which occur during server startup and shutdown, and during server operation. For example, if mysqld notices that a table needs to be automatically checked or repaired, it will write a message to the error log.

On some operating systems, if mysqld exits abnormally, the error log will contain a stack trace. Trace can be used to determine where mysqld exited.

If you start with mysqld, mysqld_safe, some exception messages will also be written to the error log. For example, when mysqld_safe notices an abnormal mysqld exit, it restarts mysqld and writes a mysqld restart message to the error log.

For Windows and Unix systems, the server has slightly different interpretations of the options for determining where to write error messages. So the following two systems are introduced.


Record error logs on Unix and Unix-like systems

On Unix and Unix-like systems, mysqld uses the -log-error option to determine whether mysqld writes the error log to the console or a file, and if it is a file, write the file name:

如果--log-error未给出,则 mysqld将错误日志写入控制台。

如果--log-error给出时未命名文件,则mysqld将错误日志写入host_name.err 数据目录中命名的文件 。

如果--log-error给定一个文件名,则mysqld将错误日志写入该文件(如果该文件没有后缀,则添加.err后缀)。除非给出绝对路径名以指定其他位置,否则文件位置在数据目录下。

如果--log-error在选项文件中指定[mysqld], [server]或 [mysqld_safe]部分,那么在使用mysqld_safe启动服务器的系统上,会找到mysqld_safe部分的该选项,并将其传递到mysqld。

note:

Yum or APT package installation usually configure the error log file location under /var/log, and use an option like log-error=/var/log/mysqld.log in the server configuration file. Removing the path name from the options will cause the err file in the data directory to be used.

If the server writes the error log to the console, it will set the log_error system variable to stderr.

Open the instance:
configure in the configuration file

#编辑Mysql的配置文件
vi /etc/my.cnf

#在mysqld部分添加log-error变量,你可以指定绝对路径,也可以不指定,不指定就默认在mysql的数据目录下
[mysqld]
log-error=/usr/err.log

To check whether the error log is enabled, use the following command:

#如果开启了,会显示错误日志所在的位置
mysql> show variables like '%log_error%';

Record error logs on Windows

On Windows, mysqld uses the –log-error, –pid-file, and –console options to determine whether mysqld writes the error log to the console or a file. If writing to a file, write the file name:

If -console is specified, mysqld will write the error log to the console. (If both –console and –log-error are given, then –console takes precedence over –log-error, and the following items about –log-error do not apply. Before MySQL 5.7, this is the opposite: –log-error takes precedence over –Console.

If --log-error is not given, or the file is not named when given, mysqld will write the error log to a folder named host_name.err in the data directory, unless the --pid-file option is specified. In this case, the file name is the base name of the PID file with the .err suffix in the data directory.

If you specify –log-error and give a file name, mysqld will write the error log to the file (if the file has no suffix, add the .err suffix). Unless an absolute path name is specified to specify a different location, the file location is under the data directory.

If the server writes the error log to the console, it will set the log_error system variable to stderr. Otherwise, the server writes the error log to the file and sets log_error to the file name.

Error log filtering

The log_error_verbosity system variable can control the level of detail the server writes to the error log. The allowed values ​​are 1 (only allow errors), 2 (errors and warnings), 3 (errors, warnings and comments), and the default value is 3. If the value is greater than 2, the server will log aborted connections and connection attempts that deny access.

Error log output format

The ID contained in the error log message is the ID of the thread responsible for writing the message in mysqld. This indicates which part of the server generated the message at the time, and is consistent with the regular query log and slow query log messages (including the connection thread ID).

The log_timestamps system variable controls the time zone of the timestamp in the messages written to the error log (and the general query log and slow query log files).

The allowed values ​​for log_timestamps are UTC (default) and SYSTEM (local system time zone). The timestamp is written in the ISO 8601 / RFC 3339 format: Add the end value Z representing Zulu time (UTC) or the offset of the local system time zone adjustment relative to UTC. For example: YYYY-MM-DDThh:mm:ss.uuuuuuZ±hh:mm

Example:

2020-08-07T15:02:00.832521Z            (UTC)
2020-08-07T10:02:00.832521-05:00       (SYSTEM)

A standard error log is as follows:

2020-12-22T09:56:57.551273Z 30 [Note] Access denied for user 'root'@'localhost' (using password: YES)

Parameter Description:

2020-12-22T09:56:57.551273Z :时间

30 :线程ID

[Note]:日志类型,note为记录,warning 为警告,error为错误

Access denied for user 'root'@'localhost' (using password: YES):错误信息

Guess you like

Origin blog.csdn.net/qq_36551991/article/details/111599046