MariaDB will not start after a move: Can't create test file ... lower-test
[Warning] Can't create test file /home/mysql/beta.lower-testShort answer
The server writes a small test file in its data directory at startup. If that write is refused, it logs Can't create test file and aborts. The usual reasons are wrong ownership or parent directory permissions, a systemd unit that hides /home, an AppArmor profile that does not know the new path, or SELinux labels.
Fix it fast
1Find out which cause you have
Find the data directory the server really uses, then see who owns it and whether every parent folder can be entered.
mariadbd --help --verbose | grep 'datadir' | tail -1 namei -l /home/mysql2Match what you see, then jump to its fix
- 1
Can't create test file ... lower-test, then [ERROR] AbortingWrong owner, or a parent directory that cannot be enteredFix - 2
Permission Denied although ownership is right, with the data directory under /homeThe systemd unit hides /home (ProtectHome)Fix - 3
Permission denied in the error log and an apparmor denial in the kernel log, on Ubuntu or DebianAppArmor does not know the new data directoryFix - 4
avc: denied { write } in /var/log/audit/audit.log, on RHEL-family systemsSELinux gives the new path the wrong labelFix
- 1
What does the error look like?
[Warning] Can't create test file /home/mysql/beta.lower-test[ERROR] AbortingPermission Denied
Cause 1 of 4
Wrong owner, or a parent directory that cannot be entered
1Check
The whole data directory must belong to the user that runs mariadbd, usually mysql. Its directories need the x bit for the owner, and every parent directory up to / needs x for user, group and other.
ls -ld /home/mysql
namei -l /home/mysql2Fix
Give the tree back to mysql and open the execute bit on the parents.
sudo chown -R mysql:mysql /home/mysql
sudo chmod 755 /home3Verify
Start the service.
sudo systemctl start mariadbConfirmed by mariadb.com
Cause 2 of 4
The systemd unit hides /home (ProtectHome)
1Check
The default unit file sets ProtectHome, which blocks access to /home, /root and /run/user. It also sets ProtectSystem=full, which restricts a few other directories.
systemctl show mariadb.service | grep -E 'ProtectHome|ProtectSystem'2Fix
Add a drop-in that turns ProtectHome off, then reload systemd. For a data directory under /usr/local, grant it with ReadWritePaths instead.
sudo systemctl edit mariadb.service
# in the editor:
[Service]
ProtectHome=false
# then:
sudo systemctl daemon-reload3Verify
Start the service.
sudo systemctl start mariadbConfirmed by mariadb.com
Cause 3 of 4
AppArmor does not know the new data directory
1Check
Recent Ubuntu and Debian packages ship an AppArmor profile that is on by default. Look for denials.
aa-status | grep mariadb
sudo dmesg | grep -i apparmor2Fix
Map the old path to the new one in the alias file, then restart AppArmor. Local additions belong in /etc/apparmor.d/local/mariadbd.
# add to /etc/apparmor.d/tunables/alias
alias /var/lib/mysql/ -> /data/mariadb/,
# then:
sudo systemctl restart apparmor3Verify
Start the service and check for new denials.
sudo systemctl start mariadb
sudo dmesg | grep -i apparmorConfirmed by mariadb.com
Cause 4 of 4
SELinux gives the new path the wrong label
1Check
A custom data directory needs the mysqld_db_t context. Check the current label.
ls --directory --scontext /mariadb/data2Fix
Add the file context rule for the directory and apply it.
sudo semanage fcontext -a -t mysqld_db_t "/mariadb/data(/.*)?"
sudo restorecon -Rv /mariadb/data3Verify
Start the service.
sudo systemctl start mariadbConfirmed by mariadb.com
How do I stop it coming back?
- After moving a data directory, check owner, parent permissions, the systemd unit and the security profile before the first start.
- Keep the data directory outside /home when you can.
- Do not turn AppArmor or SELinux off to hide the problem; add the path rule instead.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fixDatabases
Locked out of a fresh MariaDB install as root on Mac or Linux
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
A fresh MariaDB root account is not a normal password account.
3 causes, each with a fixDatabases
An Aria table fails with 'Read page with wrong checksum' (error 1030, code 176)
ERROR 1030 (HY000): Got error 176 "Read page with wrong checksum" from storage engine Aria
Code 176 means MariaDB's Aria engine read a data or index page whose checksum does not match, so that table file is damaged.
3 causes, each with a fixDatabases
The client cannot open a TCP connection to MariaDB or MySQL (error 2003)
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111)
Error 2003 means the client tried a TCP connection to the host and port and nobody answered.
4 causes, each with a fix