mysqlbashsu

How to run mysql query as other user


I have a mysql command that I am able to execute as my user testuser. Root does not have access to the db.

I'm creating a startup script which runs as root, but I need to run the query from above as testuser during this script. I tried this:

sudo -u testuser "mysql -D keystone -e "UPDATE endpoint SET url=REPLACE(url, '$OLD_IP', '$HOST_IP');""

But I get the error:

bash: syntax error near unexpected token `('

Does anyone know where my syntax is off?

Solution was no quotes around entire mysql command:

sudo -u testuser mysql -D keystone -e "UPDATE endpoint SET url=REPLACE(url, '$OLD_IP', '$HOST_IP');"

Solution

  • The second " is closing the string that begins at mysql, try quoting the inner quoted string:

    sudo -u testuser "mysql -D keystone -e \"UPDATE endpoint SET url=REPLACE(url, '$OLD_IP', '$HOST_IP');\""