记录 MySQL 个交互式查询

Logging MySQL interactive queries

我有几个人直接在某些系统中进行一些数据库查询。

我想将 interactively/by 手动完成的所有查询记录到安全的系统日志服务器,否则,使用 mysql 二进制客户端完成的所有查询。

我正在使用 Debian Jessie 和 Oracle MySQL 5.6.31

我怎样才能做到这一点?

从 MySQL 5.7 开始,mysql 命令接受 --syslog 选项。所以我将一个测试系统升级到 MySQL 5.7,它可以运行。 我将创建一个从 mysqlmysql --syslog

的别名

来自MySQL 5.7 Reference Manual - mysql Logging

syslog Logging Characteristics

If the --syslog option is given, mysql writes interactive statements to the system logging facility. Message logging has the following characteristics.

Logging occurs at the “information” level. This corresponds to the LOG_INFO priority for syslog on Unix/Linux syslog capability and to EVENTLOG_INFORMATION_TYPE for the Windows Event Log. Consult your system documentation for configuration of your logging capability.

Message size is limited to 1024 bytes.

Messages consist of the identifier MysqlClient followed by these values:

SYSTEM_USER

The system user name (login name) or -- if the user is unknown.

MYSQL_USER

The MySQL user name (specified with the --user option) or -- if the user is unknown.

CONNECTION_ID:

The client connection identifier. This is the same as the CONNECTION_ID() function value within the session.

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 --syslog. This output is formatted for readability; each logged message actually takes a single line.

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;'

正如您从 MySQL 5.7 中正确发现的那样,现在可以选择登录 syslog

不是像上面那样为命令创建别名,而是可以将 syslog 添加到 /etc/mysql/my.cnf 或 [=15 中的文件的 [mysql] 部分=]

[mysql]
syslog

这也允许方便地集成到 logwatch