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

标签 mysql linux bash shell variables

我将密码存储在变量 $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/

相关文章:

php - Crontab 无法正常运行

php - Codeigniter 的 form_dropdown 在下拉列表中显示类别名称,但插入类别 id

php - Laravel 查询生成器 'where' 方法已损坏

python - 如何在 SQLAlchemy ORM 中选择带有 "as"的查询并连接表

linux - 在父进程 : SIGCHLD caught by ps 中执行

bash - RETVAL 是什么意思?

mysql - 将数据插入通过连接表连接的 2 个表中

Linux bash 脚本卡在 sleep 状态

linux - 如何仅显示 wget 进度条?

python - 无法使用 python3 从 apache2 服务器运行 bash 脚本?