How to show the last queries executed on MySQL?

后端 未结 10 722
感情败类
感情败类 2020-11-22 07:03

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

10条回答
  •  伪装坚强ぢ
    2020-11-22 07:18

    After reading Paul's answer, I went on digging for more information on https://dev.mysql.com/doc/refman/5.7/en/query-log.html

    I found a really useful code by a person. Here's the summary of the context.

    (Note: The following code is not mine)

    This script is an example to keep the table clean which will help you to reduce your table size. As after a day, there will be about 180k queries of log. ( in a file, it would be 30MB per day)

    You need to add an additional column (event_unix) and then you can use this script to keep the log clean... it will update the timestamp into a Unix-timestamp, delete the logs older than 1 day and then update the event_time into Timestamp from event_unix... sounds a bit confusing, but it's working great.

    Commands for the new column:

    SET GLOBAL general_log = 'OFF';
    RENAME TABLE general_log TO general_log_temp;
    ALTER TABLE `general_log_temp`
    ADD COLUMN `event_unix` int(10) NOT NULL AFTER `event_time`;
    RENAME TABLE general_log_temp TO general_log;
    SET GLOBAL general_log = 'ON';
    

    Cleanup script:

    SET GLOBAL general_log = 'OFF';
    RENAME TABLE general_log TO general_log_temp;
    UPDATE general_log_temp SET event_unix = UNIX_TIMESTAMP(event_time);
    DELETE FROM `general_log_temp` WHERE `event_unix` < UNIX_TIMESTAMP(NOW()) - 86400;
    UPDATE general_log_temp SET event_time = FROM_UNIXTIME(event_unix);
    RENAME TABLE general_log_temp TO general_log;
    SET GLOBAL general_log = 'ON';
    

    Credit goes to Sebastian Kaiser (Original writer of the code).

    Hope someone will find it useful as I did.

提交回复
热议问题