eye eye - 3 months ago 16
MySQL Question

Protect the password when using Mysql in an unattended bash-script

I am writing a bash script (for a cron job) that uses mysql:

mysql -uusername -ppassword -e 'something;'


I am looking for a good way to keep the password handy for use in the script, but in a manner that will also keep this information secure from other users on that system. Users who could use ps -ef and users who might read text files...

So how can I safeguard passwords that will be used in an automated script on Linux?

Answer

Put all the settings in an option file. You can use your default ~/.my.cnf file, or you can specify an alternate file using --defaults-file==filename. See the documentation 4.2.3.4. Command-Line Options that Affect Option-File Handling

The option file contains default settings for mysql commands. You can put the following in it, for example.

[mysql]
user=username
password=password
database=yourdb

Make the option file readable only by you, so other users can't see your password.