【问题标题】:Bash script calling mysql after ssh returns access denied在 ssh 返回访问被拒绝后调用 mysql 的 Bash 脚本
【发布时间】:2018-09-25 02:30:50
【问题描述】:

我在编写 bash 脚本时遇到了一点问题,我可以在终端中运行各个命令,它工作得很好,但是从 bash 脚本运行会返回错误;

mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

bash脚本如下;

#!/bin/bash
ssh user@myhost.com << EOF
    /usr/bin/mysql -h localhost -u root -p_kHaTX(!G_$=Y5Xa
    show databases;
EOF

我也尝试将密码包装在 '' 中

/usr/bin/mysql -h localhost -u root -p'_kHaTX(!G_$=Y5Xa'

还是什么都没有

但如果我自己通过终端运行命令,一切都会正常

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 这也可能有帮助:echo 'show databases;' | /usr/bin/mysql -h localhost -u root '-p_kHaTX(!G_$=Y5Xa'
  • @Cyrus ... 仍然返回相同的错误
  • 用这个命令测试:mysql -u root -h 127.0.0.1 --password=YOUR_PASS
  • @JJJ 如果我在终端中运行它,我会得到 -bash: !: event not found 如果我从我的脚本运行它,我会得到 -bash: line 1: syntax error near unexpected token ` ('

标签: linux bash stdin heredoc


【解决方案1】:

为了解决错误并出于安全考虑,请将密码存储在单独的文件中,例如:

# echo '_kHaTX(!G_$=Y5Xa' > /root/mysql_pwd

然后将密码作为变量传递:

# MYSQL_PWD=`cat /root/mysql_pwd` /usr/bin/mysql -u root -p -e "show databases"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-06
    • 2017-08-09
    • 1970-01-01
    • 2011-07-06
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多