Poor Man’s “top” for MySQL

2009/06/18

I’m currently working at a startup that’s small enough that we don’t have a dedicated DBA and I’ve been doing a lot of mysql maintenance work recently. I wanted a quick dashboard for which commands were currently running and how long they’ve been running for. Sort of like top but for mysql.

Combining the unix “watch” command with the mysql “show processlist” command gives me what I’m looking for. A quick, self-updating status of the current state of the database.

watch -n 5 --differences "mysql -u username -psekrit -e 'show processlist'"

Shows something like this:

Every 5.0s: mysql -n 5 -u username -psekrit -e 'show processlist'                                   Thu Jun 18 05:25:14 2009
 
Id      User    Host       db      Command Time    State         Info
3141    admin   localhost  mydb    Query   34978   freeing items SELECT id, type, active, email FROM user WHERE email
3146    admin   localhost  mydb    Sleep   0                     NULL       
24876   root    localhost  NULL    Query   0       NULL          show processlist

Where the “time” column is the number of seconds the query has been running, and Info holds the actual query (you can use “show full processlist” to see the full query).

Watch is a nice little linux util that runs a command every “n” seconds (it defaults to 2 seconds). If you use the –differences switch, it will highlight the differences between one update and another. I use watch for all kinds of monitoring activities, such as watching a directory to see a file grow in size as it gets transfered.

Here’s a quick shell function that you can add to your .bashrc/.bash_profile/.zshrc to use on arbitrary hosts:

function mysqltop() {
    MYSQL_OPTS=$@
    watch -n 5 --differences "mysql $MYSQL_OPTS -e 'show processlist'"
}

Then just pass in any creds/host info you need like a normal mysql command:

mysqltop -u ted -psekrit1 -h example.com -P 3307

If you’re on linux, you probably already have “watch” installed. If you’re on OSX, you probably don’t, but you can get it quickly through macports. Install macports, make sure “port” is in your path and run:

sudo port install watch

There is also a command called mytop that you can get which looks like the same thing, but prints out the processlist details with some nicer formatting and a little extra information. It’s in macports, but it has a number of dependencies including mysql so if you didn’t install mysql through macports, you might want to stick with what I have above or get it another way.

(EDIT: updated with bash function)

There are 3 comments in this article:

  1. 2009/06/18Alex Vollmer say:

    I love watch. The first time I saw somebody use it, I was blown away.

    Have you seen “MyTop”? I’ve used it on other projects and it does something pretty similar to what you’ve got with a couple of extras. http://jeremy.zawodny.com/mysql/mytop/

    It looks like mytop was originally targeted to mysql 3.x and 4.x, but http://ebergen.net/wordpress/2006/12/09/mytop-support-for-50/ has a patch. Might be worth checking out (or not).

    Cheers,

    Alex

  2. 2009/06/18tednaleid say:

    A poster over on DZone also mentioned mtop which I wasn’t familiar with. A quick search doesn’t show it in macports, so it’s still something that’s a little more difficult to cobble together than what I have above, but it could be worth a look if you’re want something better than the “poor man’s” version :).

  3. 2009/06/18tednaleid say:

    @Alex heh, you were actually the first person I ever saw use watch over at HTS. I was similarly blown away and use it for all kinds of stuff now :).

    I mentioned mytop briefly in the post and it does look a little better, but it’s less ubiquitous than just having watch installed (plus mysql of course).

Write a comment: