MySQL 显示查询历史的 SQL 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7818031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:23:39  来源:igfitidea点击:

SQL command to display history of queries

mysql

提问by surya.in

I would like to display my executed sql command history in my MYSQL Query Browser. What is the sql statement for displaying history?

我想在我的 MYSQL 查询浏览器中显示我执行的 sql 命令历史记录。显示历史的sql语句是什么?

回答by Maysam Torabi

try

尝试

 cat ~/.mysql_history

this will show you all mysql commands ran on the system

这将显示系统上运行的所有 mysql 命令

回答by GTodorov

For MySQL > 5.1.11 or MariaDB

对于 MySQL > 5.1.11 或 MariaDB

  1. SET GLOBAL log_output = 'TABLE';
  2. SET GLOBAL general_log = 'ON';
  3. Take a look at the table mysql.general_log
  1. SET GLOBAL log_output = 'TABLE';
  2. SET GLOBAL general_log = 'ON';
  3. 看看表 mysql.general_log

If you want to output to a log file:

如果要输出到日志文件:

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

As mentioned by jeffmHyman in comments, these settings will be forgetting before next session unless you edit the configuration files (e.g. edit /etc/mysql/my.cnf, then restart to apply changes).

正如 jeffmHyman 在评论中提到的,除非您编辑配置文件(例如,编辑/etc/mysql/my.cnf,然后重新启动以应用更改),否则这些设置将在下一次会话之前忘记。

Now, if you'd like you can tail -f /var/log/mysql/mysql.log

现在,如果你愿意,你可以 tail -f /var/log/mysql/mysql.log

More info here: Server System Variables

更多信息:服务器系统变量

回答by Vamsidhar Muggulla

(Linux) Open your Terminal ctrl+alt+trun the command

(Linux) 打开你的终端ctrl+alt+t运行命令

 cat ~/.mysql_history

you will get all the previous mysql query history enjoy :)

您将获得所有以前的 mysql 查询历史记录,享受 :)

回答by DimiDak

You 'll find it there

你会在那里找到它

~/.mysql_history

You 'll make it readable (without the escapes) like this:

您将使其可读(没有转义),如下所示:

sed "s/\0/ /g" < .mysql_history

回答by user4493696

Look at ~/.myslgui/query-browser/history.xmlhere you can find the last queries made with mysql_query_browser(some days old)

看看~/.myslgui/query-browser/history.xmlhere你可以找到最后的查询mysql_query_browser(几天前)

回答by GoYun.Info

You can see the history from ~/.mysql_history. However the content of the file is encoded by wctomb. To view the content:

您可以从 ~/.mysql_history 中查看历史记录。但是文件的内容是由 wctomb 编码的。查看内容:

shell> cat ~/.mysql_history | python2.7 -c "import sys; print(''.join([l.decode('unicode-escape') for l in sys.stdin]))"

shell> cat ~/.mysql_history | python2.7 -c "import sys; print(''.join([l.decode('unicode-escape') for l in sys.stdin]))"

Source:Check MySQL query history from command line

来源:从命令行检查 MySQL 查询历史记录

回答by Kieren Johnstone

You can look at the query cache: http://www.databasejournal.com/features/mysql/article.php/3110171/MySQLs-Query-Cache.htmbut it might not give you access to the actual queries and will be very hit-and-miss if it did work (subtle pun intended)

您可以查看查询缓存:http: //www.databasejournal.com/features/mysql/article.php/3110171/MySQLs-Query-Cache.htm但它可能无法让您访问实际查询,并且会非常如果它确实有效,则命中注定(微妙的双关语意)

But MySQL Query Browser very likely maintains its own list of queries that it runs, outside of the MySQL engine. You would have to do the same in your app.

但是 MySQL 查询浏览器很可能会在 MySQL 引擎之外维护自己的查询列表。你必须在你的应用程序中做同样的事情。

Edit: see dan m's comment leading to this: How to show the last queries executed on MySQL?looks sound.

编辑:请参阅 dan m 的评论导致此:如何显示在 MySQL 上执行的最后查询?看起来很健全。