0

I have a bash script that opens queries from MySQL. For example:

$ cat script.sh 
mysql -u joe -D database -p ........ < first_query.sql
mysql -u joe -D database -p ........ < second_query.sql

This 2 lines inside my script.sh file return the results of the queries, but: Everytime I call the ‘mysql’ command, if it has the «-p» option, it asks for password TWICE (because I call mysql twice). How can I avoid this behavior?

I have tried passing my password as parameter, for example:

$ sh script.sh ‘mypassword’ 

and storing it in a variable and putting it after the «-p» option. But it still asks for the password twice.

1
  • You have two valid answers. You should pick your favorite and mark this question as resolved. Commented Apr 29, 2019 at 18:03

2 Answers 2

2

You can place your password staight after the -p. If you password is "123asd", the command will be:

mysql -u user -p123asd

Make sure there are no spaces between -p and your password.

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

Comments

1

Probably the simplest way would be to edit your my.cnf (found in /etc/mysql/ by default) to include your password, like so:

[client]
user = joe
password = ......

Comments

Your Answer

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