MySQL Can't Create Test File: Troubleshooting Guide
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
Robert Cooper
Robert Cooper Senior Engineer at Basedash
· January 31, 2025
When MySQL reports it “can’t create test file,” that usually means there are permission or path issues in the environment where MySQL is running. This guide delves into common causes and solutions for this error.
This error occurs when MySQL cannot write to the data directory specified in its configuration. It’s essential to understand that MySQL needs write access to this directory to operate correctly.
Start by examining the permissions of the MySQL data directory. Use the following command to check:
ls -ld /path/to/mysql/data/directory
The output shows the permissions, which should allow the MySQL user to write to the directory. If permissions are incorrect, adjust them with:
sudo chown mysql:mysql /path/to/mysql/data/directory
sudo chmod 755 /path/to/mysql/data/directory
On systems with AppArmor or SELinux, these security modules can restrict access to certain directories. Ensure that the MySQL profile allows access to the data directory.
For AppArmor, check /etc/apparmor.d/ for MySQL related profiles and adjust them if necessary.
For SELinux, use the following command to check the context:
ls -Z /path/to/mysql/data/directory
To change the context, use:
sudo semanage fcontext -a -t mysqld_db_t "/path/to/mysql/data/directory(/.*)?"
sudo restorecon -Rv /path/to/mysql/data/directory
Lack of disk space can cause this error. Check the available space using:
df -h
If the disk is full, either clean up unnecessary files or expand the disk space.
Ensure the datadir path in MySQL’s configuration file (my.cnf or my.ini) is correct. Incorrect paths can lead to this error. You can find the configuration file path with:
mysql --help | grep "Default options" -A 1
File system corruption might prevent file creation. Run a file system check to ensure there are no underlying issues.
sudo fsck /path/to/affected/partition
Note: Run fsck on an unmounted partition to avoid data loss.
After making changes, restart the MySQL service to apply them.
sudo systemctl restart mysql
Written by
Senior Engineer at Basedash
Robert Cooper is a senior engineer at Basedash who builds full-stack product systems across SQL data infrastructure, APIs, and frontend architecture. His work focuses on application performance, developer velocity, and reliable self-hosted workflows that make data operations easier for teams at scale.
Basedash lets you build charts, dashboards, and reports in seconds using all your data.