Neat Tricks for the MySQL Command-line Pager

How many of you use the MySQL command-line client?  And did you know about the pager command you can give it?  It’s pretty useful.  It tells MySQL to pipe the output of your commands through the specified program before displaying it to you.

MySQL Command-line Pager

Here’s the most basic thing I can think of to do with it: use it as a pager. (It’s scary how predictable I am sometimes, isn’t it?)

For big result sets, it’s a pretty handy way to be able to search and scroll through. No mouse required, of course.

But it doesn’t have to be this simple! You can specify anything you want as a pager. Hmm, you know what that means? It means you can write your own script and push the output through it. You can’t specify arguments to the script, but since you can write your own, that’s not really a limitation. (Edit: I’m wrong! You can. See Giuseppe’s comment below.) For example, here’s a super-simple script that will show the lock waits in the output of SHOW INNODB STATUS. Save this file as /tmp/lock_waits and make it executable.

Now in your mysql session, set /tmp/lock_waits as your pager and let’s see if there are any lock waits:

Pretty useful, isn’t it? But we can do even more. For example, the Maatkit tools are specifically designed to be useful at the command line in the traditional Unix pipe-and-filter manner. What sort of goodies can we think of here?

Now, that’s handy.

What are your favorite ideas?

Share this post

Comments (15)

  • Giuseppe Maxia

    Cool tricks. My most frequent usage of “pager” is “vim -” and then manipulate the output at will.

    I have a handy script that gives me the size of indexes in a database from a SHOW TABLE STATUS. (Useful when you need to calculate how much memory to allocate for a buffer).

    $ cat
    perl -lane ‘$i+= $F[17];END{print $i}’

    mysql> pager ./
    PAGER set to ‘./’
    mysql> show table status;

    mysql> nopager


    June 23, 2008 at 10:38 pm
  • Parvesh Garg

    This is cool. I have known (and ignored) this earlier, my bad. But something with mk-visual-explain (one of my favorite tools) is just great.

    Next, I would like to see some regex based piping that I can specify in my.cnf, something like

    pager ^explain = mk-visual-explain
    pager ^show engine = less
    pager .* = stdout

    I dont think I’m asking too much 🙂


    June 24, 2008 at 2:42 am
  • PaulM

    This is one I use a bit, requires a Unix/Linux OS though. You could do similar on MySQL on Windows.

    pager grep -v “Sleep” |sort -n -t “|” -k 7 |tail

    show processlist will now suppress sleeping sessions and display the top 10 oldest sessions.

    Thanks for the tip on passing any script… very nice. I have interesting tests to try out… or just use mytop eh?

    Have Fun


    June 24, 2008 at 4:32 am
  • Clint Byrum

    Parvesh, you can have your cake and eat it too.




    # find the last ^mysql>

    lastcmd=$(tail -n $goback $teefile | grep “^mysql> “|tail -1| awk -F”mysql> ” ‘{print $2}’|sed -e ‘s/\*/\\*/g’)

    echo lastcmd=$lastcmd

    if echo “$lastcmd” | egrep -qi “^\s*explain” ; then
    exec mk-visual-explain
    if echo “$lastcmd” | egrep -qi “^\s*show\s+engine” ; then
    fgrep -C3 -i “LOCK” | less

    One has to do a \T ~/.mysqltee before the \P superpager.bash , but thats nice because everything you do gets logged..

    Works reasonably well for something I hacked together with bash in 10 minutes. Would be nice if the pager could be passed args like “last command” but this is a decent enough hack until they implement that in version 6.1 of mysql. 😉

    June 24, 2008 at 4:39 pm
  • Ryan Lowe

    I use pager for two things:

    1) Save the results of the last query
    2) Replace EXPLAIN with mk-visual-explain

    Unfortunately, MySQL 5.0.51a and earlier writes to the tee file AFTER the pager has completed (and to .mysql_history only after you exit mysql), so getting the last command is impossible via these methods (however, it works if you are willing to issue queries twice …). You *can* still override commands (EXPLAIN, for example) by having scripts that error if the input does not conform to a standard; an example follows:


    use strict;
    use warnings FATAL => ‘all’;
    use English qw ( -no_match_vars );

    my $out;

    while ( my $line = ) {

    $out .= $line;


    # Log results to a file (always useful)
    # But only ever save one 🙂
    open (PAGER_LOG, ‘>/tmp/mysql_pager.log’);
    print PAGER_LOG “$out\n”;
    close (PAGER_LOG);

    # See if it an EXPLAIN
    eval { local $SIG{‘__WARN__’}; system(‘/usr/local/bin/mk-visual-explain /tmp/mysql_pager.log 2> /dev/null’); };

    if ($?) {

    print $out;


    June 24, 2008 at 9:34 pm
  • Ryan Lowe

    gah. The scrubber changed:

    while ( my $line = <STDIN>)


    while ( my $line = )

    June 24, 2008 at 9:35 pm
  • Javier Tacón

    pager less -niSFX

    June 25, 2008 at 2:59 am
  • Parvesh Garg

    Check my post for a small hack that can do regex based paging.

    July 8, 2008 at 9:55 am
  • Kevin Benton

    All – these are a nice set of improvements when using the command-line mysql. I took this a few steps further when looking more into mytop by Jeremy Zawodny. Want to see more – browse to th blog 🙂

    January 16, 2009 at 9:22 pm
  • Derek September 2, 2010 at 1:13 am
  • Mark

    Good stuff.

    Is there a way to set vi-style command editing for the mysql command line client? That would be super useful.

    August 30, 2012 at 7:19 am
  • Nik

    cat >> ~/.bashrc << EOF
    alias mysql="mysql –pager='less -niSFX'"

    October 2, 2012 at 2:17 pm
  • Carl

    On *nix systems, before your sql statement enter:
    \P more
    \P less

    June 3, 2013 at 5:59 am
  • Amos

    > pager column -t | less -S
    meaning you don’t have to look at wrapped rows, you can just scroll right and left

    September 26, 2013 at 1:36 pm
  • Amos

    Actually, I correct myself. 2 nice options for aligned nowrap browsing

    mysql> pager less -S


    mysql> pager vim -R -c “set nowrap” –

    September 26, 2013 at 5:03 pm

Comments are closed.

Use Percona's Technical Forum to ask any follow-up questions on this blog topic.