使用 JDBC 启用 MySQL 通用查询日志 [英] Enabling MySQL general query log with JDBC

查看:36
本文介绍了使用 JDBC 启用 MySQL 通用查询日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过 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'

这篇关于使用 JDBC 启用 MySQL 通用查询日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆