mysql执行SQL语句时报错:[Err] 3 - Error writing file '/tmp/MYP0G1B8' (Errcode: 28 - No space left on device)

时间:2021-11-08 03:49:05

问题描述:

  今天一同事在mysql中执行SQL语句的时候,报了/tmp空间不足的问题,报错如下:

[SQL] SELECT f.prov as 字段1, MAX( CASE f.flag_name WHEN '字段1' THEN f.num ELSE 0 END ) AS '字段1', MAX( CASE f.flag_name WHEN '店员量' THEN f.num ELSE 0 END ) AS '字段1', MAX( CASE f.flag_name WHEN '字段1' THEN f.num ELSE 0 END ) AS '字段1' FROM 
( SELECT '字段1' AS flag_name, ( SELECT b.AREA_NAME FROM sc_area b WHERE b.AREA_CODE = a.privinceCode ) AS prov, COUNT(DISTINCT a.storeID) AS num FROM promotion a WHERE DATE_FORMAT(a.pushTime, '%Y') = '2018'GROUP BY a.privinceCode
  UNION ALL SELECT '字段1' AS flag_name, (   SELECT b.AREA_NAME FROM sc_area b WHERE b.AREA_CODE = a.privinceCode ) AS prov, COUNT(DISTINCT a.staffID) AS num FROM promotion a WHERE DATE_FORMAT(a.pushTime, '%Y') = '2018' GROUP BY a.privinceCode
  UNION ALL SELECT '字段1' AS flag_name, ( SELECT b.AREA_NAME FROM sc_area b WHERE b.AREA_CODE = a.privinceCode ) AS prov, COUNT(1) AS num FROM promotion a WHERE DATE_FORMAT(a.pushTime, '%Y') = '2018' AND a.pushSts = '1' GROUP BY a.privinceCode
) AS f GROUP BY f.prov ;
[Err] 3 - Error writing file '/tmp/MYP0G1B8' (Errcode: 28 - No space left on device)

故障分析:

  通过以上的错误提示,可以知道是在执行SQL的时候,创建临时表进行排序的时候,/tmp空间不足导致的.

故障处理:

1.查看磁盘上tmp空间大小,发现/tmp空间剩余5.2GB

2.与同事沟通,SQL查询的表非常的打,有8G大小,所以明显是tmp空间大小不够用了

3.无法扩展/tmp的大小,所以就修改mysql中tmpdir的位置,由于该参数是只读参数,只能在my.cnf中修改,重启生效

4.创建临时目录,并且修改权限

[root@testvm mnt]# mkdir mysql_tmp_dir
[root@testvm mnt]# ls
mysql_tmp_dir
[root@testvm mnt]# chmod mysql_tmp_dir/ #必须修改权限,让启动mysql的账户是可读写的. 此处是在root下创建,也可以切换到mysql用户下,然后创建目录,这样默认mysql就有读写权限了.
[root@testvm mnt]# ls -ltr
total
drwxrwxrwx root root Jul : mysql_tmp_dir

5.修改my.cnf

tmpdir = /mnt/mysql_tmp_dir

6.重启mysql实例

7.查看tmpdir变量值

mysql> show variables like '%tmpdir%';
+-------------------+--------------------+
| Variable_name | Value |
+-------------------+--------------------+
| innodb_tmpdir | |
| slave_load_tmpdir | /mnt/mysql_tmp_dir |
| tmpdir | /mnt/mysql_tmp_dir |
+-------------------+--------------------+
3 rows in set (0.01 sec)

备注:tmpdir变量已经修改生效了,后续执行SQL过程中生成的临时文件都会存储在这个目录下了.

tmpdir系统变量,官方文档参考:

mysql执行SQL语句时报错:[Err] 3 - Error writing file '/tmp/MYP0G1B8' (Errcode: 28 - No space left on device)

The path of the directory to use for creating temporary files. It might be useful if your default /tmp directory resides on a partition 
that is too small to hold temporary tables. This option accepts several paths that are used in round-robin fashion. Paths should be
separated by colon characters (:) on Unix and semicolon characters (;) on Windows. If the MySQL server is acting as a replication slave,
you should not set --tmpdir to point to a directory on a memory-based file system or to a directory that is cleared when the server host
restarts. For more information about the storage location of temporary files, see Section B.5.3.5, “Where MySQL Stores Temporary Files”.
A replication slave needs some of its temporary files to survive a machine restart so that it can replicate temporary tables or LOAD DATA
INFILE operations. If files in the temporary file directory are lost when the server restarts, replication fails.

文档创建时间:2018年7月5日17:53:46