How to Check if a MySQL Database Exists Using Shell Script


1 views

While mysqladmin create does provide exit status information about database existence (0 for success, 1 for failure), it has a significant side effect - it actually creates the database if it doesn't exist. This makes it unsuitable for pure checking purposes in automated scripts.

The mysqlshow utility is perfect for this task as it's designed specifically for database inspection without modification:

#!/bin/bash
DB_NAME="foo"
if mysqlshow -uroot "$DB_NAME" > /dev/null 2>&1; then
    echo "Database $DB_NAME exists"
else
    echo "Database $DB_NAME does not exist"
fi

For more flexibility, you can query MySQL's metadata directly:

#!/bin/bash
DB_NAME="foo"
if mysql -uroot -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$DB_NAME'" \
   | grep -q "$DB_NAME"; then
    echo "Database exists"
else
    echo "Database does not exist"
fi

For production scripts, consider using MySQL option files instead of command-line credentials:

#!/bin/bash
DB_NAME="foo"
if mysql --defaults-extra-file=/path/to/my.cnf -e "SHOW DATABASES LIKE '$DB_NAME'" \
   | grep -q "$DB_NAME"; then
    echo "Database exists"
fi

The mysqlshow method is generally fastest for simple existence checks. The information_schema query becomes more efficient when you need to check multiple databases or require additional metadata.

Always include proper error handling in your scripts:

#!/bin/bash
DB_NAME="foo"
if ! mysql -uroot -e "EXIT" 2>/dev/null; then
    echo "MySQL connection failed" >&2
    exit 1
fi

The simplest way to check if a MySQL database exists is by using the mysqlshow utility. This tool comes bundled with MySQL and provides a clean way to verify database existence without modifying anything.


if mysqlshow -uroot dbname &>/dev/null; then
    echo "Database exists"
else
    echo "Database doesn't exist"
fi

For more programmatic control, you can query MySQL's information_schema database directly:


if mysql -uroot -e "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'dbname'" | grep -q dbname; then
    echo "Database exists"
else
    echo "Database doesn't exist"
fi

Unlike mysqladmin create which creates the database if it doesn't exist, these methods are purely for checking existence:


# This won't create the database if it doesn't exist
RESULT=$(mysql -uroot -e "SHOW DATABASES LIKE 'dbname'" | grep -v Wildcard | grep -c dbname)
if [ "$RESULT" -eq 1 ]; then
    echo "Database exists"
else
    echo "Database doesn't exist"
fi

For production scripts, avoid putting passwords in command lines. Use MySQL option files instead:


if mysql --defaults-extra-file=/path/to/my.cnf -e "SHOW DATABASES LIKE 'dbname'" | grep -q dbname; then
    echo "Database exists"
fi

For frequent checks in automated scripts, the mysqlshow method is generally fastest as it doesn't require parsing SQL output. The information_schema query provides more flexibility if you need additional database metadata.