How to Move MySQL Database Data Directory
Posted on Wednesday July 27, 2011 by Eric Potvin
If for some reason your database expands at a high rate, you might need to consider to move 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
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 the my.cnf file to update the new path
sudo vi /etc/mysql/my.cnf
datadir = /var/lib/mysql
and update to:
datadir = /path/to/new/mysql
Restart the mysql deamon (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