[英]MariaDB --init-command when invoking mysql from a BASH script
我是 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 数据库,因为我希望任何尝试重新创建此问题的人都可以使用该数据库。
预先感谢您的协助。
欢迎来到 SO。
您必须从脚本中指明要使用的密码。
选项-p
强制它用于密码的交互式介绍,这不适用于脚本。
如果您改为使用-ppassword
(请注意,您仍然写“p”,只是将它写在密码旁边,不带空格),您的连接将正常工作。
所以,你只需要修改这一行:
declare invoke="mysql -ppassword -D information_schema"
(当然,别忘了在我写“密码”的地方写下你的密码:))
一些选项:
选项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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.