使用shell中的变量将密码传递给mysql_config_editor [英] Pass password to mysql_config_editor using variable in shell

查看:402
本文介绍了使用shell中的变量将密码传递给mysql_config_editor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将密码存储在变量$ db_pwd中,并希望在shell脚本中将其传递给mysql_config_editor.我不能使用配置文件或db_pwd环境变量.

I have password stored in a variable $db_pwd and I want to pass it to mysql_config_editor in a shell script. I can not use config file or db_pwd environment variable.

我正在这样做

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

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

它的作用是要求输入密码"Enter Password",但是我希望使用变量来提供密码.

What it does is ask for password "Enter Password", but I wish to supply the password using variable.

我尝试过:

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

  • expect.但是,如果出现诸如此路径已存在,请重写(y/n)."之类的警告,则会导致错误.
  • 选项文件,但是即使我将其用作第一个参数,它们仍然会给我变量未找到的错误.选项文件与mysql配置编辑器兼容吗?
  • expect. But this leads to error in case there are warnings like "This path already exists, rewrite (y/n).
  • options file, but they still give me variable not found error even when I am using it as the first argument. Is options file compatible with mysql config editor?

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

Any way to do this? Or should I revert to using mysql instead of mysql_config_editor?

推荐答案

我发现在没有TTY的情况下,其他建议的答案也不起作用.因此,我使用了这个bash脚本,该脚本可以在没有终端的Terraform/ssh之类的地方工作:

I found the other suggested answer did not work when there was no TTY. So I used this bash script that works in places like Terraform/ssh that doesn't have a terminal:

#!/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

这篇关于使用shell中的变量将密码传递给mysql_config_editor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆