0

I'm trying to run a mysql query on a remote host using a bash script.

${MYSQL} -u ${USER} -p${PASS} -P${PORT} -h ${HOST} -e "select * from information_schema;"

My PASS looks something like "dfsf#DFD". It conatins '#' character. For some reasons the PASS is not retrieving correctly in the script. Its getting chopped off after '#'.

And for some reason including source /etc/environment in the script, seems to fix the problem. I want to understand why is this happening. How to make it work without the source statement.

  • add spaces for a start e.g. `-p ${PASS}` like you have done for -u have you tried wrapping the whole string in quotes and seeing what's being output? – bob dylan Aug 07 '20 at 08:19
  • You need double quotes around your variable expansion like: `"$MYSQL" -u "$USER" -p "$PASS" -P "$PORT" -h "$HOST" -e 'select * from information_schema;'` – Léa Gris Aug 07 '20 at 08:19
  • @LéaGris any idea why the problem is getting fixed after using `source /etc/environment' in the script? – Nikhil Singh Aug 07 '20 at 08:29
  • Are you setting $USER $PASS etc in your local session e.g. not exporting /setting in the script? – bob dylan Aug 07 '20 at 08:44
  • @bobdylan i have declared those variables in `/etc/environment' – Nikhil Singh Aug 07 '20 at 08:57
  • @NikhilSingh: You set the userid/password for your database in a world-readable file? Seriously? – Ljm Dullaart Aug 07 '20 at 09:13
  • apparently the actual issue is this: https://stackoverflow.com/questions/63300003/environment-variable-string-getting-chopped-off-because-of-special-character Not related to mysql. – Nikhil Singh Aug 07 '20 at 10:45

1 Answers1

0

As far as I know - the safest way to specify remote connection details for MySQL is using configuration file: https://dev.mysql.com/doc/refman/8.0/en/option-files.html So briefly you just put something like this in ~/.my.cnf file:

[client]
host='...'
port='...'
user='...'
password='...'

And then you simply run

mysql -e "select * from information_schema;"

Not sure it suits your situation, but it should solve your issue, if you quote the values. Quoting at command line is also a solution, but your credentials will be visible to everyone able to see your processes at the system where you run that.

  • Apparently, the actual issue is this: https://stackoverflow.com/questions/63300003/environment-variable-string-getting-chopped-off-because-of-special-character I agree its not the safest way. But I'm just trying things out right now. Will definitely use the advice. – Nikhil Singh Aug 07 '20 at 10:42