簡體   English   中英

MariaDB --init-command 從 BASH 腳本調用 mysql 時

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM