rss feed Twitter Page Facebook Page Github Page Stack Over Flow Page

How to Move MySQL Database Data Directory

If for some reason your database expands at a high rate, you might need to consider moving your data to another disk. Here's a simple trick on how to move the MySQL database directory to another location or drive.

First stop the mysql service :

sudo /etc/init.d/mysql stop

Create new MySQL directory

sudo mkdir /path/to/new/mysql

Move the data from the current data directory to new data directory.

sudo mv /var/lib/mysql /path/to/new/mysql

Or

sudo cp -R /var/lib/mysql/* /path/to/new/mysql/

Assign the MySQL group and owner for the new data directory.

chown -R mysql:mysql /data/mysql

Edit my.cnf file to update the new path

sudo vi /etc/mysql/my.cnf

Look for:

datadir = /var/lib/mysql

and update to:

datadir = /path/to/new/mysql

Restart the mysql daemon (best during off-peak hours in case of errors)

sudo /etc/init.d/mysqld restart

After few days or weeks if you feel confident and everything is good you can remove the old data:

sudo rm -rf /var/lib/mysql