Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass login parameters to mysql

Tags:

shell

unix

mysql

I have a shell script which calls the mysql command line client, it looks like this:

 $ cat ms
 mysql --host=titanic --user=fred --password="foobar"

It works OK:

$ ./ms 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 810
...

Now, I'd like to keep the script in a git repository, but without the user and password details. So, I thought I would have a file SECRET with: --host=titanic --user=fred --password="foobar" which I wouldn't add to the git repository, and change the ms script like this:

mysql $(cat SECRET)

Unfortunately, it doesn't work. I get this error when I run it:

$ ./ms
ERROR 1045 (28000): Access denied for user 'fred'@'example.com' (using password: YES)

I cannot understand it - when the $(cat SECRET) is evaluated/expanded it looks exactly the same as the straightforward invocation of mysql. Still, it doesn't work. The same happens if I try to do it directly in interactive shell:

$ mysql --host=titanic --user=fred --password="foobar"

works OK, but the below does not:

$ cat SECRET
--host=titanic --user=fred --password="foobar"
$ mysql $(cat SECRET)
ERROR 1045 (28000): Access denied for user 'fred'@'example.com' (using password: YES)
$ echo mysql $(cat SECRET)
mysql --host=titanic --user=fred --password="foobar"

Anybody can shed some light on what's going on here and how to fix it? Many thanks in advance.

like image 826
piokuc Avatar asked Jun 12 '13 16:06

piokuc


1 Answers

Change the file to:

--host=titanic --user=fred --password=foobar

Quotes aren't processed on the result of command or variable substitution, only word splitting and filename expansion are done.

But a better solution would probably be to use an option file, e.g. mysecret.cnf:

[mysql]
user=fred
password=foobar
host=titanic

Then run mysql as:

mysql --defaults-file=mysecret.cnf
like image 84
Barmar Avatar answered Oct 17 '22 05:10

Barmar