Skip to content

Instantly share code, notes, and snippets.

@dotancohen
Last active May 16, 2017 08:33
Show Gist options
  • Save dotancohen/dc9c8e3abd7834323c9fdc3375ca05bc to your computer and use it in GitHub Desktop.
Save dotancohen/dc9c8e3abd7834323c9fdc3375ca05bc to your computer and use it in GitHub Desktop.
Log into MySQL databases easily and securely

Log into MySQL databases via preconfigured aliases. Note that the alias name 'foo' is to be identified in the config file as 'clientfoo'.

$ cat ~/.bashrc

my() {
    /usr/bin/mysql --defaults-group-suffix="$@"
}

myd() {
    /usr/bin/mysqldump --defaults-group-suffix="$@"
}

$ ls -l ~/.my.cnf
-rw------- 1 dotancohen dotancohen 245 Apr 24 17:10 /home/dotancohen/.my.cnf

$ cat ~/.my.cnf 

[clientfoo]
host=localhost
user=root
password=toor

[clientbar]
host=1.2.3.4
user=snowden
password=letmein
database=nsa_leaks

$ my foo
MySQL [(none)]> exit

$ my bar
MySQL [nsa_leaks]> exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment