在shellscript中执行多行mysql
当我尝试通过 shell 脚本在 mysql 中执行多行 SQL 时:
mysql -uroot -ppass mydb <<<EOF
SELECT * INTO OUTFILE 'table.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM mytable limit 1;
EOF
我收到语法错误:
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 'EOF' at line 1
编写脚本的正确方法是什么?
When I attempt to execute a multi-line SQL in mysql via shell script:
mysql -uroot -ppass mydb <<<EOF
SELECT * INTO OUTFILE 'table.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM mytable limit 1;
EOF
I get a syntax error:
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 'EOF' at line 1
What's the right way to script it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
bash heredoc 的语法是:
所以你有一个额外的
<。
为了准备和测试,你可以用
cat
替换COMMAND
(即本例中的mysql -uroot -ppass mydb
)来看看将执行的确切 SQL 代码。The syntax for bash heredoc is:
So you have an extra
<
.In order to prepare and test you can replace
COMMAND
(i.e.mysql -uroot -ppass mydb
in this case) withcat
to have a look at the exact SQL code that will be executed.对于未来的读者,如果他们希望使用 bash 批量导出,一种简单的方法如下,在 mariadb 上进行了测试,同样也应该在 mysql 中工作。
如果您是由非 root 用户导出,请设置如下权限
重新启动或重新加载 mysqld
示例代码片段
执行
For future readers, one easy way is as follows if they wish to export in bulk using bash, tested on mariadb, same should work in mysql too.
If you're exporting by non-root user then set permission like below
Restart or Reload mysqld
Sample code snippet
Execute