The mysql client can do these types of logging for statements executed interactively:mysql客户端可以对交互执行的语句进行以下类型的日志记录:
On Unix, mysql writes the statements to a history file. 在Unix上,mysql将语句写入历史文件。By default, this file is named 默认情况下,此文件在主目录中名为.mysql_history
in your home directory. .mysql_history
。To specify a different file, set the value of the 要指定其他文件,请设置MYSQL_HISTFILE
environment variable.MY_RESHISTFILE
环境变量的值。
On all platforms, if the 在所有平台上,如果给定--syslog
option is given, mysql writes the statements to the system logging facility. --syslog
选项,mysql会将语句写入系统日志记录工具。On Unix, this is 在Unix上,这是syslog
; on Windows, it is the Windows Event Log. The destination where logged messages appear is system dependent. syslog
;在Windows上,它是Windows事件日志。显示记录消息的目标取决于系统。On Linux, the destination is often the 在Linux上,目标通常是/var/log/messages
file./var/log/messages
文件。
The following discussion describes characteristics that apply to all logging types and provides information specific to each logging type.以下讨论描述了适用于所有日志记录类型的特征,并提供了每种日志记录类型特有的信息。
For each enabled logging destination, statement logging occurs as follows:对于每个启用的日志记录目标,语句日志记录按如下方式进行:
Statements are logged only when executed interactively. Statements are noninteractive, for example, when read from a file or a pipe. 语句仅在交互执行时记录。例如,从文件或管道读取时,语句是非交互式的。It is also possible to suppress statement logging by using the 还可以通过使用--batch
or --execute
option.--batch
或--execute
选项来抑制语句日志记录。
Statements are ignored and not logged if they match any pattern in the “ignore” list. This list is described later.如果语句与“忽略”列表中的任何模式匹配,则会被忽略且不会记录。稍后将描述此列表。
mysql logs each nonignored, nonempty statement line individually.mysql单独记录每个不可忽略的、非空的语句行。
If a nonignored statement spans multiple lines (not including the terminating delimiter), mysql concatenates the lines to form the complete statement, maps newlines to spaces, and logs the result, plus a delimiter.如果一个未被忽略的语句跨越多行(不包括终止分隔符),mysql会将这些行连接起来形成完整的语句,将换行符映射到空格,并记录结果,再加上一个分隔符。
Consequently, an input statement that spans multiple lines can be logged twice. Consider this input:因此,跨越多行的输入语句可以被记录两次。考虑以下输入:
mysql>SELECT
->'Today is'
->,
->CURDATE()
->;
In this case, mysql logs the “SELECT”, “'Today is'”, “,”, “CURDATE()”, and “;” lines as it reads them. It also logs the complete statement, after mapping 在这种情况下,mysql在读取“SELECT”、“'Today is'”、“,”、“CURDATE()”和“;”行时会记录这些行。在将SELECT\n'Today is'\n,\nCURDATE()
to SELECT 'Today is' , CURDATE()
, plus a delimiter. SELECT\n'Today is'\n,\nCURDATE()
映射到SELECT 'Today is' , CURDATE()
以及一个分隔符之后,它还会记录完整的语句。Thus, these lines appear in logged output:因此,这些行出现在日志输出中:
SELECT 'Today is' , CURDATE() ; SELECT 'Today is' , CURDATE();
mysql ignores for logging purposes statements that match any pattern in the “ignore” list. mysql忽略与“ignore”列表中的任何模式匹配的语句用于日志记录。By default, the pattern list is 默认情况下,模式列表为"*IDENTIFIED*:*PASSWORD*"
, to ignore statements that refer to passwords. "*IDENTIFIED*:*PASSWORD*"
,忽略涉及密码的语句。Pattern matching is not case-sensitive. Within patterns, two characters are special:模式匹配不区分大小写。在模式中,有两个字符是特殊的:
?
matches any single character.?
匹配任何单个字符。
*
matches any sequence of zero or more characters.*
匹配任何零个或多个字符的序列。
To specify additional patterns, use the 要指定其他模式,请使用--histignore
option or set the MYSQL_HISTIGNORE
environment variable. --histignore
选项或设置MY_RESHISTIGNORE环境变量。(If both are specified, the option value takes precedence.) The value should be a list of one or more colon-separated patterns, which are appended to the default pattern list.(如果同时指定了这两个选项,则选项值优先。)该值应该是一个或多个冒号分隔的模式列表,这些模式将附加到默认模式列表中。
Patterns specified on the command line might need to be quoted or escaped to prevent your command interpreter from treating them specially. 命令行上指定的模式可能需要引用或转义,以防止命令解释器对其进行特殊处理。For example, to suppress logging for 例如,除了引用密码的语句外,要抑制UPDATE
and DELETE
statements in addition to statements that refer to passwords, invoke mysql like this:UPDATE
和DELETE
语句的日志记录,请按如下方式调用mysql:
mysql --histignore="*UPDATE*:*DELETE*"
The .mysql_history
file should be protected with a restrictive access mode because sensitive information might be written to it, such as the text of SQL statements that contain passwords. .mysql_history
文件应使用限制访问模式进行保护,因为敏感信息可能会写入其中,例如包含密码的SQL语句文本。See Section 6.1.2.1, “End-User Guidelines for Password Security”. 请参阅第6.1.2.1节,“密码安全最终用户指南”。Statements in the file are accessible from the mysql client when the up-arrow key is used to recall the history. 当使用向上箭头键调用历史记录时,可以从mysql客户端访问文件中的语句。See Disabling Interactive History.请参阅禁用交互历史记录。
If you do not want to maintain a history file, first remove 如果您不想维护历史文件,请先删除.mysql_history
if it exists. Then use either of the following techniques to prevent it from being created again:.mysql_history
(如果存在)。然后使用以下任何一种技术来防止再次创建它:
Set the 将MYSQL_HISTFILE
environment variable to /dev/null
. To cause this setting to take effect each time you log in, put it in one of your shell's startup files.MYSQL_HISTFILE
环境变量设置为/dev/null
。要使此设置在每次登录时生效,请将其放在shell的启动文件中。
Create 创建.mysql_history
as a symbolic link to /dev/null
; this need be done only once:.mysql_history
作为/dev/null
的符号链接;这只需要做一次:
ln -s /dev/null $HOME/.mysql_history
If the 如果给出了--syslog
option is given, mysql writes interactive statements to the system logging facility. --syslog
选项,mysql会将交互式语句写入系统日志记录工具。Message logging has the following characteristics.消息日志记录具有以下特征。
Logging occurs at the “information” level. This corresponds to the 日志记录发生在“信息”级别。这对应于Unix/Linux系统日志功能的系统日志的LOG_INFO
priority for syslog
on Unix/Linux syslog
capability and to EVENTLOG_INFORMATION_TYPE
for the Windows Event Log. LOG_INFO
优先级,以及Windows事件日志的EVENTLOG_INFORMATION_TYPE
。Consult your system documentation for configuration of your logging capability.有关日志记录功能的配置,请参阅系统文档。
Message size is limited to 1024 bytes.消息大小限制为1024字节。
Messages consist of the identifier 消息由标识符MysqlClient
followed by these values:MysqlClient
和以下值组成:
SYSTEM_USER
The operating system user name (login name) or 操作系统用户名(登录名)或--
if the user is unknown.--
如果用户未知。
MYSQL_USER
The MySQL user name (specified with the MySQL用户名(用--user
option) or --
if the user is unknown.--user
选项指定)或--
(如果用户未知)。
CONNECTION_ID
:
The client connection identifier. This is the same as the 客户端连接标识符。这与会话中的CONNECTION_ID()
function value within the session.CONNECTION_ID()
函数值相同。
DB_SERVER
The server host or 服务器主机或--
if the host is unknown.--
如果主机未知。
DB
The default database or 默认数据库或--
if no database has been selected.--
如果未选择数据库。
QUERY
The text of the logged statement.记录的语句文本。
Here is a sample of output generated on Linux by using 以下是在Linux上使用--syslog
. This output is formatted for readability; each logged message actually takes a single line.--syslog
生成的输出示例。此输出的格式是为了可读性;每条记录的消息实际上只需要一行。
Mar 7 12:39:25 myhost MysqlClient[20824]: SYSTEM_USER:'oscar', MYSQL_USER:'my_oscar', CONNECTION_ID:23, DB_SERVER:'127.0.0.1', DB:'--', QUERY:'USE test;' Mar 7 12:39:28 myhost MysqlClient[20824]: SYSTEM_USER:'oscar', MYSQL_USER:'my_oscar', CONNECTION_ID:23, DB_SERVER:'127.0.0.1', DB:'test', QUERY:'SHOW TABLES;'