I have a bash script that runs a bunch of MySQL commands, separated by user input.
Each time a query is run, the password must be re-entered.
I understand it's best to avoid putting the password on the commandline, so I am wary of asking the user for the password and then passing it in on the commandline to each command.
Is there a recommended way to do this without compromising security?
You can store the credentials in
~/.my.cnf file
. Mysql will look up this file by default and use the credentials specified in it, if no other credentials are specified in the command.It's best not to handle password in shell scripts.
I had a similar question:
https://security.stackexchange.com/questions/182927/what-would-be-a-secure-way-to-handle-password-prompts-in-shell
I made the decision use a shell script as a wrapper to an expect script - based on my use case.
It's not perfectly secure (what is?) but:
Is it 'secure'? It's good enough security for my situation.
Plenty of docs on how to run shell script w/an eye for security. Here's one:
https://developer.apple.com/library/content/documentation/OpenSource/Conceptual/ShellScripting/ShellScriptSecurity/ShellScriptSecurity.html
Also consider: https://security.stackexchange.com/questions/66832/are-shell-scripts-bash-inherently-less-secure-than-other-script-languages-su
I don't have a MySQL server to test with, but you should be able to
mysql
process, andInstead of having MySQL -u root -pPASSWORD have this: `mysql —defaults-file=/etc/mysql/debian.cnf that will use the debian sys maint user
You could store the user input (the password) on a variable and pass it as a param to the MySQL CLI. That's not bad. It doesn't compromise security.
As a user, when you are typing commands, they get stired in your
~/.bash_history
in plain text. So, as a user, if you pass the password as a param, it gets stored in a file on the machine. This isn't any worse than storing the password in a config file.Making the user type the password over and over is annoying from a UX perspective. Saving it to a variable and passing it into the command is much better without compromising security.
It is worth noting that this would expose the password in the process list while each one of the MySQL commands is running.