如何让 MySQL 以不同的用户身份写入输出文件?
我正在使用写入输出文件的 MySQL 查询。 我每天或每两天运行一次此查询,因此我希望能够删除输出文件,而不必求助于 su 或 sudo。 我能想到实现这一点的唯一方法是将输出文件写为由 mysql 用户以外的其他人拥有。 这可能吗?
编辑:我没有将输出重定向到文件,我使用选择查询的 INTO OUTFILE 部分来输出到文件。
如果有帮助:
mysql --version mysql Ver 14.12 Distrib 5.0.32, for pc-linux-gnu (x86_64) using readline 5.2
I'm working with a MySQL query that writes into an outfile. I run this query once every day or two and so I want to be able to remove the outfile without having to resort to su or sudo. The only way I can think of making that happen is to have the outfile written as owned by someone other than the mysql user. Is this possible?
Edit: I am not redirecting output to a file, I am using the INTO OUTFILE part of a select query to output to a file.
If it helps:
mysql --version mysql Ver 14.12 Distrib 5.0.32, for pc-linux-gnu (x86_64) using readline 5.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
输出文件是由 mysqld 进程创建的,而不是由您的客户端进程创建的。 因此输出文件必须由 mysqld 进程的 uid 和 gid 拥有。
如果您从可以访问该文件的 uid 或 gid 下的进程访问该文件,则可以避免使用 sudo 来访问该文件。 换句话说,如果mysqld创建了uid和gid“mysql”/“mysql”拥有的文件,那么将自己的帐户添加到“mysql”组中。 然后您应该能够访问该文件,前提是该文件的权限模式包括组访问。
编辑:
您正在删除/tmp中的文件,目录权限模式为rwxrwxrwt。 粘滞位 ('t') 表示仅当您的 uid 与文件所有者相同时才可以删除文件,无论文件或目录的权限如何。
如果您将输出文件保存在另一个没有设置粘滞位的目录中,您应该能够正常删除该文件。
请阅读 Sticky(8) 手册页的摘录:
STICKY DIRECTORIES
设置了“粘性位”的目录将成为仅追加目录,或者更准确地说,是在其中删除文件受到限制。 仅当用户具有该目录的写权限并且该用户是该文件的所有者、该目录的所有者或超级用户时,该用户才可以删除或重命名粘性目录中的文件。 此功能可有效应用于 /tmp 等目录,该目录必须可公开写入,但应拒绝用户任意删除或重命名彼此文件的许可。
The output file is created by the mysqld process, not by your client process. Therefore the output file must be owned by the uid and gid of the mysqld process.
You can avoid having to sudo to access the file if you access it from a process under a uid or gid that can access the file. In other words, if mysqld creates files owned by uid and gid "mysql"/"mysql", then add your own account to group "mysql". Then you should be able to access the file, provided the file's permission mode includes group access.
Edit:
You are deleting a file in /tmp, with a directory permission mode of rwxrwxrwt. The sticky bit ('t') means you can remove files only if your uid is the same as the owner of the file, regardless of permissions on the file or the directory.
If you save your output file in another directory that doesn't have the sticky bit set, you should be able to remove the file normally.
Read this excerpt from the man page for sticky(8):
STICKY DIRECTORIES
A directory whose `sticky bit' is set becomes an append-only directory, or, more accurately, a directory in which the deletion of files is restricted. A file in a sticky directory may only be removed or renamed by a user if the user has write permission for the directory and the user is the owner of the file, the owner of the directory, or the super-user. This feature is usefully applied to directories such as /tmp which must be publicly writable but should deny users the license to arbitrarily delete or rename each others' files.
不使用“SELECT...INTO OUTFILE”语法,不。
您需要以另一个用户身份运行查询(即客户端),并重定向输出。 例如,编辑您的 crontab 以在需要时运行以下命令:
这将创建 /tmp/outfile.txt 作为您添加该命令的 crontab 用户。
Not using the "SELECT...INTO OUTFILE" syntax, no.
You need to run the query (ie client) as another user, and redirect the output. For example, edit your crontab to run the following command whenever you want:
That will create /tmp/outfile.txt as the user who's crontab you've added the command to.
我只需执行
添加
并
即可,我可以轻松地
SELECT INTO OUTFILE
任何文件名I just do
and add
and
And that's it, I can do easily
SELECT INTO OUTFILE
any filename如果您有另一个用户从 cron 运行查询,它将以该用户的身份创建该文件。
If you have another user run the query from cron, it will create the file as that user.