Save time and energy: How to …

Posted: 23rd February 2009 by Darren Cassar in Beginner, Databases, Intermediate, MySQL
Tags: , ,

Save time and energy: How to:

Listening to “Highway to Hell” in an Oxford University computer lab on a sunday afternoon writing a MySQL blog can be legitimally defined by some as sick but thats what a geek calls a relaxing yet productive sunday afternoon.

For the sake of all those CLI ambassadors like myself, I wanted to share a couple of tips and tricks I use quite often when doing my mysql db administration work / scripting. I strongly suggest any mysql dba / dev use CLI simply because thats the one thing you should always have on any machine on which mysql is installed, be it Unix, Linux, Mac, Microsoft or whatever platform you are using.

Cancelling a query you are typing without exiting to the terminal:
`\c` – How many times you ended up hitting `CTRL+C` (default process kill in most OSes) in order to cancel a command, finding yourself going back to the console and having to connect again? Nuisence? Hell yeah! Especially if you had a number of temp tables waiting to be used.


A `CTRL+C` would bring send you to console:
mysql> SELECT
-> User, Host
-> OOPS I DID A MISTAKE
-> FROM --Hit `CTRL+C` here
-> Aborted
mysqlpreacher:~ darrencassar$

A `\c` in the mysql program would just cancel the wrong query:
mysql> SELECT
-> User, Host
-> OOPS I DID A MISTAKE
-> FROM
-> \c
mysql>

BTW it doesn’t work with the USE command – it gives an error but at least doesn’t terminate the connection:

mysql> USE m\c
ERROR 1049 (42000): Unknown database 'mc'
mysql>

Running a console command from within a mysql terminal session:
`\!` – Need to run a console command while you are logged in a mysql terminal session? There is no need to exit the current session or to dupplicate the terminal session and run the command, just use `\!`


mysql> \! pwd
/var/root/sandboxes/mysql_5130
mysql> \! ls
COPYING VERSION data my restart stop
README clear grants.mysql my.sandbox.cnf send_kill use
USING current_options.conf load_grants proxy_start start
mysql> \! ps -ef | grep mysql | grep -v grep
0 36319 1 0 0:00.01 ttys002 0:00.02 /bin/sh /mysql/releases/5.1/5.1.30/bin/mysqld_safe --defaults-file=/var/root/sandboxes/mysql_5130/my.sandbox.cnf
0 36389 36319 0 0:00.14 ttys002 0:01.48 /mysql/releases/5.1/5.1.30/bin/mysqld --defaults-file=/var/root/sandboxes/mysql_5130/my.sandbox.cnf --basedir=/mysql/releases/5.1/5.1.30 --datadir=/var/root/sandboxes/mysql_5130/data --user=root --log-error=/var/root/sandboxes/mysql_5130/data/msandbox.err --pid-file=/var/root/sandboxes/mysql_5130/data/mysql_sandbox3308.pid --socket=/tmp/mysql_sandbox3308.sock --port=3308
0 36987 26344 0 0:00.01 ttys002 0:00.02 mysql -u root -p --auto-rehash -h 127.0.0.1 -P 3308
0 36992 36987 0 0:00.00 ttys002 0:00.00 sh -c ps -ef | grep mysql
mysql>

Autocompletion on command line mysql:
`\#`, `rehash` or `–auto-rehash` – Autocompletion is fast, handy and help you produce more in less.
Autocompletion in MySQL is enabled by default, and there is a need to use –disable-auto-rehash (note that –no-auto-rehash or -A are deprecated) in order to disable rehashing which makes mysql start faster. If this is done, then it can be called upon using either of the following `\#`, `rehash` or using `–autu-rehash` depending on where you are and what you are comfortable with. If you are already in a mysql terminal session, then type in `\#` or `rehash`. Another option is to connect to mysql using: `mysql -u user -p –auto-rehash` as a startup option. Having done that, remember to initialise the hashing by issueing a `USE DATABASENAME` as otherwise the autocompletion won’t work. Autocompleting a word / name can be done in a similar way to what you’d do in CL UNIX, LINUX or DOS, typing the first part of the word and then pressing `TAB`.

–auto-rehash:

mysqlpreacher:~ darrencassar$ mysql -u root -p --auto-rehash -h 127.0.0.1 -P 3308
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.30 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> use m
max_connections max_questions max_updates max_user_connections modified mysql
mysql> use mysql
Database changed

\# or rehash:

mysqlpreacher:~ darrencassar$ mysql -u root -p -h 127.0.0.1 -P 3308
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.1.30 MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> \#
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> use m
max_connections max_questions max_updates max_user_connections modified mysql
mysql> use mysql

more tips and tricks will follow in future blogs,

Asta la vista mysql user!

VN:F [1.9.22_1171]
Rating: 7.7/10 (3 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Save time and energy: How to …, 7.7 out of 10 based on 3 ratings

*