如何显示在 MySQL 上执行的最后查询?

2021-11-20 00:00:00 logging mysql

是否有任何查询/方法可以显示在所有服务器上执行的最后查询?

Is there any query/way to show the last queries executed on ALL servers?

推荐答案

对于那些拥有 MySQL >= 5.1.12 的人,你可以在运行时全局控制这个选项:

For those blessed with MySQL >= 5.1.12, you can control this option globally at runtime:

  1. 执行SET GLOBAL log_output = 'TABLE';
  2. 执行SET GLOBAL general_log = 'ON';
  3. 看一下表mysql.general_log

如果您更喜欢输出到文件而不是表格:

If you prefer to output to a file instead of a table:

  1. SET GLOBAL log_output = "FILE"; 默认.
  2. SET GLOBAL general_log_file = "/path/to/your/logfile.log";
  3. SET GLOBAL general_log = 'ON';

我更喜欢这种方法来编辑 .cnf 文件,因为:

I prefer this method to editing .cnf files because:

  1. 您没有编辑 my.cnf 文件并可能永久开启日志记录
  2. 您不是在文件系统中四处寻找查询日志——或者更糟的是,因为需要完美的目的地而分心./var/log/var/data/log /opt/home/mysql_savior/var
  3. 您不必重新启动服务器并中断与它的任何当前连接.
  4. 重新启动服务器会使您从开始的地方离开(默认情况下日志仍处于关闭状态)
  1. you're not editing the my.cnf file and potentially permanently turning on logging
  2. you're not fishing around the filesystem looking for the query log - or even worse, distracted by the need for the perfect destination. /var/log /var/data/log /opt /home/mysql_savior/var
  3. You don't have to restart the server and interrupt any current connections to it.
  4. restarting the server leaves you where you started (log is by default still off)

有关更多信息,请参阅MySQL 5.1 参考手册 - 服务器系统变量 - general_log

相关文章