MySQL – SELECT * INTO OUTFILE LOCAL ?
MySQL 太棒了!我目前正在参与一个主要的服务器迁移,之前,我们的小型数据库曾经与客户端托管在同一台服务器上。
所以我们过去这样做:SELECT * INTO OUTFILE .... LOAD DATA INFILE ....
现在,我们将数据库移动到不同的服务器并SELECT * INTO OUTFILE ....
不再有效,可以理解 - 我相信是出于安全原因。 但是,有趣的是,LOAD DATA INFILE ....
可以更改为 LOAD DATA LOCAL INFILE ....
并且“bam”,它有效。
我并不是在抱怨,也不是表达对 MySQL 的厌恶。另一种方法是添加 2 行额外代码和一个系统调用,形成 .sql 脚本。我想知道的是为什么 LOAD DATA LOCAL INFILE 有效,为什么没有 SELECT INTO OUTFILE LOCAL 这样的东西?
我做了功课,找不到上述问题的直接答案。我也找不到@ MySQL 的功能请求。如果有人能解决这个问题,那就太棒了!
MariaDB有能力处理这个问题吗?
MySQL is awesome! I am currently involved in a major server migration and previously, our small database used to be hosted on the same server as the client.
So we used to do this : SELECT * INTO OUTFILE .... LOAD DATA INFILE ....
Now, we moved the database to a different server and SELECT * INTO OUTFILE ....
no longer works, understandable - security reasons I believe.
But, interestingly LOAD DATA INFILE ....
can be changed to LOAD DATA LOCAL INFILE ....
and bam, it works.
I am not complaining nor am I expressing disgust towards MySQL. The alternative to that added 2 lines of extra code and a system call form a .sql script. All I wanted to know is why LOAD DATA LOCAL INFILE
works and why is there no such thing as SELECT INTO OUTFILE LOCAL
?
I did my homework, couldn't find a direct answer to my questions above. I couldn't find a feature request @ MySQL either. If someone can clear that up, that had be awesome!
Is MariaDB capable of handling this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从手册中可以看出:
SELECT ... INTO OUTFILE
语句的主要目的是让您非常快速地将表转储到服务器计算机上的文本文件中。如果您想在服务器主机以外的某个客户端主机上创建结果文件,则不能使用SELECT ... INTO OUTFILE
。在这种情况下,您应该使用诸如 mysql -e "SELECT ..." > 之类的命令。 file_name 在客户端主机上生成文件。”http: //dev.mysql.com/doc/refman/5.0/en/select.html
示例:
From the manual:
The SELECT ... INTO OUTFILE
statement is intended primarily to let you very quickly dump a table to a text file on the server machine. If you want to create the resulting file on some client host other than the server host, you cannot useSELECT ... INTO OUTFILE
. In that case, you should instead use a command such asmysql -e "SELECT ..." > file_name
to generate the file on the client host."http://dev.mysql.com/doc/refman/5.0/en/select.html
An example:
您可以使用传入的 -s (--silent) 选项通过 mysql 控制台实现您想要的效果。
同时传入 -r (--raw) 选项可能是个好主意,这样特殊字符就不会被转义。您可以使用它来像您想要的那样管道查询。
mysql -u username -h hostname -p -s -r -e "select concat('this',' ','works')"
编辑:另外,如果您想从输出中删除列名,只需添加另一个-s(mysql -ss -r 等)
You can achieve what you want with the mysql console with the -s (--silent) option passed in.
It's probably a good idea to also pass in the -r (--raw) option so that special characters don't get escaped. You can use this to pipe queries like you're wanting.
mysql -u username -h hostname -p -s -r -e "select concat('this',' ','works')"
EDIT: Also, if you want to remove the column name from your output, just add another -s (mysql -ss -r etc.)
您为 LOAD DATA INFILE 提供的路径适用于运行服务器的计算机上的文件系统,而不是您连接的计算机上的文件系统。
LOAD DATA LOCAL INFILE
适用于客户端机器,但它要求服务器以正确的设置启动,否则不允许。您可以在这里阅读所有相关内容: http:// dev.mysql.com/doc/refman/5.0/en/load-data-local.html至于
SELECT INTO OUTFILE
我不确定为什么没有本地版本,除此之外,通过连接进行操作可能会很棘手。您可以通过 mysqldump 工具获得相同的功能,但不能通过将 SQL 发送到服务器来获得。The path you give to
LOAD DATA INFILE
is for the filesystem on the machine where the server is running, not the machine you connect from.LOAD DATA LOCAL INFILE
is for the client's machine, but it requires that the server was started with the right settings, otherwise it's not allowed. You can read all about it here: http://dev.mysql.com/doc/refman/5.0/en/load-data-local.htmlAs for
SELECT INTO OUTFILE
I'm not sure why there is not a local version, besides it probably being tricky to do over the connection. You can get the same functionality through themysqldump
tool, but not through sending SQL to the server.由于我发现自己经常寻找这个确切的问题(希望我之前错过了一些东西......),我最终决定花时间写一个 将 MySQL 查询导出为 CSV 文件的小要点,有点像 https://stackoverflow .com/a/28168869 但基于 PHP 并有更多选项。这对于我的用例很重要,因为我需要能够微调 CSV 参数(分隔符、NULL 值处理),并且文件需要是实际有效的 CSV,以便简单的
CONCAT
还不够,因为如果值包含换行符或 CSV 分隔符,它不会生成有效的 CSV 文件。注意:需要在服务器上安装 PHP!
(可以通过
php -v
检查)“安装”
mysql2csv
via(下载要点内容,检查校验和并使其可执行)
< strong>使用示例
生成文件
/tmp/result.csv
,其中内容参考帮助
Since I find myself rather regularly looking for this exact problem (in the hopes I missed something before...), I finally decided to take the time and write up a small gist to export MySQL queries as CSV files, kinda like https://stackoverflow.com/a/28168869 but based on PHP and with a couple of more options. This was important for my use case, because I need to be able to fine-tune the CSV parameters (delimiter, NULL value handling) AND the files need to be actually valid CSV, so that a simple
CONCAT
is not sufficient since it doesn't generate valid CSV files if the values contain line breaks or the CSV delimiter.Caution: Requires PHP to be installed on the server!
(Can be checked via
php -v
)"Install"
mysql2csv
via(download content of the gist, check checksum and make it executable)
Usage example
generates file
/tmp/result.csv
with contenthelp for reference
按照 Waverly360 的建议,使用带有 -e 选项的 mysql CLI 是一个不错的选择,但这可能会导致内存不足,并在结果较大时被杀死。 (还没有找到背后的原因)。
如果是这种情况,并且您需要所有记录,我的解决方案是:mysqldump + mysqldump2csv:
Using mysql CLI with -e option as Waverly360 suggests is a good one, but that might go out of memory and get killed on large results. (Havent find the reason behind it).
If that is the case, and you need all records, my solution is: mysqldump + mysqldump2csv:
尝试将路径设置为
/var/lib/mysql-files/filename.csv
(MySQL 8)。通过在 mysql 客户端命令行中键入SHOW VARIABLES LIKE "secure_file_priv";
确定哪个文件目录是您的。请参阅此处的答案:(...) MySQL 中的 --secure-file-priv 由 vhu 用户 于 2015 年回答
Try setting path to
/var/lib/mysql-files/filename.csv
(MySQL 8). Determine what files directory is yours by typpingSHOW VARIABLES LIKE "secure_file_priv";
in mysql client command line.See answer about here: (...) --secure-file-priv in MySQL answered in 2015 by vhu user
Re: SELECT * INTO OUTFILE
检查 MySQL 是否有权限将文件写入服务器上的 OUTFILE 目录。
Re: SELECT * INTO OUTFILE
Check if MySQL has permissions to write a file to the OUTFILE directory on the server.