如何使用 SELECT INTO OUTFILE 写入/tmp 以外的目录? [英] How to use SELECT INTO OUTFILE to write to a directory other than /tmp?

查看:94
本文介绍了如何使用 SELECT INTO OUTFILE 写入/tmp 以外的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SELECT INTO OUTFILE 将文件写入另一个目录中的自定义目录,该目录高于我的文档根目录.我可以写入/tmp,所以我知道其他一切都在工作.

I'm trying to use SELECT INTO OUTFILE to write a file to a custom directory inside another directory that is one level above my document root. I can write to /tmp so I know everything else is working.

我使用的是文件的绝对完整路径.我可以使用 PHP 创建目录和 chmod 到 0777.目录创建成功,模式设置正确(通过SSH和FTP查看).

I'm using the absolute full path to the file. I'm able to create the directory and chmod to 0777 with PHP. The directory is successfully created and the mode is properly set (as viewed via SSH and FTP).

我想使用的路径示例:

/var/www/vhosts/mysite/private_www/data_export/newdir/

我原以为只要目录是世界可读/可写/可执行的,MySQL就可以写入它.但我仍然收到此错误.

I had thought that as long as the directory was world readable/writable/executable, that MySQL would be able to write to it. But I am still getting this error.

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

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

我发现了很多关于此错误的帖子,但除了写入/tmp 之外没有其他解决方案.但是,如果我写入/tmp,则必须添加更多代码来处理文件名,然后将文件复制到我想要的位置.

I've found lots of posts regarding this error, but no solutions other than write to /tmp. However, if I write to /tmp, I have to add more code to handle filenames and then copy the file to where I want it.

我认为我可能需要一个与 PHP 不同的 MySQL 路径.有没有办法检查 MySQL 将什么视为服务器根目录?但是,我不确定这可能是问题所在,因为我可以使用相同的路径 LOAD DATA INFILE.

I considered that maybe I needed a different path for MySQL than for PHP. Is there a way to check what MySQL sees as the server root? However, I'm not sure that could be the issue since I am able to LOAD DATA INFILE using the same path.

我的设置

  • CentOS 6
  • PHP 5.3
  • MySQL 5.5
  • 带有 Plesk 控制面板的专用服务器
  • PHP 作为 Apache 模块运行

我错过了什么?

推荐答案

虽然您已经正确设置了目标目录的可写权限,但为了让用户遍历目录树进入该目标目录,您必须提供执行权限该用户位于每个 更高的目录中,进入您的目标目录.这是因为在目录上下文中,执行实际上意味着查看内部以列出内容.

Although you had correctly set writable permissions on the target directory, in order for a user to traverse the directory tree into that target directory, you must supply an execute permission for that user on every higher directory leading into your target directory. This is owing to the fact that in a directory context, execute actually means look inside to list contents.

这意味着路径中每个目录的最小(忽略所有者/组权限)

This means that the minimum (ignoring the owner/group permissions) for each of the directories in the path

/var/www/vhosts/mysite/private_www/data_export

应该是 0751,其中最后的 1 是其他"用户类别的执行权限.因此,请验证目标路径中的每个目录都至少具有 MySQL 服务器用户的执行权限(在 CentOS 系统中可能是 mysql).

would be 0751, where the final 1 is an execute permission for the "other" user category. So, verify that every directory in your target path has at least execute permissions for the MySQL server user (probably mysql in a CentOS system).

允许在所有这些目录上执行 (list-contents) 确实意味着任何系统用户都可以检查它们,而不仅仅是 MySQL 用户.在决定最终路径的位置时,请考虑这一点.如果可以,您可能希望将其与 www 文件分开.

Permitting execute (list-contents) on all those directories does mean that any system user could inspect them, not just the MySQL user. Consider this as you decide where the final path will be. You might want to separate it from your www files if you can.

在您的设置中,您已将目标目录设置为 0777,全球可读,全球可写.如果可以避免,这是不可取的.由于所有真正需要 MySQL 能够写入它,考虑改为将其组所有权更改为 mysql 组.这将使您能够避免使其成为可写的.

In your setup you have set the target directory as 0777, world-readable, world-writable. That's not advisable if it can be avoided. Since all that's really necessary there is for MySQL to be able to write to it, consider instead changing its group ownership to the mysql group. That will enable you to avoid making it world writable.

# Give group ownership to mysql
chown :mysql /var/www/vhosts/mysite/private_www/data_export/newdir
# And just make it writable by the group, not everyone
chmod 0770 /var/www/vhosts/mysite/private_www/data_export/newdir

这篇关于如何使用 SELECT INTO OUTFILE 写入/tmp 以外的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆