5

I have about 20 different MySQL instances that I'd like to easily connect to without having to input the whole address, username, and crazy long password each time. What can I do to script this process, so all I have to do is run one .sh script for each instance?

What I have so far, saved as "instance1.sh"

#!/bin/bash
mysql -h instance1-address.com -u username -p password -e "show databases"

After making this file executable, it still asks for my password, even though it is in the script itself. What can I do?

3
  • 1
    You can put default options in your .my.cnf file. Commented Mar 17, 2017 at 19:38
  • 1
    Our preferred method is to put the options into a file and reference the file with defaults-extra-file dev.mysql.com/doc/refman/5.7/en/option-files.html. (The prompt for a password is happening because mysql isn't seeing "password" as the supplied password, and that's because of the space that follows -p. Remove the space e.g. -pmysecret and MySQL will take "mysecret" to be the password. Avoid the warning by using an options file instead of supplying the password on the command line. ) Commented Mar 17, 2017 at 19:45
  • putting the username and pwd in a config file, then referring to that in the .sh still forces me to enter the password: my.cnf: [client] username="usr" password="mysecret" .sh: #!/bin/bash mysql -h instancehere.com -u$USERNAME -p$PASSWORD Commented Mar 17, 2017 at 20:29

1 Answer 1

12

remove the space after -p so like this:

#!/bin/bash
mysql -h instance1-address.com -u username -ppassword -e "show databases"

it will still show a warning about it being insecure

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

1 Comment

Do you need to 'disconnect' from mysql server inside such script?

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.