Cannot change mariadb datadir since debian 9

debian-stretchmariadb

I've done this 1000 times on debian 8 but with no success since I tried on debian 9.

I would like to change the default datadir for mariadb, here is the process from a virgin debian 9 VM:

apt-get update && apt-get install mariadb-server mariadb-client
service mysql stop
rsync -av /var/lib/mysql /home/
vi /etc/mysql/mariadb.conf.d/50-server.cnf

grep -r datadir /etc/mysql/
/etc/mysql/mariadb.conf.d/50-server.cnf:datadir     = /home/mysql   

[11:29:32]root@deb9: ~ # ls -la /home/mysql/
drwxr-xr-x 4 mysql mysql     4096 Sep  7 11:25 .
drwxr-xr-x 3 root  root      4096 Sep  7 11:25 ..
-rw-rw---- 1 mysql mysql    16384 Sep  7 11:25 aria_log.00000001
-rw-rw---- 1 mysql mysql       52 Sep  7 11:25 aria_log_control
-rw-r--r-- 1 root  root         0 Sep  7 11:25 debian-10.1.flag
-rw-rw---- 1 mysql mysql 12582912 Sep  7 11:25 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Sep  7 11:25 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Sep  7 11:25 ib_logfile1
-rw-rw---- 1 mysql mysql        0 Sep  7 11:25 multi-master.info
drwx------ 2 mysql root      4096 Sep  7 11:25 mysql
drwx------ 2 mysql mysql     4096 Sep  7 11:25 performance_schema

service mysql start


Sep  7 09:27:44 debian systemd[1]: Starting MariaDB database server...
Sep  7 09:27:44 debian mysqld[15106]: 2017-09-07 11:27:44 140699007124032 [Note] /usr/sbin/mysqld (mysqld 10.1.26-MariaDB-0+deb9u1) starting as process 15106 ...
Sep  7 09:27:44 debian mysqld[15106]: 2017-09-07 11:27:44 140699007124032 [Warning] Can't create test file /home/mysql/deb9.lower-test
Sep  7 09:27:44 debian mysqld[15106]: #007/usr/sbin/mysqld: Can't change dir to '/home/mysql/' (Errcode: 13 "Permission denied")
Sep  7 09:27:44 debian mysqld[15106]: 2017-09-07 11:27:44 140699007124032 [ERROR] Aborting
Sep  7 09:27:44 debian systemd[1]: mariadb.service: Main process exited, code=exited, status=1/FAILURE

As you may see, the error is Permission denied
some threads talks about app armor but this one is not running on processlist and I didn't touch anything about it.

grep -r maria /etc/apparmor
grep -r mysql /etc/apparmor

returns nothing.

I don't use SELINUX either …

Thanks for your help.

Best Answer

The problem is the systemd service file mariadb.service which has set

ProtectHome = true

by default. This setting prevents the service from accessing /home, /root and /run/user directories.

So either you mount your mysql data to a different location or you set this feature to false.

To disable this feature, best would be to use systemctl edit mariadb which opens the editor defined in the environment variable EDITOR. Just put

[Service]
ProtectHome = false

in that editor. Save and close and an override file /etc/systemd/system/mariadb.service.d/override.conf will be created. After that a systemctl daemon-reload is needed to re-read the setting. Start your service.