Introduction
In certain server environments, MySQL may encounter problems due to limited space in /tmp
or high memory usage that forces the server to rely on swap. This is especially true for operations involving large temporary tables (e.g., ALTER TABLE
, GROUP BY
, ORDER BY
). A practical solution is to move MySQL’s temporary directory (tmpdir
) to a larger partition such as /home
.
🛠️ Procedure
Step 1: Adjusting Settings for MariaDB (If Applicable)
If you’re using MariaDB (common on many cPanel servers), you must allow MariaDB access to /home
.
- Create a systemd override directory and file:
mkdir -p /etc/systemd/system/mariadb.service.d nano /etc/systemd/system/mariadb.service.d/override.conf
- Add the following configuration:
[Service] ProtectHome=false
- Reload systemd and restart MariaDB:
systemctl daemon-reexec /scripts/restartsrv_mysql
Step 2: Moving MySQL tmpdir
to /home
Whether you’re using MySQL or MariaDB, continue with these steps:
- Create a new temporary directory:
mkdir /home/mysqltmp chown mysql:mysql /home/mysqltmp chmod 750 /home/mysqltmp
- Modify the MySQL configuration:
Open/etc/my.cnf
in your preferred editor and add the following line under the[mysqld]
section:[mysqld] tmpdir=/home/mysqltmp
- Restart MySQL to apply the changes:
/scripts/restartsrv_mysql
Step 3: Verify the Configuration
To confirm the new temporary directory is in use:
mysqladmin variables | grep tmpdir
Expected output:
| slave_load_tmpdir | /home/mysqltmp |
| tmpdir | /home/mysqltmp |
✅ Conclusion
Moving MySQL’s tmpdir
to /home
can significantly enhance performance and stability on systems where /tmp
is constrained or when working with large datasets. Just be sure to:
- Set correct permissions.
- Avoid using user-writable locations (e.g.,
/home/user/tmp
) to prevent security risks. - Monitor disk usage regularly.
This is a safe and effective optimization when implemented properly on cPanel servers running AlmaLinux 9 / CloudLinux 9 with LiteSpeed and MariaDB/MySQL.