Move mariaDb database to home folder

fedoramariadbselinux

after install I'm trying to move the database location
On fedora:

# su
# systemtcl stop mariadb
# cp -rp /var/lib/mysql /home
# chown mysql.mysql /home/mysql

Next i'm editing the file /etc/my.cnf.d/mariadb-server.cnf

from

[mysqld]
datadir=/var/lib/mysql/
socket=/var/lib/mysql.sock

to

[mysqld]
datadir=/home/mysql/
socket=/home/mysql.sock

then

# systemctl start mariadb
Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.

So

systemctl status mariadb
● mariadb.service - MariaDB 10.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2016-06-29 17:51:18 CEST; 29s ago
  Process: 6488 ExecStopPost=/usr/libexec/mysql-wait-stop (code=exited, status=0/SUCCESS)
  Process: 3103 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 6304 ExecStartPost=/usr/libexec/mysql-wait-ready $MAINPID (code=exited, status=1/FAILURE)
  Process: 6303 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=1/FAILURE)
  Process: 6259 ExecStartPre=/usr/libexec/mysql-prepare-db-dir %n (code=exited, status=0/SUCCESS)
  Process: 6231 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 6303 (code=exited, status=1/FAILURE)

Jun 29 17:51:17 123PUB-PC systemd[1]: Starting MariaDB 10.0 database server...
Jun 29 17:51:17 123PUB-PC mysqld_safe[6303]: 160629 17:51:17 mysqld_safe Logging to '/var/log/maria...g'.
Jun 29 17:51:17 123PUB-PC mysqld_safe[6303]: mkdir: impossible de créer le répertoire « /home/mysql…xiste
Jun 29 17:51:17 123PUB-PC mysqld_safe[6303]: Fatal error Can't create database directory '/home/mys...ck'
Jun 29 17:51:17 123PUB-PC systemd[1]: mariadb.service: Main process exited, code=exited, status=1/FAILURE
Jun 29 17:51:18 123PUB-PC systemd[1]: mariadb.service: Control process exited, code=exited status=1
Jun 29 17:51:18 123PUB-PC systemd[1]: Failed to start MariaDB 10.0 database server.
Jun 29 17:51:18 123PUB-PC systemd[1]: mariadb.service: Unit entered failed state.
Jun 29 17:51:18 123PUB-PC systemd[1]: mariadb.service: Failed with result 'exit-code'.
Hint: Some lines were ellipsized, use -l to show in full.

So it seem that mysql don't have write permission on home/mysql, but :

# ls -la /home/mysql
drwxr-xr-x. 4 mysql mysql     4096 Jun 29 17:38 .
drwxr-xr-x. 5 root  root      4096 Jun 29 17:54 ..
-rw-rw----. 1 mysql mysql    16384 Jun 29 17:38 aria_log.00000001
-rw-rw----. 1 mysql mysql       52 Jun 29 17:38 aria_log_control
-rw-rw----. 1 mysql mysql 12582912 Jun 29 17:38 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Jun 29 17:38 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Jun 29 17:05 ib_logfile1
-rw-rw----. 1 mysql mysql        0 Jun 29 17:05 multi-master.info
drwx------. 2 mysql mysql     4096 Jun 29 17:05 mysql
-rw-r--r--. 1 mysql mysql       16 Jun 29 17:05 mysql_upgrade_info
drwx------. 2 mysql mysql     4096 Jun 29 17:05 performance_schema

I don't see any difference with the main directory.

So what next?

Best Answer

It's an SELinux issue. But to make sure of that disable enforcing just for testing.

Testing

Execute this in your server (root only) to temporarily disable SELinux

# setenforce 0

Now you can start the server

# systemctl start mariadb

Connect using mysql client but first modify the configuration on file /etc/my.cnf.d/client.cnf to use the new socket file.

#
# These two groups are read by the client library
# Use it for options that affect all clients, but not the server
#


[client]
socket=/home/mysql/mysql.sock

# This group is not read by mysql client library,
# If you use the same .cnf file for MySQL and MariaDB,
# use it for MariaDB-only client options
[client-mariadb]

If everything was OK then you'll be able to connect to your newly located database.

Stop the server

# systemctl stop mariadb

Permanent solution with SELinux configuration

SELinux prevents mysqld to work (read, write, access) in any directory other than the default.
To overcome this you need to modify this behavior so SELinux grants permissions to your new location.

Get the context. Listing may differ but should show mysqld_db_t context.

# ls -lZ /var/lib/mysql
drwx------. mysql mysql unconfined_u:object_r:mysqld_db_t:s0 mysql

Add the context obtained (mysqld_db_t) to the mapping for /home/mysql.

# semanage fcontext -a -t mysqld_db_t "/home/mysql(/.*)?"

Apply the context

# restorecon -R -v /home/mysql

Check that the context is applied

# grep -i mysql /etc/selinux/targeted/contexts/files/file_contexts.local
/home/mysql(/.*)?    system_u:object_r:mysqld_db_t:s0


# ls -lZ /home/mysql
drwxr-xr-x. mysql mysql system_u:object_r:mysqld_db_t:s0 mysql

Everything is now in place. You should have no problem in re enforcing SELinux and get your server UP.

# setenforce 1

# systemctl start mariadb