Skip to content

List mysql databases from the BASH command prompt

2010/07/04

After finding no good solutions to get a list of mysql databases without dropping into the mysql shell, inspiration delivered this little oneliner that does the trick quite handily.

$ echo "show databases;" | mysql

If your user does not have access to mysql, just pass the -u flag to the mysql command, like so:

$ echo "show databases;" | mysql -u [user with mysql access]

e.g.

$ echo "show databases;" | mysql -u root

If you need a password, modify the command with the -p flag.

$ echo "show databases;" | mysql -p -u [user with mysql access]

e.g.

$ echo "show databases;" | mysql -p -u root

You should be prompted for your password, and upon authenticating successfully, a list of databases will be printed to stdout (your terminal session) and you will be returned to the BASH command prompt.

$ hope this is useful, enjoy!

Advertisements

From → Linux, Productivity

2 Comments
  1. Has been a while since you posted this, but this is what I came up with:

    mysql -u[user] -p[password] -e ‘show schemas;’ | awk ‘{print $c}’ c=${1:-1}

    Only problem is that it returns the ‘Databases’ header, which I am not wanting in my result…

  2. anonymous permalink

    mysql –batch –skip-pager –skip-column-names –raw -u [user] -p[password] –execute=’SHOW DATABASES’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: