Check if mysql database exists, perform action based on result

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

    Here's how i did it inside a bash script:

    #!/bin/sh
    
    DATABASE_USER=*****
    DATABASE_PWD=*****
    DATABASE_NAME=my_database
    
    if mysql -u$DATABASE_USER -p$DATABASE_PWD -e "use $DATABASE_NAME";
    then
    echo "Database $DATABASE_NAME already exists. Exiting."
    exit
    else
    echo Create database
    mysql -u$DATABASE_USER -p$DATABASE_PWD -e "CREATE DATABASE $DATABASE_NAME"
    fi
    

提交回复
热议问题