The more popular ways of using MySQL are to use GUI front ends, such as phpMyAdmin or the MySQL GUI administration tools. While these tools are fantastic and make managing a MySQL database easier, the mysql command-line program works quite well also, and is more flexible.

You can customise the look of the mysql prompt, which may ease using it somewhat as the default prompt is simply mysql>, which is hardly informative. You can customise this to show the connected username, host, and current database using:

mysql> prompt mysql (u@h)::d >_
PROMPT set to 'mysql (u@h)::d >_'
mysql (root@localhost)::db1 >

To make this setting permanent, edit the ~/.my.cnf file and add:

[mysql]
prompt=mysql (u@h)::d >_

Another nice and quick use for the mysql command-line client is that you can call and parse MySQL output from shell scripts without interactive sessions. For instance:

$ mysql -u me --password=secret wiki -e "select count
(page_id) from page"

+----------------+
| count(page_id) |
+----------------+
| 131 |
+----------------+

If you wish to use something easier to parse, use a vertical format by appending the \G identifier at the end of the query:

$ mysql -u me --password=secret wiki -e "select count
(page_id) from pageG"

*************************** 1. row
***************************

count(page_id): 131

Since passing the password on the command-line could be picked up by other processes if the Linux kernel doesn't support process hiding, consider defining it in the ~/.my.cnf file:

[mysql]
user = me
password = secret

Make sure that ~/.my.cnf is mode 0600 so only the user can read (and write to) the file. With that setting, user and password requirements can be omitted, so the above could be executed as:

$ mysql wiki -e "select count(page_id) from page\G"

Of course, if you want to connect to MySQL as a different user, simply supply the -u option with the appropriate username and the -p option to prompt for a password. What is defined in ~/.my.cnf is just a default.

Open Sourcery This was published in Open Sourcery, check every Monday for more stories

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

Log in


Sign up | Forgot your password?

  • Staff XP stays on life support for longer

    This week's Roundup looks at Microsoft's decision to extend the life of Windows XP, the release of Microsoft Surface SDK, Firefox's new Geode plug-in, Yahoo's new tool -- Smush It and more. Read more »

    -- posted by Staff

  • Chris Duckett The good and truly awful celluloid depictions of computers

    Ever wonder why your lawyer uncle leaves the room whenever you turn over to Boston Legal? Or why your forensic science cousin can't stand crime drama? You know the answer: it’s the horrid trivialisation and dumbing down of an occupation to make it appear entertaining. Sometimes it is so unbelievable that it actually hurts and yelling at the screen is the only outlet. Read more »

    -- posted by Chris Duckett

  • Brendon Chase Apple's iPhone engineers to tour Sydney, Melbourne

    Aussie developers will be able to get up close and personal with some of the iPhone engineers in November to learn how to build applications for the platform. Read more »

    -- posted by Brendon Chase

What's on?