3

I have a problem to move the default datadir of Maria DB to another partition, it appears to be very common but i tried everything I can without luck.

Mysql is installed as Mariadb 10.1.26 with the default debian package (apt-get install mysql-server) on a Debian 9.1 (stretch) server, mysqld -v returns mysqld 10.1.26-MariaDB-0+deb9u1

Default_mysql_datadir : /var/lib/mysql

New_mysql_datadir : /home/mysql

/var/lib/mysql is mounted to "/" (/dev/md3)

/home/mysql is mounted to "/home" (/dev/md4)

What I've tried

# systemctl stop mysql
# mv /var/lib/mysql /home

Change datadir in /etc/mysql/my.cnf

datadir = /home/mysql

Check if the rights/permissions are ok

# chown -R mysql.mysql /home/mysql

apparmor is NOT installed nor running on the system though the /etc/apparmor.d/usr.sbin.mysqld file is existing with the following rules :

/home/mysql/ r,
/home/mysql/** rwk,

I even tried to create and empty /var/lib/mysq folder refering to this bug

But when I start I always get the same error :

# systemctl start mysql
[Warning] Can't create test file /home/mysql/<user>.lower-test
#007/usr/sbin/mysqld: Can't change dir to '/home/mysql/' (Errcode: 13 "Permission denied") 2017-09-07  0:16:59 140119808397888 [ERROR] Aborting
mariadb.service: Main process exited, code=exited, status=1/FAILURE
Failed to start MariaDB database server.
mariadb.service: Unit entered failed state.
mariadb.service: Failed with result 'exit-code'.

Any suggestion ?

Thanks

0

2 Answers 2

5

You must set ProtectHome=false in the systemd config file in order to move your datadir to the /home directory.

According to Debian policy rules, you should set this option value in a custom file like /etc/systemd/system/mariadb.service.d/YOUR_CUSTOM_FILE.conf containing:

[Service]

# Prevent accessing /home, /root and /run/user
ProtectHome=false

Then reload systemctl daemon:

systemctl daemon-reload

After that you should be able to change MariaDB datadir from /var/lib/mysql to /home/mysql.

2
  • We just ran into this after a version upgrade on a production server. We had to add ProtectSystem=false in addition to ProtectHome=false. After adding ProtectHome=false, the error changed to: [ERROR] mysqld: File '/home/mysql/mysql-bin.index' not found (Errcode: 30 "Read-only file system") When it was clearly not actually a read only situation.
    – Jestep
    Feb 7, 2018 at 16:04
  • systemd documentation for mariadb contains this an other tips. Needing ProtectSystem=false seems odd. Did you miss running daemon-reload maybe?
    – danblack
    Feb 20, 2019 at 0:09
0

This whitelist in /etc/systemd/mariadb.service.d/whatever.conf:

[Service]
ReadWritePaths=/home/mysql

may be another solution, but I would not have found it without Mr. Payart's wisdom.

1
  • FYI: It turns out the ProtectHome=false is also required. Feb 21, 2019 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.