gpt4 book ai didi

mysql - 在 shell 中使用变量将密码传递给 mysql_config_editor

转载 作者:IT王子 更新时间:2023-10-29 00:31:34 24 4
gpt4 key购买 nike

我将密码存储在变量 $db_pwd 中,我想将它传递给 shell 脚本中的 mysql_config_editor。我不能使用配置文件或 db_pwd 环境变量。

我在做这个

mysql_config_editor set --login-path=local --host=localhost --user=username --password

(https://stackoverflow.com/a/20854048/6487831)。

它的作用是要求输入密码“Enter Password”,但我希望使用变量提供密码。

我试过这个:

mysql_config_editor set --login-path=local --host=localhost --user=username --password $db_pwd

mysql_config_editor set --login-path=local --host=localhost --user=username --password | echo $db_pwd

echo "$db_pwd" | mysql_config_editor set --login-path=local --host=localhost --user=username --password

  • 期待。但这会导致错误,以防出现“此路径已存在,重写 (y/n)”之类的警告。
  • options 文件,但即使我将它用作第一个参数,它们仍然给我 variable not found 错误。选项文件是否与 mysql 配置编辑器兼容?

有什么办法吗?或者我应该恢复使用 mysql 而不是 mysql_config_editor?

最佳答案

我发现当没有 TTY 时,其他建议的答案不起作用。所以我使用了这个 bash 脚本,它可以在 Terraform/ssh 等没有终端的地方工作:

#!/bin/bash

if [ $# -ne 4 ]; then
echo "Incorrect number of input arguments: $0 $*"
echo "Usage: $0 <login> <host> <username> <password>"
echo "Example: $0 test 10.1.2.3 myuser mypassword"
exit 1
fi

login=$1
host=$2
user=$3
pass=$4

unbuffer expect -c "
spawn mysql_config_editor set --login-path=$login --host=$host --user=$user --password
expect -nocase \"Enter password:\" {send \"$pass\r\"; interact}
"

测试它:

./mysql_config.sh login 10.1.2.3 myuser mypass < /dev/null

关于mysql - 在 shell 中使用变量将密码传递给 mysql_config_editor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42232310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com