使用 JDBC 启用 MySQL 通用查询日志

2021-12-19 00:00:00 logging mysql jdbc

有没有办法通过 JDBC 启用 MySQL 常规查询日志记录?我通过搜索发现的最接近的是能够通过 JDBC (http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-configuration-properties.html) 记录慢查询.也许我应该这样做并将慢查询阈值设置为 0 毫秒?

Is there a way to enable MySQL general query logging through JDBC? The closest thing I have found through my search is the ability to log slow queries through JDBC (http://dev.mysql.com/doc/refman/5.5/en/connector-j-reference-configuration-properties.html). Maybe I should do that and set the slow query threshold to 0 ms?

我想以人类可读的格式记录通过 MySQL 的所有查询,并想指定应该写入日志文件的位置.我知道我的性能会受到影响,但我的应用程序只有一个用户,而且非常简单,如果性能受到明显影响,我会感到惊讶.反正我想试试看.

I would like to log all queries through MySQL in a human-readable format and would like to specify the location where the log file should be written. I know I will take a performance hit, but my application only has one user and is simple enough that I would be surprised if the performance hit was noticeable. I would like to try it out anyway to see.

我相信我的另一个选择是打开二进制日志记录并使用 mysqlbinlog 将二进制日志转换为人类可读的格式,但听起来通用查询日志将提供一种更简单的方法来获取我想要的内容.

I believe another option I have is to turn on binary logging and use mysqlbinlog to convert the binary logs to a human-readable format, but it sounds like the general query log would provide a simpler means of getting what I want.

推荐答案

我最终找到了解决方法.我通过在运行时使用以下 SQL 查询修改 MySQL 全局系统变量来通过 Java 启用 MySQL 常规查询日志记录.

I ended up finding a workaround. I enable MySQL general query logging through Java by modifying MySQL global system variables at runtime with the following SQL queries.

SET GLOBAL log_output="FILE"
SET GLOBAL general_log_file="Path/File"
SET GLOBAL general_log='ON'

我建议在 general_log_file 路径中使用正斜杠.即使在 Windows 环境中,我也无法使用反斜杠.

I recommend using forward slashes in the general_log_file path. I could not get backslashes to work, even in a Windows environment.

我在运行时使用以下 SQL 查询禁用了一般查询日志记录.

I disable general query logging at runtime with the following SQL query.

SET GLOBAL general_log='OFF'

相关文章