Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am attempting to execute a MySQL query on a remote server over ssh. Here is the command I have:

ssh -p 2020 mysql@mysql1.local.mydomain.com 'mysql --compress --secure-auth --database operations --execute \'INSERT INTO `ops`.`accounts` (`alias`, `id`, `web_server`, `mysql_server`) VALUES (\'thisisatest\', \'act_67vhVnS30bV\', \'web2\', \'mysql1\');\''

That should be right, but I am getting:

bash: syntax error near unexpected token `('

Any ideas?

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You can rewrite the command like:

ssh -p 2020 mysql@mysql1.local.mydomain.com "mysql --compress --secure-auth --database operations --execute 'INSERT INTO \`ops\`.\`accounts\` (\`alias\`, \`id\`, \`web_server\`, \`mysql_server\`) VALUES ('thisisatest', 'act_67vhVnS30bV', 'web2', 'mysql1');'"

Use double quotes for the complete SSH command. Single quotes don't require to be escaped. You need to escape the backticks \`.

share|improve this answer
    
since double-escaping can be quite troublesome but is occasionally necessary with MySQL statements run over SSH, I would suggest dumping the entire remote mysql --[...] command into a mysqlcommand file and just running ssh -p 2020 mysql@mysql1.local.mydomain.com < mysqlcommand. –  the-wabbit Apr 15 '13 at 8:58
    
Unfortunately, I can't dump the SQL into a file, since it is built up dynamically in a shell script. Unless, there is a way I am not aware of. –  Justin Apr 15 '13 at 9:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.