问题
I have several people doing some database queries directly into some systems.
I would like to log to a secure syslog server all the queries done interactively/by hand, or failing that, all the queries done by using the mysql
binary client.
I am using Debian Jessie, and Oracle MySQL 5.6.31
How could I achieve that?
回答1:
As you correctly spotted from MySQL 5.7 there is now the option to log to syslog
.
Instead of creating an alias to the command as you have above, you could instead add syslog
to a [mysql]
section of either your /etc/mysql/my.cnf
or a file in /etc/mysql/conf.d/
As in
[mysql]
syslog
This also allows for handy integration into logwatch
.
回答2:
Starting in MySQL 5.7, the mysql
command accepts the --syslog option. So I upgraded a test system to MySQL 5.7, and it works.
I will make an alias from mysql
to mysql --syslog
From 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;'
来源:https://stackoverflow.com/questions/37834849/logging-mysql-interactive-queries