0

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?

branquito
  • 3,864
  • 5
  • 35
  • 60
  • 1
    You can ask for password, store it in a separate variable and pass it in the command as: `mysql --password $pass_var` – hjpotter92 Oct 16 '14 at 23:47

2 Answers2

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?

Community
  • 1
  • 1
lvolmar
  • 2,022
  • 1
  • 12
  • 8
  • There's also a very poorly-documented flag that allows you to specify a particular file: `mysql --defaults-extra-file=/path/to/.my.cnf` – Sammitch Oct 17 '14 at 00:53
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