Check if a database exists in MySQL from the command line
Published by Nicholas Dunbar on November 1st, 2012
To check if a database exists from the command line in MySQL echo
the following query and pipe (|) it to the MySQL command line utility:
echo SELECT COUNT(*) as num FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMATA.SCHEMA_NAME="some_database_name"; | mysql --debug-check --user root --host localhost --password some_password
This will return something like the following if it does exist:
num
1
If it does not exist it will return something like:
num
0