Sogl
Sogl

Reputation: 822

How to pass password from file to mysql command?

I have a shell script which calls the mysql command with one parameter from external file, it looks like this (also I saw this example in other resources):

mysql --user=root --password=`cat /root/.mysql`

Bit it not working:

Failed to connect to MySQL server: Access denied for user 'root'@'localhost' (using password: YES).

I tried different quotes without success. How to pass it?

UPDATE 1: Found that I can pass password without space symbol. The problem in this, my root pass contains spaces.

Upvotes: 15

Views: 33952

Answers (7)

user3183111
user3183111

Reputation: 321

The other way to do it is with an options file. Here is how I connect in perl

my $dsn = 'DBI:mysql:' .
          ';mysql_read_default_group=local' .
          ';mysql_read_default_file=/etc/.mysqloptions';

   cat /etc/.mysqloptions 
    [local]
    host            =       localhost
    database        =       test
    user            =       blah
    password        =       blah

Upvotes: 0

user3183111
user3183111

Reputation: 321

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

Upvotes: 15

Mogens TrasherDK
Mogens TrasherDK

Reputation: 680

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

Upvotes: 10

ThorSummoner
ThorSummoner

Reputation: 18099

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

Upvotes: 28

Sogl
Sogl

Reputation: 822

Finally this line working:

 mysql --user=root --password="$(cat /root/.mysql)"

or:

mysql --user=root --password="$(< /root/.mysql)"

Root password must be without quotes: bla bla bla

If your password not contains spaces you can use:

mysql --user=root --password=`cat /root/.mysql`

Upvotes: 6

Jayesh Dhandha
Jayesh Dhandha

Reputation: 2119

Try:

if [ $MYSQL_PASS ]
then
  mysql -u "$MYSQL_ROOT" -p "$MYSQL_PASS" -e "SHOW DATABASES"
else
  mysql -u "$MYSQL_ROOT" -e "SHOW DATABASES"
fi

Upvotes: -2

makallio85
makallio85

Reputation: 1356

I am not sure if that is possible, but you could definitely use configuration file.

Upvotes: -2

Related Questions