简体   繁体   English

Bash用户输入Mysql登录

[英]Bash user input Mysql login

Will start with what I am trying to accomplish. 从我要完成的事情开始。 I wrote up a menu script to add a new database and echo back to screen the results. 我写了一个菜单脚本来添加一个新的数据库并回显屏幕结果。 But can't seem to get it to login with a variable. 但是似乎无法使用变量登录。

Heres the part I am having problems with: 这是我遇到的问题:

#!/bin/bash
while [[ "$yn" != "Yes" && "$yn" != "Y" && "$yn" != "y" && "$yn" != "yes" ]]; do
    echo "  Please, type password for root user.             #"
read -r  mysqlrp
    echo "  You have entered $mysqlrp as your MySQL password #"
    echo "  Is this correct? (Yes or No)            #"
      read yn
done
mysql -u root -p$mysqlrp

have also tried: 也尝试过:

mysql -u root -p${mysqlrp}

as well as mysql -u root -p'${mysqlrp}' 以及mysql -u root -p'${mysqlrp}'

I get the following: 我得到以下内容:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

even though when I try without the script works fine. 即使我尝试不使用脚本也能正常工作。

Please help, Thanks in advance, Joe 请帮助,在此先感谢,乔

To supply a password directly in the command line string, you should use mysql --password=[password] . 要直接在命令行字符串中提供密码,您应该使用mysql --password=[password] See this article . 看到这篇文章

And to prompt the user for a password, you should probably use something like this. 为了提示用户输入密码,您可能应该使用类似这样的方式。

#!/bin/bash
read -p "Username: " uname
stty -echo
read -p "Password: " passw; echo
stty echo

You must not ever print the password. 您绝对不能打印密码。 And it should not be seen while typing it in either. 而且在任何一个中键入它时都不应看到它。


#!/bin/bash
read -p "Username: " uname
stty -echo
read -p "Password: " passw; echo
stty echo
mysql --user=$uname --password=$passw

That script works for me. 该脚本对我有用。 If it doesn't for you, please check that your mysql permissions allow you to login from localhost . 如果不适合您,请检查您的mysql权限是否允许您从localhost登录。

In a shellscript, you can do 在shellscript中,您可以执行

set -x

to see what the command being executed looks like. 看看正在执行的命令是什么样子。

You should prolly do mysql --password="$mysqlrp". 您应该正确地执行mysql --password =“ $ mysqlrp”。 '$mysqlrp' -> '$mysqlrp'. '$ mysqlrp'->'$ mysqlrp'

In your real script, I suppose you probably want to do 在您的真实脚本中,我想您可能想做

read -p "Password, plz:" -s mysqlrp

In order to properly answer the question, the actual error you get would be helpful. 为了正确回答该问题,您得到的实际错误会有所帮助。 [UPDATE: answered in comment.] [更新:在评论中回答。]

Also, note that if your query is longrunning, your password will be visible in "ps -ef" 另外,请注意,如果查询长时间运行,则密码将在“ ps -ef”中显示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM