Linux 检查mysql数据库是否存在,根据结果执行操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7364709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:07:03  来源:igfitidea点击:

Check if mysql database exists, perform action based on result

mysqllinuxbash

提问by David

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?

是否可以从 bash 脚本中检查 mysql 数据库是否存在。根据结果​​然后执行另一个操作或终止脚本?

采纳答案by chown

Example script (Thanks to Bill Karwin for the --userand --passwordcomment!):

示例脚本(感谢比尔Karwin为--user--password评论!):

#!/bin/bash
## --user=XXXXXX --password=XXXXXX *may* not be necessary if run as root or you have unsecured DBs but
##   using them makes this script a lot more portable.  Thanks @billkarwin
RESULT=`mysqlshow --user=XXXXXX --password=XXXXXX myDatabase| grep -v Wildcard | grep -o myDatabase`
if [ "$RESULT" == "myDatabase" ]; then
    echo YES
fi

These are what the commands look like when run at a prompt:

这些是在提示符下运行时命令的样子:

[root@host ~]# mysqlshow myDatabase
Wildcard: myDatabase
+------------------+
|    Databases     |
+------------------+
| myDatabase       |
+------------------+

If no DB exists, the output will look like this:

如果不存在 DB,输出将如下所示:

[root@host ~]# mysqlshow myDatabase
Wildcard: myDatabase
+-----------+
| Databases |
+-----------+
+-----------+

Then, parse the output and do what you need to based on if it exists or not!

然后,解析输出并根据它是否存在执行您需要的操作!

回答by ajreal

YES

是的

for db in $(mysql -u -p -N <<<"show databases like '%something%'")
do
  case $db in 
    "something")
      // do something
    ;;
    "something else")
      // do something else
    ;;
  esac
done

回答by Cfreak

Use the -eoption to the mysqlcommand. It will let you execute any query (assuming the right credentials).

使用命令的-e选项mysql。它将让您执行任何查询(假设凭据正确)。

This may be an example:

这可能是一个例子:

if mysql "DATABASE_NAME" -e exit > /dev/null 2>&1; then
    echo "Exists"
else
    echo "Not exists"
fi

回答by Bill Karwin

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:

我给@chown+1回答,但这是另一种选择:如果 bash 脚本与 MySQL 实例一起在本地运行,并且您知道 datadir 的路径,则可以测试:

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.

这还假设您运行脚本的 shell 用户具有读取 MySQL 数据目录内容的文件系统级权限。这种情况经常发生,但并不确定。

回答by Matthieu Napoli

I couldn't get the accepted answer work for me (the grepin the quotes didn't work), so here is my version:

我无法为我获得接受的答案(grep引号中的无效),所以这是我的版本:

RESULT=`mysql -u $USER -p$PASSWORD --skip-column-names -e "SHOW DATABASES LIKE 'myDatabase'"`
if [ "$RESULT" == "myDatabase" ]; then
    echo "Database exist"
else
    echo "Database does not exist"
fi

I used the option --skip-column-namesto remove the column names from the result.

我使用该选项--skip-column-names从结果中删除列名。

回答by mattes

mysqlshow "test" > /dev/null 2>&1 && echo "Database exists."

Depending on the exit status of the mysqlshow command, it will execute the following echo.

根据 mysqlshow 命令的退出状态,它将执行以下 echo。

回答by cavernicola

Following command should do the trick for both the cases,

以下命令应该可以解决这两种情况,

mysqlshow "DB_NAME" &> /dev/null && echo "YES" || echo "NO"

回答by gurel_kaynak

Here is an alternate version:

这是一个替代版本:

 RESULT=`mysql -u$USER -p$PASSWORD -e "SHOW DATABASES" | grep $DATABASE`
 if [ "$RESULT" == "$DATABASE" ]; then
    echo "Database exist"
 else
    echo "Database does not exist"
 fi

IF there is a DB named abcdand we use -Foafter grepthen for the search result of DB a/ab/abcthe script will show the result Database exist.

如果有一个名为DBabcd和我们使用-Fogrep则DB的搜索结果a/ ab/abc该脚本将显示结果Database exist

回答by Fedir RYKHTIK

if [ $(mysqlshow DB 1>/dev/null 2>/dev/null) -eq 0 ]; then
    echo "DB found"
fi

回答by busy

mysqlshowwill not show underscore characters '_' in the database name.

mysqlshow不会在数据库名称中显示下划线字符“_”。

mysqlshow $DOMAIN %

https://dev.mysql.com/doc/refman/5.1/en/mysqlshow.html

https://dev.mysql.com/doc/refman/5.1/en/mysqlshow.html