SSH를 통해 원격 MySQL 쿼리 실행

SSH를 통해 원격 MySQL 쿼리 실행

다음 명령을 사용하여 MySQL쿼리를 실행하려고 합니다 .ssh

ssh -p 2020 [email protected] "mysql --verbose --compress --secure-auth --database ops --execute \
'INSERT INTO \`ops\`.\`accounts\` (\`alias\`, \`id\`, \`web_server\`, \`mysql_server\`) VALUES ('foobar', 'foobar', 'web2', 'mysql1')'"

문제는 이것이 실행될 때 , , 및 주위의 작은따옴표가 foobar제거 foobar된다는 web2mysql1입니다. 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)
--------------

이 문제를 어떻게 해결할 수 있나요? 감사해요.

답변1

솔루션에는 값 주위에 이스케이프된 작은따옴표의 또 다른 레이어가 추가되었습니다.

ssh -p 2020 [email protected] "mysql --verbose --compress --secure-auth --database ops --execute \
'INSERT INTO \`ops\`.\`accounts\` (\`alias\`, \`id\`, \`web_server\`, \`mysql_server\`) VALUES ('\'foobar\'', '\'foobar\'', '\'web2\'', '\'mysql1\'')'"

답변2

다음이 작동합니다.

echo "INSERT INTO `ops`.`accounts` (`alias`, `id`, `web_server`, `mysql_server`) VALUES ('foobar', 'foobar', 'web2', 'mysql1')" | ssh [email protected] "mysql --verbose --compress --secure-auth --database ops"

로컬 포트를 원격 서버로 전달할 수도 있습니다.

$ ssh -L2206:localhost:3306 [email protected]

그런 다음 다른 터미널에서 다음 명령을 사용하여 원격 MySQL 서버에 연결할 수 있습니다 mysql.

$ mysql --host localhost --port 2206

관련 정보