1

In a simple script like this one:

set -x
# Check if db exists, if not we make it, make user, give privileges
if ! mysql -u root -p -e "use $db" 2>/dev/null; then

    c1="CREATE DATABASE $db"
    c2="GRANT ALL PRIVILEGES ON ${db}.* to '$username'@'localhost' IDENTIFIED BY '$password'"
    c3="FLUSH PRIVILEGES"

    mysql -u root -p -e "$c1; $c2; $c3"
else
    echo 'DATABASE ExISTS, ABORTING'; exit $DB_EXISTS
fi

I am asked each time, bash sees mysql command, for my root credentials. Is there a way to avoid that, so that once entered the root password, all additional mysql commands execute seamlessly?

1
  • 1
    You can ask for password, store it in a separate variable and pass it in the command as: mysql --password $pass_var Commented Oct 16, 2014 at 23:47

2 Answers 2

4

Try looking into adding password to ~/.my.cnf

[client]
user = root
password = XXXXXXXX

Check out :

How to execute a MySQL command from a shell script?

Sign up to request clarification or add additional context in comments.

1 Comment

There's also a very poorly-documented flag that allows you to specify a particular file: mysql --defaults-extra-file=/path/to/.my.cnf
0

Specifying the --password argument

mysql -u root --password=my_mysql_pass db_name

Safer using a bash variable

mysql -u root --password=$MYSQL_PASS db_name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.