Mysql,进入outfile的问题,一个简单的解决方案还是更好的方法?

发布于 2024-09-02 16:33:31 字数 1137 浏览 4 评论 0原文

目前我正在 ksh 中编写一个简单的脚本,该脚本应该从数据库中获取一些字符串并将它们列在 shell 上。它应该是这样工作的:

  • 查询数据库中的所有数据
  • 将它们导出到文本文件
  • 使用 awk 显示不同的列

问题是我正在查询的表的两个字段包含句子。所以 awk 显然“认为”不同单词之间的空格是列之间的分隔符,所以我想出了以下解决方案:

  • SELECT * INTO OUTFILE "output" FIELDS TERMINATED BY ',' FROM table
  • 使用 awk 列出结果(显然现在的分隔符是',')

但是这里我有另一个问题,尽管我已经

  • 向我正在使用的mysql用户授予了FILE权限,但
  • 在它应该写入的目录上给了mysql 777

即便如此我还是收到以下错误:

“路径”:无法创建/写入文件“文件”(错误代码:13)

对此感到沮丧,我开始上网寻找答案,但发现了一个“不完整”的答案。在某个站点中,他们建议:

  • chown root:root /tmp

  • chmod 1777 /tmp

  • /etc/init.d 。 d/mysqld start

我认为,如果这个解决方案在 /tmp 上运行良好,那么我选择的任何其他目录上都不应该有任何问题。显然我完全错了:)。如果我给 outfile 选项的路径是 /tmp/'file' 那么就没有问题,任何其他路径都会返回之前的错误。那么我找到问题的“解决方案”了吗?嗯,是的,但是要使用这个脚本,有人必须:

  • 拥有一个具有 FILE 权限的 mysql 用户
  • 有权“rm” /tmp 中的文件(mysql 的输出文件是临时的)

我不认为这是甜蜜又美好。那我问你什么呢?以下:

  • 如何强制 mysql 在我想要的任何地方写入文件?
  • 你有比我更可行和“甜蜜”的解决方案来建议我吗?

啊,我用的是 Ubuntu 10.4。 感谢您的帮助。

at the moment I'm writing a simple script in ksh who should take some strings from a DB and list them on the shell. This is how it should work:

  • Query the DB for all the datas
  • Export them to a text file
  • Using awk to show the different columns

The problem is that two fields of the table I'm querying contain sentences. So awk, obviously, "thinks" that the white spaces between the different words are separators between columns, so I came up with the following solution:

  • SELECT * INTO OUTFILE "output" FIELDS TERMINATED BY ',' FROM table
  • Using awk to list the results (obviously the separator now is ',')

But here I have another issue, though I've

  • Granted the FILE privilege to the mysql user I'm using
  • Gave to mysql 777 on the directory where it should write

Even so I'm receiving the following error:

'path': Can't create/write to file 'file' (Errcode: 13)

Frustrated by this I've started surfing the web to find an answer, and I found an "incomplete" one. In a certain site they suggest to:

  • chown root:root /tmp

  • chmod 1777 /tmp

  • /etc/init.d/mysqld start

I thought that, if this solution works fine on /tmp then there shouldn't be any problem on any other directory I choose. Obviously I was totally wrong :). If the path I'm giving to the outfile option is /tmp/'file' then there's no problem, any other path returns me the previous error. So did I find a 'solution' to the problem? Well, yes but to use this script a someone have to:

  • Have a mysql user with the FILE privilege
  • Have the rights to 'rm' a file in /tmp (the output file of mysql is temporary)

And I don't think this is sweet&nice. So what I'm asking you? The following:

  • How can I force mysql to write a file wherever I want?
  • Have you more viable and 'sweet' solution than mine to suggest me?

Ah, I'm using Ubuntu 10.4.
Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2024-09-09 16:33:31

更好的解决方案是重定向输出:

mysql --default-character-set=utf8 -uuser -ppass -s -e "SELECT * FROM mytable;" > results.txt

注意 -s 选项:

  -s, --silent        Be more silent. Print results with a tab as separator,
                      each row on new line.

A better solution is to redirect output:

mysql --default-character-set=utf8 -uuser -ppass -s -e "SELECT * FROM mytable;" > results.txt

Mind the -s option:

  -s, --silent        Be more silent. Print results with a tab as separator,
                      each row on new line.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文