How to pass password from file to mysql command?

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

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

Bit does not work:

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

I tried different quotes without success. How to convey this?

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

+6
source share
6 answers

Finally, this line works:

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

or

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

The root password must be without quotes: bla bla bla

, :

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

mysql cnf:

install -m 700 -d /srv/secrets/
install -m 600 /dev/null /srv/secrets/root@localhost.cnf
editor /srv/secrets/root@localhost.cnf

client.password ini

[client]
password="password"

mysql:

mysql \
    --defaults-extra-file=/srv/secrets/root@localhost.cnf \
    --user=root \
    --host=localhost \
    --no-auto-rehash
+12

mysql_config_editor .

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

.mylogin.cnf .

mysql --login-path=name_of_connection dbname dbname server.example.com login_as_user

--login-path , --silent, --login-path /

+3

mysql_config_editor, mysql

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

, ,

mysql --login-path=dev
+1

Try:

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

I'm not sure if this is possible, but you can definitely use the file configuration .

-1
source

Source: https://habr.com/ru/post/1625260/


All Articles