MySQL 会话/用户的文件权限

发布于 2024-12-02 11:56:30 字数 274 浏览 1 评论 0原文

当有人在我的 MySQL 数据库中注册时,此功能必须起作用:

mysql_query("SELECT mail 
               FROM users 
               INTO OUTFILE 'test.txt'");

但我收到错误

用户“registerdb”@“%”的访问被拒绝(使用密码:YES)

那么我如何向会话/用户授予文件写入权限?

When someone is registerd in my MySQL database, this function must work:

mysql_query("SELECT mail 
               FROM users 
               INTO OUTFILE 'test.txt'");

But I get the error

Access denied for user 'registerdb'@'%' (using password: YES)

So how I give the FILE writing permission to the session/user?

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

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

发布评论

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

评论(2

倥絔 2024-12-09 11:56:30

检查该用户的权限:

显示“registerdb”@“%”的资助

如果没有列出 FILE 权限,只需添加它:

. 上的文件授予 'registerdb'@'%'

,然后:

同花顺特权;

但请注意,通过授予 *.* 上的 FILE 权限,您实际上是 授予该用户对服务器上任何文件的完全访问权限

限制 限制可以读取文件的位置写入后,将 secure_file_priv 系统设置为特定目录

Chek for permissions of that user:

SHOW GRANTS FOR 'registerdb'@'%'

If there no listed FILE permission, just add it:

GRANT FILE ON . to 'registerdb'@'%'

and then:

FLUSH PRIVILEGES;

But beware for by doing granting the FILE permission on *.* you are essentially giving that user full access to any file the server.

To limit limit the location in which files can be read and written, set the secure_file_priv system to a specific directory.

一抹淡然 2024-12-09 11:56:30

您需要先删除 MySQL 之外的文件,MySQL 中没有函数可以删除或覆盖文件。
这是一项安全措施,您应该对此表示感谢。

请注意,弄清楚 MySQL 存储其文件的确切位置可能很棘手。

我通常使用

LOAD DATA INFILE 'nonexistingfile' INTO validtable

生成的错误,为您提供 SELECT ... INTO OUTFILE 将写入的当前数据库的完整路径。
您可以使用多种方法来删除(或更好地移动)文件,例如cron-job
您甚至可以编写一个自定义 UDF 来为您执行删除操作,尽管这是一个巨大的安全风险,而这样做的程序员应该遭受比可怕的事情更糟糕的命运。

另一种选择是

START TRANSACTION;
SELECT @today:= CURDATE();
INSERT INTO saved_mail_log (filename, whensaved, user_id) 
  VALUES (CONCAT(@today,'mailsave.csv'), @today, '1234');
SELECT mail FROM users WHERE user_id = '1234' INTO OUTFILE CONCAT(@today,'mailsave.csv');
COMMIT;    

我不 100% 确定您可以使用函数来创建 OUTFILE 参数,如果不是,您将必须选择该值并使用动态 SQL 将其注入到查询中。

您可以使用以下方式获取最新文件:

SELECT * FROM saved_mail_log WHERE user_id = '1234' ORDER BY whensaved DESC LIMIT 1

You need to delete the file outside of MySQL first, there is no function in MySQL that can delete or overwrite a file.
This is a security measure and you should be thankful for that fact.

Note that it can be tricky to figure out where exactly MySQL stores its files.

I usually use

LOAD DATA INFILE 'nonexistingfile' INTO validtable

The error that generates, gives you the full path for the current database that SELECT ... INTO OUTFILE will write to.
You can use a variety of ways to delete (or better move) the file, a cron-job comes to mind.
You could even write a custom UDF that will do the deleting for you although that is a mayor security risk and programmers who do this deserve a fate worse than something horrid.

Another option is to do

START TRANSACTION;
SELECT @today:= CURDATE();
INSERT INTO saved_mail_log (filename, whensaved, user_id) 
  VALUES (CONCAT(@today,'mailsave.csv'), @today, '1234');
SELECT mail FROM users WHERE user_id = '1234' INTO OUTFILE CONCAT(@today,'mailsave.csv');
COMMIT;    

I'm not 100% sure you can use a function to create the OUTFILE parameter, if not you will have to select that value and inject it into the query using dynamic SQL.

You can get the latest file using:

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