如何让 MySQL 以不同的用户身份写入输出文件?

发布于 2024-07-07 10:15:35 字数 317 浏览 12 评论 0原文

我正在使用写入输出文件的 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 技术交流群。

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

发布评论

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

评论(4

天煞孤星 2024-07-14 10:15:35

输出文件是由 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.

公布 2024-07-14 10:15:35

不使用“SELECT...INTO OUTFILE”语法,不。

您需要以另一个用户身份运行查询(即客户端),并重定向输出。 例如,编辑您的 crontab 以在需要时运行以下命令:

mysql db_schema -e 'SELECT col,... FROM table' > /tmp/outfile.txt

这将创建 /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:

mysql db_schema -e 'SELECT col,... FROM table' > /tmp/outfile.txt

That will create /tmp/outfile.txt as the user who's crontab you've added the command to.

裂开嘴轻声笑有多痛 2024-07-14 10:15:35

我只需执行

sudo gedit /etc/apparmor.d/usr.sbin.mysqld

添加

 /var/www/codeigniter/assets/download/* w,

sudo service mysql restart

即可,我可以轻松地SELECT INTO OUTFILE任何文件名

I just do

sudo gedit /etc/apparmor.d/usr.sbin.mysqld

and add

 /var/www/codeigniter/assets/download/* w,

and

sudo service mysql restart

And that's it, I can do easily SELECT INTO OUTFILE any filename

丑疤怪 2024-07-14 10:15:35

如果您有另一个用户从 cron 运行查询,它将以该用户的身份创建该文件。

If you have another user run the query from cron, it will create the file as that user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文