Moving MySQL Data directory to different location

  • KM00965731
  • 30-May-2014
  • 26-Apr-2021

Summary

Here you will find the steps you will need to follow when you need to move your MySQL data to a different location

Question

Here you will find the steps you will need to follow to change the directory of MySQL data to another partition or directory.

Answer

1.       Stop MySQL using the following command:
sudo /etc/init.d/mysql stop
 
2.       Copy the existing data directory (default located in /opt/mysql/data) using the following command:
sudo cp -R -p /opt/mysql/data/ /<newpath>/
 
Where <newpath> is the new location you need to move the data. In my example is tmp/MySQL-Test
 
Example: sudo cp -R -p /opt/mysql/data/ /tmp/MySQL-Test
 
3.       Edit the following MySQL configuration files:
  •  /opt/NA/server/conf/my.cnf
  • /etc/my.cnf
                There are two lines on each file. Taking the last example, both should look like this:
                datadir = /tmp/MySQL-Test/data
                innodb_data_home_dir = /tmp/MySQL-Test/data
 
4.       Restart MySQL services:
 
sudo /etc/init.d/mysql restart