Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

ssh -p 2020 '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
up vote 3 down vote accepted

You can rewrite the command like:

ssh -p 2020 "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 < 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

Your Answer


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.