Find last query executed by specific session on MySQL server

MySQL allows you to check the queries executed by each session. From these results we will take the last query executed by a specific session

Preparation

First you need to turn on the MySQL query log, which you can do it by executing the following queries

set global log_output = 'table';
set global general_log = 'on';

Next you can see all the executed queries

select * from mysql.general_log;

Result

Sample result of the executed queries

Find the last query

Now we will find the last query executed by a specific session, e.g. 'root'

select event_time as time,
    user_host,
    thread_id,
    server_id,
    command_type,
    argument
from mysql.general_log
where user_host like 'root%'
order by event_time desc
limit 1;

Columns

  • event_time - time the query was executed
  • user_host - specific user name, host name and connection address
  • thread_id - thread identification
  • server_id - server identification
  • command_type - command type
  • argument - query executed

Rows

  • row: the query returns one row with the last query information

Sample results

Sample results for the session 'root'