Check if mysql database exists, perform action based on result

前端 未结 20 2027
傲寒
傲寒 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:24

    mysql_user=
    mysql_pass=
    target_db=
    if [ "`mysql -u${mysql_user} -p${mysql_pass} -e 'show databases;' | grep ${target_db}`" == "${target_db}" ]; then
      echo "Database exist"
    else
      echo "Database does not exist"
    fi
    

    This executes a MySQL query to get all DB names, then greps to check that the required database exists.

提交回复
热议问题