How to Safely Move the MySQL Data Directory on cPanel/CentOS

Post Date: May 5, 2018
Last Updated: September 19, 2026

Introduction

By default, MySQL stores its database files under /var/lib/mysql. When the existing MySQL data directory requires additional storage, better disk allocation, or a dedicated filesystem, it may be necessary to move the MySQL data directory to another location.

This article explains how to safely move the MySQL data directory from /var/lib/mysql to /home/mysql on a cPanel/CentOS server. The procedure includes copying the existing database files, stopping MySQL, performing a final synchronization, updating the MySQL configuration, and restarting the service.

Prerequisites

Before moving the MySQL data directory, ensure the following requirements are met:

  1. Root or sudo access to the server.
  2. Sufficient disk space is available in /home to store the complete MySQL data directory.
  3. A valid backup of the MySQL databases is available before making any changes.
  4. Verify the current MySQL data directory, normally /var/lib/mysql.
  5. Schedule a maintenance window, as MySQL must be stopped during the final synchronization and configuration change.
  6. Ensure no critical applications or websites are actively writing to the databases during the migration.

Implementation

This post explains how to move or set up a different data directory for the MySQL application. If no database exists, skip the first step. This post deals with moving the 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 running the rsync process in screen

screen -S rsync

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

Press Ctrl + A +  D to come out of screen

Depending on the size of the directory, it may take a 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 permissions of the 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

Conclusion

Moving the MySQL data directory to /home/mysql can help when additional storage or a dedicated filesystem is required. The migration should be performed carefully by first copying the existing data, stopping MySQL, running a final rsync synchronization, correcting ownership, updating the MySQL configuration, and then starting the service.

After the migration, verify that MySQL starts successfully and that the databases are accessible from the cPanel accounts and applications. Keep the original /var/lib/mysql data intact until the new data directory has been fully verified and confirmed to be working correctly.

FAQs

1. What is the default MySQL data directory?

The default MySQL data directory is usually:

/var/lib/mysql

2. Why would I move the MySQL data directory?

The data directory may be moved when additional storage is required, a separate filesystem is available, or better disk-space management is needed.

3. Can I move the MySQL data directory while MySQL is running?

It is recommended to stop the MySQL service before performing the final synchronization and changing the data directory configuration. This helps ensure that all database files are copied consistently.

Configure Dreamweaver to access MySQL database via FTP – cPanel – Access database via remote connection

Talk to our experts

Looking for the right technology solution for your business?. Our experts can help with web hosting, domain registration, DevOps and cloud, software development, web applications, mobile applications, and enterprise solutions. Get in touch with our team here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top