mysql新建查询运行报错_mysql执行SQL语句时报错:[Err] 3 - Error writing file '/tmp/MYP0G1B8' (Errcode: 28 - No space l...

当在MySQL中执行复杂SQL时遇到[Err] 3 - Error writing file '/tmp/MYP0G1B8' (Errcode: 28 - No space left on device)的错误,通常是由于/tmp分区空间不足导致。解决方案包括检查磁盘空间,确认SQL查询的数据量,以及修改MySQL配置文件my.cnf,将tmpdir指向具有足够空间的新目录,并确保权限设置正确。重启MySQL服务后,临时文件将存储在新目录下,从而避免错误。
摘要由CSDN通过智能技术生成

问题描述:

今天一同事在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]# mkdirmysql_tmp_dir

[root@testvm mnt]#lsmysql_tmp_dir

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

total4drwxrwxrwx2 root root 4096 Jul 5 17:49 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系统变量,官方文档参考:

9d0b4c94c8474936ab6690bbf70f87cf.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值