150

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

Bridge
  • 29,818
  • 9
  • 60
  • 82
surya.in
  • 1,501
  • 2
  • 10
  • 3
  • 6
    sounds like a repost of this question: http://stackoverflow.com/questions/650238/how-to-show-the-last-queries-executed-on-mysql – lazarus Oct 19 '11 at 07:44
  • Possible duplicate of [How to show the last queries executed on MySQL?](https://stackoverflow.com/questions/650238/how-to-show-the-last-queries-executed-on-mysql) – Siraj Alam Apr 30 '18 at 09:32

8 Answers8

210

try

 cat ~/.mysql_history

this will show you all mysql commands ran on the system

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Maysam Torabi
  • 3,672
  • 2
  • 28
  • 31
40

For MySQL > 5.1.11 or MariaDB

  1. SET GLOBAL log_output = 'TABLE';
  2. SET GLOBAL general_log = 'ON';
  3. Take a look at the table 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';

As mentioned by jeffmjack 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).

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

More info here: Server System Variables

Skippy le Grand Gourou
  • 6,976
  • 4
  • 60
  • 76
GTodorov
  • 1,993
  • 21
  • 24
  • 4
    Just some clarification to the above: The copy-paste commands here are for a single session of MySQL, e.g. you can enter these from the interactive MySQL command line. If you want them to be a permanent feature of your MySQL instance, you need to put those commands in /etc/mysql/my.cnf and restart the MySQL service. – jeffmjack Feb 06 '19 at 14:24
  • Also note that logging will stop if `/path/to/your/logfile.log` is deleted, until you execute `SET GLOBAL general_log_file = "/path/to/your/logfile.log";` again (or maybe restart the session if you edited the configuration ?). – Skippy le Grand Gourou Aug 26 '19 at 13:33
  • for further clarification of @jeffmjack 's answer, the format of the commands in `/etc/mysql/my.cnf` is of the form `general_log=on` – Kai Carver Jun 15 '22 at 11:24
38

You 'll find it there

~/.mysql_history

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

sed "s/\\\040/ /g" < .mysql_history
DimiDak
  • 4,820
  • 2
  • 26
  • 32
22

(Linux) Open your Terminal ctrl+alt+t run the command

 cat ~/.mysql_history

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

NooBskie
  • 3,761
  • 31
  • 51
2

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

Blackcoat77
  • 1,574
  • 1
  • 21
  • 31
2

@GoYun.Info answer but with python 3

cat ~/.mysql_history | python3 -c "import sys; print(''.join([l.encode('utf-8').decode('unicode-escape') for l in sys.stdin]))" 
Sérgio
  • 6,966
  • 1
  • 48
  • 53
0

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

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

GoYun.Info
  • 1,356
  • 12
  • 12
-1

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

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.

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

Community
  • 1
  • 1
Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144