Check if mysql database exists, perform action based on result

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

    The mysqlshow path requires parsing the output (at least for the version of mysql I have) because it always returns success. Dale makes a very good point about differentiating between failures.

    However, if you know that everything is running and you have correct credentials, etc, and you want to tell only whether the DB exists are not you can do it in one line with a blank sql command:

    > mysql -uroot -ppassword good_db -e ''
    > echo $?
    0
    > mysql -uroot -ppassword bad_db -e ''
    ERROR 1049 (42000): Unknown database 'busker_core_locala'
    > echo $?
    1
    

提交回复
热议问题