Shell脚本:使用shell变量启动Mysql脚本

时间:2015-09-28 12:41:49

标签: mysql shell

我在从shell启动MySQL脚本时遇到问题。我使用filename为变量${x}赋值。所以我必须使用这个变量启动一个MySQL脚本。我想启动脚本而不在shell中插入所有MySQL代码(太长)但使用:

mysql -h localhost -uuser -ppsw DB < script.sql

我的尝试是:

mysql -h localhost -uuser -ppsw DB -e "set @x=${x}; source script.sql"

mysql -h localhost -uuser -ppsw DB -e "set @x=${x};"
mysql -h localhost -uuser -ppsw DB< script.sql

但不适合我。你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

我很惊讶您的第一个解决方案无效:

mysql -h localhost -uuser -ppsw DB -e "set @x=${x}; source script.sql"

分析

我认为将两种类型合并为一个语句可能存在问题,因为MySQL --execute=statement, -e statement应该execute the statement并且退出。

和退出部分是我尝试第一个想法时忽略重定向的stdin的原因:

mysql -h localhost -uuser -ppsw DB -e "set @x=${x};" < script.sql

解决方案

经过进一步的实验,我发现只需在source命令中添加分号就可以防止语法错误。 我不能说为什么这可行,因为通常不需要终止列表的最后一个SQL语句,但是你有它:

mysql -h localhost -uuser -ppsw DB -e "set @x=${x}; source script.sql;"

正如Glenn Jackman所指出的,如果shell变量是一个非数字字符串,那么shell变量必须用单引号括起来,以便在分配MySQL变量时,MySQL将处理右侧( shell变量)作为字符串文字而不是列名的标识符:

mysql -h localhost -uuser -ppsw DB -e "set @x='$x'; source script.sql;"

此版本也可以安全地使用数字字符串,如下面的示例所示。我也删除了shell变量周围的花括号,因为它们不是必需的。

实施例

t.sql的内容:

select now();
select @variable as 'Contents of variable';

使用数字字符串作为shell变量:

$ number=3
$ mysql -e "set @variable=$number; source t.sql;"
+---------------------+
| now()               |
+---------------------+
| 2015-10-02 13:06:45 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| 3                    |
+----------------------+

使用非数字字符串,因为shell变量会生成错误:

$ text=text
$ mysql -e "set @variable=$text; source t.sql;"
ERROR 1054 (42S22) at line 1: Unknown column 'text' in 'field list'

$ text="This is a string"
$ mysql -e "set @variable=$text; source t.sql;"
ERROR 1064 (42000) at line 1: 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 'a string' at line 1

现在将shell变量用单引号括起来:

$ mysql -e "set @variable='$text'; source t.sql;"
+---------------------+
| now()               |
+---------------------+
| 2015-10-02 13:08:04 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| This is a string     |
+----------------------+

$ text=text
$ mysql -e "set @variable='$text'; source t.sql;"
+---------------------+
| now()               |
+---------------------+
| 2015-10-02 13:10:53 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| text                 |
+----------------------+

$ mysql -e "set @variable='$number'; source t.sql;"
+---------------------+
| now()               |
+---------------------+
| 2015-10-02 13:11:42 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
| 3                    |
+----------------------+

使用不存在的shell变量会将MySQL变量设置为空字符串:

$ mysql -e "set @variable='$nonexistent'; source t.sql;"
+---------------------+
| now()               |
+---------------------+
| 2015-10-02 13:06:14 |
+---------------------+
+----------------------+
| Contents of variable |
+----------------------+
|                      |
+----------------------+
相关问题