Move mysql data folder to ZFS on CentOS 7

1. change origin data folder as mount point

sudo systemctl stop mariadb.service
cd /var/lib
sudo mv mysql mysql.bak
sudo mkdir mysql
sudo chown mysql:mysql mysql

2. create zfs file system

sudo zfs create -o mountpoint=/var/lib/mysql mysqldata/mysql

3. mount zfs file system

sudo zfs mount -a

4. change mount point owner as mysql and cp all data file to /var/lib/mysql

sudo chown mysql:mysql mysql
cd mysql.bak
sudo cp -p -r * ../mysql
cd ..

5. install package policycoreutils-python

sudo yum install policycoreutils-python

6. run semanage fcontext and restorecon

sudo semanage fcontext -a -t mysqld_db_t "/mysql(/.*)?"
sudo grep -i mysql /etc/selinux/targeted/contexts/files/file_contexts.local
sudo restorecon -R -v /var/lib/mysql

7. start mysql

sudo systemctl start mariadb.service

Reference: MariaDB Changing Database Location

How to install zfs on CentOS7

1. Install epel-release and setup zfs repository.

sudo yum -y install epel-release
sudo yum localinstall --nogpgcheck

2. update and reboot

sudo yum -y update
sudo shutdown -r now

3. install kernel-dev zfs

$ sudo yum install kernel-devel zfs
$ sudo modprobe zfs
$ sudo lsmod |grep zfs
zfs 2713912 4
zunicode 331170 1 zfs
zavl 15236 1 zfs
zcommon 55411 1 zfs
znvpair 93227 2 zfs,zcommon
spl 92223 3 zfs,zcommon,znvpair

4. create a zpool

$ ls /dev/vd*
/dev/vda  /dev/vda1  /dev/vda2  /dev/vdb  /dev/vdb1  /dev/vdb9
$ sudo zpool create -f <pool name> /dev/vdb

5. add a new disk to zpool (This disk cannot be removed after add. Only spare disk can be removed)

$ ls /dev/vd*
/dev/vda  /dev/vda1  /dev/vda2  /dev/vdb  /dev/vdb1  /dev/vdb9 /dev/vdc
$ sudo zpool add<pool name> /dev/vdc

NOTE: if zpool cannot be mounted automatically after reboot, running the following command can fix it.

sudo systemctl preset zfs-import-cache zfs-import-scan zfs-mount zfs-share zfs-zed