Repost: The BEST way to migrate MySQL to a new drive.
I am reposting due to a troll hijacking my other thread, so I can be of help to anyone willing to learn. 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/sdb
/dev/hdb
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:
*Once you run the first line, all database powered sites will stop working.
In this process, all permissions, ownership, paths and all that jazz are preserved. mysql.actual will also service as a backup!
Then the actual process, run these commands:
This will mount the whole drive, adjust the path to target a partition, seems I have to spell this out instead of simply implying it. This will make your partition, label it, move the data flawlessly without changing my.cnf or permissions. To target only a partition, change /dev/sdb to /dev/sdb# with # being your partition number (1-4)
service mysql stop;
mkdir /var/lib/mysql.actual;
rsync -avz /var/lib/mysql/ /var/lib/mysql.actual;
/sbin/mkfs.ext4 -L /var/lib/mysql /dev/sdb;
mount /dev/sdb /var/lib/mysql;
rsync -avz /var/lib/mysql.actual/ /var/lib/mysql;
service mysql start;
then in /etc/fstab add this to mount at boot:
(DO NOT DO THIS YOURSELF IF YOU DON'T UNDERSTAND /etc/fstab!!! YOU MIGHT NOT BOOT BACK ONLINE IF DONE WRONG.)
Run command:
vi /ect/fstab;
then find a blank line and enter the letter s
then find a blank line or make one and paste this by right clicking.
LABEL=/var/lib/mysql /var/lib/mysql ext4 defaults 1 2
Now just hit :wq to save and exit. Reboot and check everything. Do this during your host's business hours so you can get help in case you set the paths wrong in fstab and be sure to contact your host to verify it FIRST if needed.
Please sign in to leave a comment.
Comments
0 comments