标签归档: log

怎么打开/查看MySQL的SQL记录?

我们在开发的时候经常经常遇到在程序中增加调试语句很麻烦的情况,这时候难免会想如果Mysql能看到我们执行了什么SQL语句就好了。

实际上MySQL是有这个功能的,在MySQL 5.1的时候我们就可以通过mysqld的-l参数来启动mysql来记录查询日志。

但是现在-l参数自5.1.12之后已经不推荐使用了,改为–general_log

As of MySQL 5.1.12, as an alternative to –log or -l, use –general_log[={0|1}] to specify the initial general query log state. In this case, the default general query log file name is used. With no argument or an argument of 1, –general_log enables the log. With an argument of 0, this option disables the log.

 

–general_log 打开后日志默认会输出到你的data目录下,默认文件名是 hostname.log。hostname是你的机器名,在windows上貌似是localhost或者其他什么奇怪的名字吧。

 

如果服务器已经启动或者不是直接用mysqld启动服务器的怎么办呢?(运行时开关General Log)

执行下面两个SQL就可以开关日志

打开日志

SET GLOBAL general_log = 'ON';

关闭日志

SET GLOBAL general_log = 'OFF';

 

配置文件可以设置General Log的开关吗?

在配置文件中[mysqld]段增加下面一行

general_log = 1

如果需要指定日志文件路径

general_log_file = 文件路径

Read: 6881