简体   繁体   中英

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}'

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] . 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 .

In a shellscript, you can do

set -x

to see what the command being executed looks like.

You should prolly do mysql --password="$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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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