Change mysql data directory on cPanel| Safely move mysql data directory – Centos

Date Posted: 05-05-2018

This post explains on how to move or setup different data directory for mysql application. If there is no database exists, skip the first step. This post deals with moving data directory to /home/mysql

Create an existing data directory in a tar file. The default data directory for mysql is /var/lib/mysql

tar -cvf mysql.tar /var/lib/mysql

Create the directory /home/mysql

mkdir -p /home/mysql

Now, rsync the data directory from /var/lib/mysql to /home/mysql. If the data directory is huge then we recommend to run rsync process in screen

screen -S rsync

rsync -avp /var/lib/mysql/ /home/mysql

Press Ctrl + A +  D to come out of screen

Depends on the size of the directory , it may take little while.  Now, open the screen and view the progress state.

screen -ls

screen -x screenID

Now, stop the mysql service.

/etc/init.d/mysql stop

Initiate the rsync process again to copy the data directory.

rsync -avp --delete /var/lib/mysql/ /home/mysql/

Change the permission of new data directory with mysql

chown -R mysql.mysql /home/mysql

Edit the mysql configuration with new data directory(/etc/my.cnf)

vi /etc/my.cnf

datadirectory=/home/mysql

Now, start the mysql service.

/etc/init.d/mysql start

 

Leave a Reply