简体   繁体   中英

Remote SSH command with MySQL won't work, but the statement itself does

I want to achieve that some MySQL commands are run on a remote server to remove all tables from a database. Therefore I connect with SSH (SSH logs in using public key) and run a mysql command, all in a bash script:

#!/bin/bash
ssh user@example.com "mysql -uadmin -p\$(cat /etc/psa/.psa.shadow) exdb<<EOFMYSQL
SET FOREIGN_KEY_CHECKS = 0;
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
  FROM information_schema.tables 
  WHERE table_schema = 'exdb';

SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;
EOFMYSQL"

The MySQL part itself succeeds, when I connect via SSH to my remote server, log into mysql and issue the commands. However, in this script, I get:

ERROR 1064 (42000) at line 8: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1

I think this error has to do with the quoting of -p\\$(cat /etc/psa/.psa.shadow) , however I could not figure out how to resolve this problem.

I'd be glad if someone could point me where the problem is.

You have no tables in the database, so you're getting no result from the select and the GROUP_CONCAT returns NULL, and the concat of DROP TABLE with NULL is... NULL .

Preparing a statement using NULL as a statement will give your error.

An SQLfiddle to show the problem .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM