Mysql 无法创建/写入文件错误#13

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8633373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 22:09:36  来源:igfitidea点击:

Mysql Can't create/write to file error#13

mysqldjangomysql-error-1064mysql-python

提问by Nipun Batra

I had created a Django view which is returning some data after reading from MySql DB.When i try and fetch around 6000 rows from the Database,everything works fine and the view returns HttpResponse as is expected.However as i try to fetch 7000+ rows i get the following error

我创建了一个 Django 视图,它在从 MySql DB 读取后返回一些数据。当我尝试从数据库中获取大约 6000 行时,一切正常,并且视图按预期返回 HttpResponse。但是当我尝试获取 7000+ 行时我收到以下错误

(1, "Can't create/write to file '/home/nipun/mysql_temp/MYzplJok' (Errcode: 13)")


Earlier i thought that the error could be due to space getting exhausted for /temp,so i changed the tempdir setting in my.cnf
I also ensured that new tmpdir /home/nipun/mysql_temp and it's parent directories are writable by me by changing the ownership. Although this is not a Django problem,here is the view


早些时候我认为该错误可能是由于 /temp 的空间已耗尽,所以我更改了 my.cnf 中的 tempdir 设置
我还通过更改所有权。虽然这不是 Django 问题,但这里是视图

def query_json(request):
    from django.utils import simplejson
    objects=Publisher.objects.filter(location='ROOM_01',sensor_name='CPU_TEMPERATURE').order_by('-id')[0:9000]

    json = simplejson.dumps( [{"reading": float(o.reading),
                           "timestamp": str(o.timestamp)
                       } for o in objects] )

    return HttpResponse(json,mimetype="application/json")



So in the filter changing 9000 to 6000 works fine.
Some more information about the error is provided in the Django stack trace

所以在过滤器中将 9000 更改为 6000 工作正常。
Django 堆栈跟踪中提供了有关该错误的更多信息

errorclass  
<class '_mysql_exceptions.InternalError'>
errorvalue  
InternalError(1, "Can't create/write to file '/home/nipun/mysql_temp/MYuotga9' (Errcode:     13)")
error   
(<class '_mysql_exceptions.InternalError'>,
InternalError(1, "Can't create/write to file '/home/nipun/mysql_temp/MYuotga9' (Errcode: 13)"))

EDIT
As per a comment, i tried this on my MySQL prompt

编辑
根据评论,我在我的 MySQL 提示上尝试了这个

mysql> CREATE TEMPORARY TABLE t (i int);
ERROR 1005 (HY000): Can't create table 't' (errno: 13)

So it essentially is now an issue of how to allow MySQL to write to temporary directory

所以现在本质上是如何允许 MySQL 写入临时目录的问题

回答by user1771398

It is probably a permission issue for the temporary folder that is used by Mysql.

这可能是Mysql使用的临时文件夹的权限问题。

You can find the folder used by Mysql for temp operations in the config file in /etc/mysql/my.cnfas such :

您可以在配置文件中找到 Mysql 用于临时操作的文件夹,/etc/mysql/my.cnf如下所示:

tmpdir          = /tmp

As you can see, the temp folder in use by default is "/tmp".

如您所见,默认使用的临时文件夹是“/tmp”。

Privileges should be :

特权应该是:

drwxrwxrwt   9 root root  4096 oct.  24 15:39 tmp/

So anybody, including the "mysql" user, can use it. If not correctly set, the following command would do the trick :

所以任何人,包括“mysql”用户,都可以使用它。如果设置不正确,以下命令可以解决问题:

chmod 0777 /tmp

Hope this helps !

希望这可以帮助 !

回答by LanreSmith

If after giving necessary write permissions to the new tmpdir you're still having the same error - and mysql therefore can't start - you might have AppArmorenabled. So do :

如果在向新的 tmpdir 授予必要的写入权限后,您仍然遇到相同的错误 - 因此 mysql 无法启动 - 您可能启用了AppArmor。这样做:

sudo vim /etc/apparmor.d/local/usr.sbin.mysqld

sudo vim /etc/apparmor.d/local/usr.sbin.mysqld

Of course you can use any text editor other than vim eg nano. Note that usr.sbin.mysqldat the end corresponds to what you get by doing which mysqlor which mysqld(with /replaced by .). When the file is open, you'll see some comments at the top like :

当然,您可以使用 vim 以外的任何文本编辑器,例如 nano。请注意,usr.sbin.mysqld最后对应于您通过执行which mysqlwhich mysqld/替换为.)获得的结果。当文件打开时,您会在顶部看到一些注释,例如:

# Site-specific additions and overrides for usr.sbin.mysqld.
# For more details, please see /etc/apparmor.d/local/README.

Then put after those comments :

然后把这些评论放在后面:

/path/to/new/tmp/dir/ r,
/path/to/new/tmp/dir/** rwk,

And then do : sudo service apparmor reload. Now try starting mysql : sudo service mysql startand you should be good to go.

然后做:sudo service apparmor reload。现在尝试启动 mysql :sudo service mysql start你应该很高兴。

回答by MarkR

You have configured mysql's tmpdir to point to a directory the server does not have permission to write to.

您已将 mysql 的 tmpdir 配置为指向服务器无权写入的目录。

For small filesorts, MySQL uses an in-memory buffer, but for larger ones uses files on disc (in the tmpdir).

对于小型文件排序,MySQL 使用内存缓冲区,但对于较大的文件排序,使用磁盘上的文件(在 tmpdir 中)。

In any case, it is a sysadmin error (not a programming error) setting the MySQL tmpdir to point to a unwritable directory.

在任何情况下,将 MySQL tmpdir 设置为指向不可写目录是系统管理员错误(不是编程错误)。

回答by Walk

Try giving mysql user access to the directory via below command:

尝试通过以下命令授予 mysql 用户访问该目录的权限:

chown -R mysql:mysql /home/nipun/mysql_temp/

chown -R mysql:mysql /home/nipun/mysql_temp/

This worked for me (:

这对我有用(: