Emacs as MySQL frontend

After working quite a time with some minor MySQL databases in the (Emacs-) Terminal I just looked up the preinstalled SQL related emacs functions. Just entered M-x sql TAB and indeed the autocompletion showed a function sql-mysql, as expected…

I gave it a try with
M-x sql-mysql
and after prompting for database, servername, username and password Emacs connected to the database and presented the MySQL shell. So I bound sql-mysql to some keyboard shortcut, BUT entering the whole connection parameters each and every time was not acceptable.

Atomized.org has a really nice post on Enhancing Emacs’ SQL Mode (you can have a look, but you cannot read the post before pasting the content to a text editor). There I found some excellent functions which would provide a solution:

(setq sql-connection-alist
(sql-product 'mysql)
(sql-server "")
(sql-user "me")
(sql-password "mypassword")
(sql-database "thedb")
(sql-port 3306))
(sql-product 'mysql)
(sql-server "")
(sql-user "me")
(sql-password "mypassword")
(sql-database "thedb")
(sql-port 3307))))

(defun sql-connect-preset (name)
"Connect to a predefined SQL connection listed in `sql-connection-alist'"
(eval `(let ,(cdr (assoc name sql-connection-alist))
(flet ((sql-get-login (&rest what)))
(sql-product-interactive sql-product)))))

(defun sql-pool-a ()
(sql-connect-preset 'pool-a))

Now, you can just run sql-pool-a and get connected right away. Because the buffers have good names, you can easily fire up many connections.

I included it in my .emacs file and appended
(DefGlobKey "s-a" 'sql-pool-a)
and with a keystroke the database promt appears.

Thx, atomized.org, but what’s that webpage giving you an epileptic fit looking at it?!. Excellent page 🙂


6 thoughts on “Emacs as MySQL frontend

    1. Oops, that‘s true. Beautiful page – when I opened it in Chrome today.

      When I wrote the post, I did use IE8 on my job machine (the firewall does not accept anything else) the background was displayed as “dancing stars” the whole site was displayed as a “christmas on LSD” firework and the fond was completely vanishing….

      Maybe it was a red hering, but I will try to reproduce it, make a screenshot and notify you.

      Excellent work by the way atomized.org. Really appreciated!

      1. Ok, it is consistent… reproduced the bug on 2 different XP/IE8 at work… atomized.org gets messed up by IE8…

  1. Are there any security issues with having your user name in password in your .emacs file? Do you take other measures to keep them secure?

    1. Yes, there are of course security issues if the datatables contain sensitive information, which is not the case for me. Anyway it is important to do
      chmod 600 .emacs so that you are the only user (and root of course) who can read/write the .emacs file. If the data in the database is highly sensitive, I would not include the password in the .emacs file.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

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

Facebook photo

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

Connecting to %s