Check if mysql database exists, perform action based on result

前端 未结 20 2029
傲寒
傲寒 2021-02-01 16:02

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?

20条回答
  •  不思量自难忘°
    2021-02-01 16:23

    I give +1 to answer by @chown, but here's another alternative: If the bash script is running locally with the MySQL instance, and you know the path to the datadir, you can test:

    if [ -d /var/lib/mysql/databasename ] ; then 
        # Do Stuff ...
    fi
    

    This also assumes your shell user running the script has filesystem-level privileges to read the contents of the MySQL datadir. This is often the case, but it is not certain.

提交回复
热议问题