How do I display a list of all databases under MySQL database server running on a Linux or Unix-like system?
You need to use the show databases SQL command. First you need to login as MySQL database root user using mysql command line client. Type the following command to login with a password at a shell prompt:
$ mysql -u USERNAME -h HOSTNAME -p
$ mysql -u root -p
At mysql prompt type the following command (show databases;):
mysql> show databases;
Sample output:
+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | wiki | +--------------------+ 3 rows in set (0.03 sec)
Of course your can use the following shell one liner to get the list of all databases too:
# Connect to the local server mysql -u root -h localhost -p -e 'show databases;' # Connect to the remove mysql server mysql -u root -h 192.168.1.250 -p -e 'show databases;' |
# Connect to the local server
mysql -u root -h localhost -p -e ‘show databases;’ # Connect to the remove mysql server
mysql -u root -h 192.168.1.250 -p -e ‘show databases;’
Sample outputs:

For your shell script, you can just get a list of all of databases using the following syntax:
mysql -u root -h localhost -p'MyPasswordHere' -e 'show databases;' | awk '{ print $1 }' # OR store it in $DBS shell variable DBS=$(mysql -u root -h localhost -p'MyPasswordHere' -e 'show databases;' | awk '{ print $1 }') echo "List of database - $DBS" |
mysql -u root -h localhost -p’MyPasswordHere’ -e ‘show databases;’ | awk ‘{ print $1 }’
# OR store it in $DBS shell variable
DBS=$(mysql -u root -h localhost -p’MyPasswordHere’ -e ‘show databases;’ | awk ‘{ print $1 }’)
echo "List of database – $DBS"
(adsbygoogle = window.adsbygoogle || []).push({});