HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

credentials file for running mysql related jobs via script?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
scriptfilerelatedcredentialsmysqlrunningjobsforvia

Problem

Is there a method to point mysql to a credentials file instead of entering account passwords directly in scripts?

Example:

mysqldump --opt --credfile /etc/mysql_creds mydatabase > output.sql


/etc/mysql_creds

username=myuser
password=ToPsEcReT

Solution

It would be better to store the credentials in a config file, and load them as an extra-defaults file:

$ mysqldump --defaults-extra-file=mycred.cnf ...


The config file is the same format as /etc/my.cnf or ~/.my.cnf

$ cat mycred.cnf
[mysqldump]
user = myuser
password = xyzzy


MySQL 5.6 also introduces encrypted storage of credentials. You can set up an encrypted file ~/.mylogin.cnf:

$ mysql_config_editor set --login-path=dump --host=localhost --user=root --password
Enter password:


Then you can dump using that login path

$ mysqldump --login-path=dump ...

Code Snippets

$ mysqldump --defaults-extra-file=mycred.cnf ...
$ cat mycred.cnf
[mysqldump]
user = myuser
password = xyzzy
$ mysql_config_editor set --login-path=dump --host=localhost --user=root --password
Enter password:
$ mysqldump --login-path=dump ...

Context

StackExchange Database Administrators Q#72417, answer score: 6

Revisions (0)

No revisions yet.