SebiF
SebiF

Reputation: 509

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 [email protected] "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.

Upvotes: 0

Views: 314

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180987

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.

Upvotes: 1

Related Questions