A query that worked yesterday now returns “The table ‘tmp_table’ is full.” Disk seems fine — /var/lib/mysql has plenty of space. The error refers to MySQL’s temporary table area, which is controlled by tmpdir and on most cPanel hosts points at /tmp, which on a CloudLinux+cPanel install is often a separate tmpfs or small partition. A complex sort or join that needs 4GB of intermediate space fails when /tmp is 100MB. The fix is moving tmpdir somewhere with real disk, but doing so changes assumptions about CloudLinux per-user temp isolation.
What’s happening
MySQL creates temporary tables for sorts, joins, group-by operations, derived tables, and some internal operations. Small temp tables stay in memory (limited by tmp_table_size and max_heap_table_size). When they exceed those, MySQL spills to disk — to a file in tmpdir. If tmpdir is small or full, the query fails.
cPanel’s /tmp is often a 1GB or 2GB tmpfs mount on CloudLinux systems (the cagefs per-user isolation uses small per-user /tmp). A single sort on a 5GB table can easily produce 4GB of intermediate sort data. The temp table fills /tmp, the query fails, and the next query has the same problem.
How to verify
mysql -e "SHOW VARIABLES LIKE 'tmpdir'"
mysql -e "SHOW VARIABLES LIKE 'tmp_table_size'"
mysql -e "SHOW VARIABLES LIKE 'max_heap_table_size'"
mysql -e "SHOW GLOBAL STATUS LIKE 'Created_tmp_disk_tables'"
mysql -e "SHOW GLOBAL STATUS LIKE 'Created_tmp_tables'"
df -h /tmp $(mysql -BNe "SHOW VARIABLES LIKE 'tmpdir'" | awk '{print $2}')
ls /tmp/#sql*
The tmpdir value is the target. The two STATUS values give the ratio of disk-spilled vs in-memory temp tables — if Created_tmp_disk_tables / Created_tmp_tables > 30%, you’re spilling too often. The df shows free space; the ls shows current MySQL temp files (#sql* is the prefix).
The fix
- Choose a tmpdir on a volume with real space. On most cPanel hosts:
- Create
/var/lib/mysql_tmp(on the same volume as/var/lib/mysql— fast and big). chown mysql:mysql /var/lib/mysql_tmp && chmod 750 /var/lib/mysql_tmp.
- Create
- Set tmpdir persistently in
/etc/my.cnf.d/cpanel.cnf:
Restart MySQL.[mysqld] tmpdir = /var/lib/mysql_tmp - Verify the new path:
mysql -e "SHOW VARIABLES LIKE 'tmpdir'"and run a sort-heavy query to confirm temp files land in the new location. - For CloudLinux hosts with cagefs, be careful. cagefs maps
/tmpper user; moving tmpdir out of /tmp means MySQL’s temp files are visible across users (which is generally fine because they’re owned bymysqluser and other accounts can’t read them). Don’t move tmpdir into a cagefs-managed path. - Raise in-memory temp table limits to reduce disk spills:
Both must be set; MySQL uses the smaller of the two. Higher values keep more sorts in RAM but consume per-connection memory.[mysqld] tmp_table_size = 64M max_heap_table_size = 64M - Investigate why temp tables are huge. A
Created_tmp_disk_tablesrate over 10% suggests queries that need indexes. Look at the slow query log for queries withusing temporary; using filesortin EXPLAIN. An index on the GROUP BY or ORDER BY column often eliminates the temp table entirely. - For one-off big imports/exports that legitimately need huge temp space, set
tmpdirper-session if the workload allows:
Not supported in all versions; check before relying on it.SET SESSION tmpdir = '/data/scratch'; - Multiple tmpdirs (separated by colon on Linux): MySQL round-robins between them. Useful on hosts with multiple disks.
For environments where temp table spills are a regular issue, Stack Harbor’s environment management team correlates the spill rate with the slow query log and proposes indexing fixes that reduce both temp tables and overall query time.