What is a good practice to execute root operations on mysql databases in bash scripts?
For example, I want to create new database and grant privileges, I have to use:
mysql -u $ROOT -p"$ROOTPASS" -e "create database $DBNAME; GRANT ALL PRIVILEGES ON $DBNAME.* T $DBUSER@localhost IDENTIFIED BY $DBPASS";
But this requires storing mysql password for root user in the script itself.
Is there any more secure way?
Should I create separate user for this? How to set up the permissions?
The separate thing is how to test if the operation was successful, and if not, revert back the changes which were successfull (transaction?)
Like many programs the MySQL (client) can and will read credentials from file instead of relying on commandline options or reading credentials from STDIN.
The recommended method is to store your credentials encrypted with the MySQL config Editor in
~/.mylogin.cnf
Check the manual for the exact location, but alternatively you set up a file
~/.my.cnf
which is only readable by your user ans store your cleartext password(s) there:Additionally see the manual section on password security