How to find an errant MySQL client

A common story: You’ve got some connection, either it’s busy running something it shouldn’t be, it’s in Sleep but holding some important lock, or you just don’t know why it’s connected to your database server in the first place. You see it in your SHOW PROCESSLIST like so:

mysql> show processlist G
     Id: 5979887
   User: root
   Host: localhost:55997
     db: NULL
Command: Sleep
   Time: 475
   Info: NULL

How do you find that client, especially if it’s on another host? MySQL is providing you all the information you need above: localhost:55997. Of course localhost is the host or IP address, and 55997 is the source port of the socket; the port number (usually randomly assigned) on the far end of the socket, from the MySQL server’s perspective. You can turn that number into something useful—the PID and user—, by running the following command on the host that made the connection:

# lsof -nPi :55997
mysqld  5026 mysql 1654u  IPv4 303329996       TCP> (ESTABLISHED)
mysql   9146 jcole    3u  IPv4 303329995       TCP> (ESTABLISHED)

(Note that here you can see both sides of the socket, since I’m running these commands on localhost. Disregard the first entry, as it’s the half of the connection owned by the MySQL server.)

You can then find out the full command-line of the process with ps:

# ps -fp 9146
jcole     9146  8740  0 12:53 pts/3    00:00:00 mysql -h -u root -p

And see what it’s doing with strace (waiting on a read of stdin in this dumb test):

# strace -p 9146
Process 9146 attached - interrupt to quit
read(0,  <unfinished ...>
Process 9146 detached

I hope that’s helpful! It’s a pretty common debugging trick for me.

4 thoughts on “How to find an errant MySQL client

  1. Tweets that mention jcoleâ€™s weblog: Jeremy Coleâ€™s take on life. Â» Blog Archive Â» How to find an errant MySQL client --

  2. what kind of MySQL patch you used? i check my 5.0 and 5.1 mysql “show processlist” find on port in local connection

    *************************** 13. row ***************************
    Id: 157
    User: root
    Host: localhost
    db: NULL
    Command: Query
    Time: 0
    State: NULL
    Info: show processlist

    • justlooks: If you connect with Unix socket (the default) there is no TCP port, and thus none is shown. If you want to force a localhost connection to use TCP/IP, use -h

What do you think?


You are commenting using your account. Log Out /  Change )

Google photo


Twitter picture


Facebook photo


Connecting to %s