Tag Archives: alias

MySQL configuration tips and tricks

A web developer often has to access different MySQL databases for different projects. To quickly navigate through them, I like to use project-specific MySQL configuration snippets and aliases. My projects usually have some kind of a namespace, a 2-3 character code specific to that project. Just to give an example, let’s call my project Xylophone Ytterbium Zeta, and its namespace code is XYZ.

I would create a my.cnf file in my project’s directory with the following content:

host = localhost
user = xyzuser
password = xyzpassword
database = xyzdatabase

If I now run:

$ mysql --defaults-file=~/projects/xyz/my.cnf

I will be logged into the project’s database immediately, without having to enter my credentials.

However, the --defaults-file argument is quite a long argument to type in every time, so I would create a bash alias, so that all I need to do is:

$ myxyz

to get into the database. To create this bash alias, I’d simply add the following line to my ~/.bash_aliases file:

alias myxyz="mysql --defaults-file=~/projects/xyz/my.cnf"

Of course your .bash_rc file needs to know about this file, so check if you have the following snippet in there:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases

The latest Ubuntu versions should have that already prepared for you.