After seeing so many BAD ANSWERS to people asking how to migrate MySQL to a new drive, I posted this to make your life a lot easier. This method will make the switch perfectly in seconds. This is for red hat, centos, etc.
___________________________________________________
CONFIRM ALL PATHS PRIOR TO RUNNING!!
cpanel MySQL/MariaDB Path: /var/lib/mysql
Linux non-cPanel MySQL/MariaDB Path: /var/bin/mysql
The path to your harddrive may be different than mine.
So, use the command lsblk to list all unmounted drives.
It may be any of these, the final letter means which drive it is, "b" would mean drive #2 and "c" would mean drive #3 and so-forth:
/dev/xvdb
/dev/sdbb
/dev/hddb
So find out which is yours and replace all instances of /dev/xvdb with your path in the blue code below.
___________________________________________________
Now to the actual commands:
*You can run one line at a time, experts can run all at once.
*Once you run the first line, all database powered sites will stop working.
service mysql stop;
mv /var/lib/mysql /var/lib/mysql.actual;
/sbin/mkfs.ext4 -L /var/lib/mysql /dev/xvdb;
mount /dev/xvdb /var/lib/mysql;
rsync -avz /var/lib/mysql.actual/ /var/lib/mysql;
service mysql start;
then in fstab add this to mount at boot:
(DO NOT DO THIS YOURSELF IF YOU DON'T UNDERSTAND fstab!!! YOU MIGHT NOT BOOT BACK ONLINE IF DONE WRONG.)
LABEL=/var/lib/mysql /var/lib/mysql ext4 defaults 1 2
Done.
___________________________________________________
CONFIRM ALL PATHS PRIOR TO RUNNING!!
cpanel MySQL/MariaDB Path: /var/lib/mysql
Linux non-cPanel MySQL/MariaDB Path: /var/bin/mysql
The path to your harddrive may be different than mine.
So, use the command lsblk to list all unmounted drives.
It may be any of these, the final letter means which drive it is, "b" would mean drive #2 and "c" would mean drive #3 and so-forth:
/dev/xvdb
/dev/sdbb
/dev/hddb
So find out which is yours and replace all instances of /dev/xvdb with your path in the blue code below.
___________________________________________________
Now to the actual commands:
*You can run one line at a time, experts can run all at once.
*Once you run the first line, all database powered sites will stop working.
service mysql stop;
mv /var/lib/mysql /var/lib/mysql.actual;
/sbin/mkfs.ext4 -L /var/lib/mysql /dev/xvdb;
mount /dev/xvdb /var/lib/mysql;
rsync -avz /var/lib/mysql.actual/ /var/lib/mysql;
service mysql start;
then in fstab add this to mount at boot:
(DO NOT DO THIS YOURSELF IF YOU DON'T UNDERSTAND fstab!!! YOU MIGHT NOT BOOT BACK ONLINE IF DONE WRONG.)
LABEL=/var/lib/mysql /var/lib/mysql ext4 defaults 1 2
Done.
Last edited: