Mysql - Examine last query run by thread in sleep state I am seeing some threads in the "sleep" state for longer than normal. Normal maybe being 1-3 seconds with the abnormal being up to 60 seconds or longer. It makes sense that this is an application issue so my question is if it's possible to see the last query run by a thread that is in a sleep state. From there I can hopefully track down the code that is leaving these connections open.
First you have to set general logs of mysql by add lines in /etc/mysql/my.cnf 's mysqld section.

general_log     = 1
general_log_file        = /var/log/mysql/mysql.log


Restart mysql -

sudo service mysql restart


If you don't want to restart mysql server-

SET global log_output = 'FILE';
SET global general_log_file='/var/log/mysql/mysql.log';
SET global general_log = 1;


If you wants to read logs from table, mysql database have a general_log table which insert all logs. you can get by- 

   SET global log_output = 'table';
   SET global general_log = 1;
   SELECT * FROM mysql.general_log;


and you can disable by - 

SET global general_log = 0;


Now you can see those process which are in sleep state &amp; get its Id-

show full processlist;


open new tab and see the logs of ID &amp; its running command-

cat /var/log/mysql/mysql.log | grep ID_value


or if you read logs from table then-

 SELECT * FROM mysql.general_log where thread_id=1;


Be careful to use it on Production server, it may effect your server performance (due to additional disk write) if transactions committing on huge no. per second.