On this case, I’m using MariaDB 10.11 (LTS), and we want to move Data Directory to /srv directory. This tutorial also might works on MySQL. All you need to know is its concept/ basic.
Shutdown MariaDB before making changes to the data directory:
sudo service mysql stop
Copy the existing database directory, /var/lib/mysql, to the new location, /srv, with rsync. Using the -a flag preserves the permissions and other directory properties, while-v provides verbose output so you can follow the progress:
sudo rsync -av /var/lib/mysql /srv
Rename the current folder with a .bak extension and keep it until you’ve confirmed the move was successful. By renaming it, you’ll avoid confusion that could arise from files in both the new and the old location:
sudo mv /var/lib/mysql /var/lib/mysql.bak
MariaDB has several ways to override configuration values. By default, the datadir is set to /var/lib/mysql in the /etc/mysql/maridb.conf.d/50-server.cnf file. Edit this file in your preferred text editor to reflect the new data directory. Here we’ll use nano:
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
datadir=/srv/mysql
You need to tell AppArmor to let MariaDB write to the new directory by creating an alias between the default directory and the new location. AppArmor is a security module in the Linux kernel that allows system administrators to restrict program capabilities through program profiles, rather than users themselves. Start by opening up and editing the AppArmor alias file:
sudo nano /etc/apparmor.d/tunables/alias
alias /var/lib/mysql/ -> /srv/mysql/
Then you need either an appropriate directory or symbolic link to start the server, you must create the minimal directory structure to pass the script’s environment check:
sudo mkdir /var/lib/mysql/mysql -p
Now you’re ready to start MariaDB:
sudo service mysql start
Done!