Is it possible from within a bash script to check if a mysql database exists. Depending on the result then perform another action or terminate the script?
FWIW, the auth_socket
plugin makes this much easier. The question may be super old, but there are still people like me coming here for inspiration.
If your script is running as root, you can do this:
DBNAME="what_you_are_looking_for"
DBEXISTS="$(mysql -u root -e "show databases like '$DBNAME'" --batch --skip-column-names)"
If the database exists, then $DBNAME = $DBEXISTS
.
If the database does not exist, then $DBEXISTS = ""
.
Both should have an exit status of 0, so you can still use non-zero statuses to report errors, rather than letting a non-existent database appear as an error.
Following command should do the trick for both the cases,
mysqlshow "DB_NAME" &> /dev/null && echo "YES" || echo "NO"