Search code examples
mysqlshellunix

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.


Solution

  • 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`