简体   繁体   English

使用MySQL的远程SSH命令不起作用,但该语句本身起作用

[英]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. 我想实现一些MySQL命令在远程服务器上运行,以从数据库中删除所有表。 Therefore I connect with SSH (SSH logs in using public key) and run a mysql command, all in a bash script: 因此,我使用bash脚本连接SSH(SSH使用公共密钥登录)并运行mysql命令:

#!/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. 当我通过SSH连接到远程服务器,登录到mysql并发出命令时,MySQL部分本身就成功了。 However, in this script, I get: 但是,在此脚本中,我得到:

ERROR 1064 (42000) at line 8: You have an error in your SQL syntax; 第8行的错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1 检查与您的MySQL服务器版本相对应的手册,以在第1行的'NULL'附近使用正确的语法

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. 我认为此错误与-p\\$(cat /etc/psa/.psa.shadow)的引用有关,但是我不知道如何解决此问题。

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 . 数据库中没有表,因此select不会得到结果, GROUP_CONCAT返回NULL,而DROP TABLENULL是... NULL

Preparing a statement using NULL as a statement will give your error. 使用NULL作为语句来准备一条语句将产生错误。

An SQLfiddle to show the problem . 一个SQLfiddle来显示问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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