Pages

Monday, February 18, 2013

Moving MySQL on cPanel: A Simple Guide

There are times you might need to change where your MySQL databases are stored on your cPanel server. Maybe your current disk is full, or you want to put your databases on a faster storage device. Whatever the reason, moving MySQL is straightforward and won't break your cPanel.


STEP 1: BACK UP YOUR DATA

Always back up your data before making any big changes. It's like saving your game progress before a big boss fight.

One easy way to create a backup:

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

This command creates a compressed file called mysql.tar that contains all your MySQL data.


STEP 2: TELL MYSQL ABOUT THE NEW LOCATION

You need to update MySQL's main configuration file, my.cnf, to point to the new data directory.

  1. Open the file /etc/my.cnf.

  2. Find the section labeled [mysqld].

  3. Add or change the datadir line to reflect your new path.

For example, if you're moving MySQL from /var/lib/mysql to /home2/mysql, you would add this line:

datadir=/home2/mysql

Do NOT restart MySQL yet!


STEP 3: MOVE THE DATA

Now it's time to copy your existing MySQL data to the new location. We'll use rsync for this, which is good for copying files efficiently.

It's best to stop MySQL, copy the data, and then start it again. If you have a very large database and the copy will take a long time, you can run rsync multiple times while MySQL is still running. However, the final copy must be done with MySQL stopped to ensure data consistency, especially if you use InnoDB tables.

Here's the command to sync, using our example of moving to /home2/mysql:

rsync -av /var/lib/mysql /home2

Next, you need to create a link so cPanel and other applications can find the MySQL socket file in its new home:

ln -sf /home2/mysql/mysql.sock /tmp


STEP 4: RESTART MYSQL

Since you've already updated the my.cnf file, all you need to do now is restart the MySQL service. It will automatically start using the new data directory.

After restarting, your MySQL databases should be running smoothly from their new location.


No comments:

Post a Comment