eye eye - 1 year ago 98
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 Source

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 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.


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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download