Quantcast
Channel: mysql – David Goodwin
Viewing all articles
Browse latest Browse all 8

Finding and killing long running MySQL queries

$
0
0

Sometimes (Woocommerce?) related queries on a MySQL server can seemingly run forever and eventually lead to the server being clogged up (and few free connections).

Something like this can help …. (i.e. kill all MySQL queries which have been running longer than 900 seconds, and aren’t from the ‘root’ user).

 

#!/bin/bash

# assume e.g. ~/.my.cnf exists with user/password etc.

QUERY="SELECT id FROM information_schema.processlist
     WHERE command <> 'Sleep' 
     AND info NOT LIKE '%PROCESSLIST%'
     AND command <> 'Killed' 
     AND user != 'root' AND time > 900"

for PID in $(mysql -u root -BNe $QUERY )
do
    # Log it ... 
    LOG_QUERY="SELECT * FROM information_schema.processlist WHERE id = $PID \G"

    OUTPUT=$(mysql -u root -NBe $LOG_QUERY)
    echo "Long running query : $OUTPUT "
    
    # If on AWS RDS then : mysql -u root -e "call mysql.rds_kill($PID)"
    # otherwise, in a normal environment :
    mysql -u root -e "KILL $PID"
done

 


Viewing all articles
Browse latest Browse all 8

Trending Articles