How to pass password from file to mysql command?

Store your password in a protected mysql cnf file:

install -m 700 -d /srv/secrets/
install -m 600 /dev/null /srv/secrets/[email protected]
editor /srv/secrets/[email protected]

Store the password in the client.password ini property

[client]
password="password"

Include this file as the first argument in your mysql command:

mysql \
    --defaults-extra-file=/srv/secrets/[email protected] \
    --user=root \
    --host=localhost \
    --no-auto-rehash

You should use the mysql_config_editor for this.

$ mysql_config_editor set \
    --login-path=name_of_connection \
    --host=server.example.com \
    --user=login_as_user \
    --password

this will prompt for a password, and save to a .mylogin.cnf file in your homedir.

mysql --login-path=name_of_connection dbname will connect to dbname on server.example.com as login_as_user

If --login-path is used with other options, ex. --silent, --login-path has to be the first argument/option


Use mysql_config_editor which is installed with the mysql client

mysql_config_editor set --login-path=dev --user=dbuser --host=localhost -p

Enter the password and then you can log in like this

mysql --login-path=dev

Tags:

Mysql

Unix

Shell