OS: Ubuntu 20.04 64bit
MySQL version: MySQL 8
1. Copy the default directory
First of all, please be sure to close the MySQL service:
sudo service mysql stop
Create a new directory in your desired location to use as the MySQL data directory:
mkdir /your_data_folder
Then, go to the directory /var/lib/mysql:
cd /var/lib/mysql
There are many contents under this directory:
Copy all the contents of this directory into the newly created directory you just created:
sudo cp -R /var/lib/mysql/* /your_data_folder
Second, modify the MySQL configuration file
Enter the directory /etc/mysql/mysql.conf.d and find the file mysqld.cnf:
Open this file:
sudo vim mysqld.cnf
In the file, find the "datadir" line, and change the path to the newly created directory:
3. Modify apparmor
Enter the following directory: /etc/apparmor.d, find the file named usr.sbin.mysqld:
Open the file for editing:
sudo vim usr.sbin.mysqld
Locate the Allow data dir access configuration area in the file:
Change the directory path to the directory path you just created, pay attention to keep the last / r and /** rwk
4. Start MySQL
If everything is modified correctly, MySQL can be started normally: