Ssh – Executing a remote MySQL query over SSH

bashMySQLssh

I am trying to execute a MySQL query over ssh using the following command:

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

The problem is that when this is executed, the single quotes around foobar, foobar, web2, and mysql1 are removed. Here is the error response from MySQL:

ERROR 1054 (42S22) at line 1: Unknown column 'foobar' in 'field list'
--------------
INSERT INTO `ops`.`accounts` (`alias`, `id`, `web_server`, `mysql_server`) VALUES (foobar, foobar, web2, mysql1)
--------------

How can I fix this? Thanks.

Best Answer

The solution was added another layer of escaped single quotes around the values.

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