今天一个同事反馈往一个MySQL数据库导入数据时,报“ERROR 1 (HY000): Can't create/write to file '/tmp/MLjnvU95' (Errcode: 13 - Permission denied)”这样的错误,如下所示:
uery OK, 0 rows affected (0.03 sec)
ERROR 1 (HY000): Can't create/write to file '/tmp/MLjnvU95' (Errcode: 13 - Permission denied)
ERROR 1 (HY000): Can't create/write to file '/tmp/MLPzmIbJ' (Errcode: 13 - Permission denied)
ERROR 1 (HY000): Can't create/write to file '/tmp/MLdpJwKm' (Errcode: 13 - Permission denied)
ERROR 1 (HY000): Can't create/write to file '/tmp/MLB7FTT0' (Errcode: 13 - Permission denied)
ERROR 1 (HY000): Can't create/write to file '/tmp/ML1wcUAF' (Errcode: 13 - Permission denied)
ERROR 1 (HY000): Can't create/write to file '/tmp/MLYjOzPk' (Errcode: 13 - Permission denied)
ERROR 1 (HY000): Can't create/write to file '/tmp/ML7gi9z0' (Errcode: 13 - Permission denied)
ERROR 1 (HY000): Can't create/write to file '/tmp/ML6nezQG' (Errcode: 13 - Permission denied)
个人使用source script.sql导入数据时,也是遇到这个错误,如上截图所示。从这个错误提示,可以明显看出是MySQL没有权限对/tmp进行操作,
查看系统变量tmpdir,如下所示,tmpdir变量指定路径为/tmp
mysql> show variables like 'tmpdir';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| tmpdir | /tmp |
+---------------+-------+
1 row in set (0.00 sec)
[root@mylx01 ~]# ls -lh /tmp
total 20M
-rw-rw-r--. 1 root root 20M Nov 7 12:04 cccc_user.sql
-rw-r--r--. 1 root root 57 Nov 7 02:00 percona-version-check
-rw-------. 1 root root 2.0K Nov 2 21:38 tmpP4BN3H
-rw-------. 1 root root 2.0K Oct 31 21:40 tmpusdOhs
-rw-------. 1 root root 2.0K Oct 30 21:36 tmp_WBZNO
-rw-------. 1 root root 2.1K Oct 29 21:38 tmpXcXptG
-rw-------. 1 root root 2.0K Nov 5 21:40 tmpyqSR0U
-rw-------. 1 root root 2.1K Oct 28 21:42 tmpZjNjtu
我们可以修改/tmp目录的权限; 或者修改数据库的临时目录来解决这个问题:
1:在配置文件my.cnf中修改数据库的临时目录,然后需要重启MySQL数据库
tmpdir = /var/lib/mysql
2:修改/tmp目录权限
chmod 1777 /tmp
或
chmod 777 /tmp
那么MySQL使用tmpdir来做什么呢?tmpdir是MySQL存临时文件的目录的路径名。如果未设置变量tmpdir,MySQL将使用系统的默认值,通常为/tmp、/var/tmp或/usr/tmp。更多详细信息,可以参考官方文档Where MySQL Stores Temporary Files的详细介绍:
MySQL arranges that temporary files are removed if mysqld is terminated. On platforms that support it (such as Unix), this is done by unlinking the file after opening it. The disadvantage of this is that the name does not appear in directory listings and you do not see a big temporary file that fills up the file system in which the temporary file directory is located. (In such cases, lsof +L1 may be helpful in identifying large files associated with mysqld.)
When sorting (ORDER BY or GROUP BY), MySQL normally uses one or two temporary files. The maximum disk space required is determined by the following expression:
(length of what is sorted + sizeof(row pointer))* number of matched rows
* 2
The row pointer size is usually four bytes, but may grow in the future for really big tables.
For some SELECT queries, MySQL also creates temporary SQL tables. These are not hidden and have names of the form SQL_*.
DDL operations that rebuild the table and are not performed online using the ALGORITHM=INPLACE technique create a temporary copy of the original table in the same directory