【问题标题】:MariaDB --init-command when invoking mysql from a BASH script从 BASH 脚本调用 mysql 时的 MariaDB --init-command
【发布时间】:2020-02-02 08:48:27
【问题描述】:

我是 Stack Overflow 的新用户,因此对于任何可能违反网站礼仪的行为,我提前道歉。

我正在尝试创建一个 BASH 脚本,该脚本将生成一个命令来调用 MariaDB 监视器,即mysql。我希望这个mysql 命令包含--init-command 选项。我希望 --init-command 选项将用户变量列表设置为其值,如配置文件中指定的那样。

该脚本构建了一个似乎对我来说是正确的字符串,但是当它调用mysql 时,会生成一个错误。如果我从脚本中打印出生成的字符串,它似乎正是我试图创建的。

我将其归结为以下代码示例:

#!/bin/sh
declare foo="name"
declare bar="value"
declare invoke="mysql -p -D information_schema"
declare opts=" --init-command='SET @$foo:=\"$bar\"'"
invoke+=$opts
echo $invoke
$invoke

当我执行这个脚本时,结果如下:

$ example.sh
mysql -p -D information_schema --init-command='SET @name:="value"'
Enter password:
ERROR 1044 (42000): Access denied for user 'Bill'@'%' to database '@name:="value"''

这个错误信息似乎没有意义。

但是,如果我复制生成的命令,并将其粘贴回命令提示符,它会要求我输入密码,然后按照我的预期继续,如下所示:

$ mysql -p -D information_schema --init-command='SET @name:="value"'
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 171
Server version: 10.3.11-MariaDB Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [information_schema]> SELECT @name;
+-------+
| @name |
+-------+
| value |
+-------+
1 row in set (0.000 sec)

MariaDB [information_schema]>

证明--init-command选项中的SET命令已成功传递给MariaDB监视器,并被执行。

我不知道这是 Linux 问题、BASH 问题还是 MariaDB 问题。所以,虽然我花了很多时间试图找到答案,但我真的不知道问题出在哪里,因此我的研究重点在哪里。

请注意:我在示例中只使用了 information_schema 数据库,因为我希望任何尝试重新创建此问题的人都可以使用该数据库。

提前感谢您的帮助。

【问题讨论】:

  • 第一篇文章结构合理!
  • 请参阅stackoverflow.com/questions/8055694/… 了解如何从脚本中调用 mysql。您不能在脚本中与 mysql 进行一次交互(因此没有给出密码)。
  • 谢谢你,squareskittles。我受益于阅读许多 S.O.线程。所以,我尽量做到彻底、清晰,并遵循我过去读过的例子。

标签: mysql linux bash mariadb


【解决方案1】:

一些选项:

选项 1:

#!/bin/sh

# USING BASH
# FILE: bash_mariadb.sh

foo="\`name\`"
bar="value"

mysql -p -D information_schema --init-command="SET @$foo:='$bar';"

选项 2:

#!/bin/sh

# USING BASH
# FILE: bash_mariadb.sh

foo="\`name\`"
bar="value"

opts=(--init-command="SET @$foo:='$bar';")
invoke=(mysql -p -D information_schema "$opts")
"${invoke[@]}"

选项 3:

#!/bin/sh

# USING BASH
# FILE: bash_mariadb.sh

foo="\`name\`"
bar="value"

#define
invoke() {
  opts=(--init-command="SET @$foo:='$bar'")

  if [[ -v opts ]]; then
    invoke=(mysql -p -D information_schema "$opts")
  else
    invoke=(mysql -p -D information_schema)
  fi

  "${invoke[@]}"
}

#call
invoke

选项 4:危险,出于安全原因不推荐使用该选项。

#!/bin/sh

# USING BASH
# FILE: bash_mariadb.sh

foo="\`name\`"
bar="value"

invoke="mysql -p -D information_schema"
opts=" --init-command='SET @$foo:=\"$bar\"'"
invoke+=$opts
eval $invoke

在所有情况下:

$ ./bash_mariadb.sh
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 10.3.11-MariaDB Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [information_schema]> SELECT @`name`;
+---------+
| @`name` |
+---------+
| value   |
+---------+
1 row in set (0.000 sec)

【讨论】:

  • 谢谢大家的鼓励和解答。
  • 您的回答很有启发性,而且非常透彻。在这一点上,我已经让我的脚本开始工作了,使用“危险”的评估方法。我将继续努力,并尝试消除 eval。无论如何,你让我重回正轨,我真诚地感谢你。
【解决方案2】:

欢迎来到 SO。

您必须在脚本中指明要使用的密码。

-p 选项强制它以交互方式介绍密码,这在脚本中不起作用。

如果您改用-ppassword(请注意,您仍然写“p”,只是将它写在密码旁边,不带空格),您的连接将正常工作。

所以,你只需要修改这一行:

declare invoke="mysql -ppassword -D information_schema"

(当然,别忘了在我写“密码”的地方写下你的密码:))

【讨论】:

    猜你喜欢
    • 2021-03-11
    • 2012-07-23
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2014-06-05
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多