Tuesday, December 18, 2007

Create aliases to make MySQL administration easier

When working in a Unix/Linux/Mac OS environment, aliases can make things a lot easier if you are running the same commands over and over again. Here are a few aliases you can define to simplify running MySQL commands. Then just type the alias name. Put these in one of your configuration files dependent on the Unix shell you are running so these aliases will be automatically set in your environment.

Alias examples ($ represents the command line prompt):
$ alias myalias='alias | grep mysql'
$ alias myenv='env | grep mysql '
$ alias mystat='mysqladmin -u root -p status'
$ alias myestat='mysqladmin -u root -p extended-status'
$ alias myping='mysqladmin -u root -p ping'
$ alias myrun='ps -aux | grep mysql | grep -v grep'
$ alias mysql='mysql -u root -p'
$ alias mystop='mysqladmin -u root -p shutdown'
$ alias mysafe='nohup /opt/mysql/5.1.22/bin/mysqld_safe -- defaults-file=/dbadmin/mysql/startup/my.cnf & '

To view your aliases type the following:
$ alias

There are a lot of additional aliases you can define to make your daily tasks easier. Normally aliases are put in a startup file (in the $HOME directory) that is executed each time a Unix shell is started.
  • In the Korn shell (ksh) put the aliases in the .kshrc file.
  • In the Bash shell (bash) put the aliases in the .bashrc file.

2 comments:

Anonymous said...

Another thing I find useful is to change the prompt of the mysql client. I think it's in the manual somewhere, but in ~/.my.cnf (or any other my.cnf type file) you can add a line like this to the [mysql] section:

prompt=mysql\\_\\u@\\h/\\d>\\_

This one shows user, host, and database in the prompt, like so:

mysql dba@melody/mail>

Unknown said...

Thanks for the addition. Customizing your prompt to identify your environment in MySQL is important.