Skip to content

MySQL tmpdir filling up on a cPanel host — where it points and why it matters

MySQL writes large temporary tables to `tmpdir`, which defaults to /tmp on most cPanel installs. A 4GB sort against a 100MB /tmp causes silent failures.

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

  1. 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.
  2. Set tmpdir persistently in /etc/my.cnf.d/cpanel.cnf:
    [mysqld]
    tmpdir = /var/lib/mysql_tmp
    Restart MySQL.
  3. 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.
  4. For CloudLinux hosts with cagefs, be careful. cagefs maps /tmp per user; moving tmpdir out of /tmp means MySQL’s temp files are visible across users (which is generally fine because they’re owned by mysql user and other accounts can’t read them). Don’t move tmpdir into a cagefs-managed path.
  5. Raise in-memory temp table limits to reduce disk spills:
    [mysqld]
    tmp_table_size = 64M
    max_heap_table_size = 64M
    Both must be set; MySQL uses the smaller of the two. Higher values keep more sorts in RAM but consume per-connection memory.
  6. Investigate why temp tables are huge. A Created_tmp_disk_tables rate over 10% suggests queries that need indexes. Look at the slow query log for queries with using temporary; using filesort in EXPLAIN. An index on the GROUP BY or ORDER BY column often eliminates the temp table entirely.
  7. For one-off big imports/exports that legitimately need huge temp space, set tmpdir per-session if the workload allows:
    SET SESSION tmpdir = '/data/scratch';
    Not supported in all versions; check before relying on it.
  8. 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.