Mysql – How to configure MySQL command line client key bindings

command-line-interfacekey-bindingskeysMySQL

I often use the mysql command-line client on an Ubuntu box. It works fine, except that the key bindings are wrong. Specifically, pressing "Home", "End", or "Delete" makes it output a ~ (tilde) character, and beep.

I've determined that Home/End can be done by pressing Control+A and Control+E respectively, as on the Mac terminal. I'm on a Windows box, using PuTTY to talk to Ubuntu, but the home/end/delete keys work fine on the regular command line. I'm pretty sure it's an issue specific to the mysql client.

I checked the man file but didn't find anything useful. Is there any way to bind the Home/End/Delete keys to their actual functions? Maybe a config file in my home directory or something?

Every time I move back a few spaces to edit a query and press delete, I just wind up having to go forward again so I can use the Backspace key to delete not only the incorrect query but also the ~ that the delete key puts in.

It's driving me slowly but surely to madness. Madness, I tell you, MADNESS!

Best Answer

Okay, I noticed that my home server did not have the problem, and comparing the work and home servers let me finally nail it down.

It's a bug Ubuntu introduced in version 9.10, which is what my work server is running. Specifically, the version of /usr/bin/mysql in Ubuntu 9.10 uses the EditLine wrapper instead of the readline wrapper:

wmartin@work:~$ mysql --version && md5sum /usr/bin/mysql && lsb_release -a

mysql  Ver 14.14 Distrib 5.1.37, for debian-linux-gnu (i486) using EditLine wrapper

e23604381e64ac805fdbb8b334ce4d17  /usr/bin/mysql

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 9.10
Release:        9.10
Codename:       karmic

Compare to the more recent release on my home server:

wmartin@home:~$ mysql --version && md5sum /usr/bin/mysql && lsb_release -a

mysql  Ver 14.14 Distrib 5.1.41, for debian-linux-gnu (i486) using readline 6.1

4274c43798239142576e77c022d35bd1  /usr/bin/mysql

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 10.04.2 LTS
Release:        10.04
Codename:       lucid

The 10.04 (and presumably later) versions work just fine -- mysql honors the home/end/delete keys using Putty's default settings.

So the solution is either to upgrade the server to 10.04 or later, or to use the command line utility rlwrap to wrap the mysql client in a readline environment, as described in the blog post restoring readline capabilities of Ubuntu's 9.10 (Karmic Koala) MySQL Client.

Sadly, the rlwrap approach has the nasty side effect of making your password visible when you type it in instead of suppressing its output. So I think I'll take the upgrade route.