I have a bash script that logs into MySQL and creates a database. It has a sequence that requests the MySQL credentials (username and password) and then executes
read username
read password
read databasename
mysql -u$username -p$password -e "CREATE DATABASE $databasename;"
whenever I run this script, I get the warning "Using a password on the command line interface can be insecure"
What is the right, secure way to create a script like this where you need to log into MySQL and then run a command?
nobody
, can read command lines; only the same account and root can read environment variables). – Charles Duffy-u"$username" -p"$password"
; otherwise, an account with a password having spaces in it (or characters in the current value ofIFS
) is liable to misbehave. For similar reasons, it's better to useIFS= read -r password
, so leading and trailing spaces and backslashes aren't discarded. – Charles DuffyMYSQL_PWD
; the documentation warns that this is insecure, but that's not the case when targeting modern Linux -- anyone who can read your environment variables can also read your credential files). – Charles Duffy