How to Change a MySQL Data Directory to a New Location

 




#create volmue mysql in digitalocean
#df
#du -hs /var/lib/mysql
#du -hs /var/lib/mysql/* | sort -rh | head -n 10
#get nanm volume
cd /dev/disk/by-id/
ls


mkdir -p /clone/disk/lib
#create backup lib
rsync -av /var/lib/* /clone/disk/lib/
#mount by name
mount /dev/sdb /var/lib/mysql
mount -o discard,defaults,noatime /dev/disk/by-id/NAME_VOLUME_DISK /var/lib/mysql
echo '/dev/disk/by-id/NAME_VOLUME_DISK /var/lib/mysql ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab

#retore
rsync -av /clone/disk/lib/* /var/lib/

#reset data 
# sudo mkfs.ext4 /dev/disk/by-id/NAME_VOLUME_DISK 


FULL UPDATE:

mkdir -p /clone/disk/backup_mysql
rsync -av /var/lib/* /clone/disk/backup_mysql/
mount -o discard,defaults,noatime /dev/disk/by-id/NAME_VOLUME_DISK  /var/lib/mysql
echo '/dev/disk/by-id/NAME_VOLUME_DISK  /var/lib/mysql ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab
rsync -av /clone/disk/backup_mysql/* /var/lib/
service mysqld restart