There's many reasons that you might want to change the directory that your MySQL data is stored in, such as your partition running out of space or you need to move to a secondary drive. This is an easy task to do, check below. This should work on most Linux distributions and has been tested on CentOS 6.6 and Ubuntu 14.04.

First stop your MySQL server:
service mysqld stop

Now copy your current MySQL data directory to the new directory that you want to use. For this guide we will use "/ssd/mysql" as our new location:
cp -rap /var/lib/mysql /ssd/mysql

Next set proper ownership of the files:
chown mysql.mysql /ssd/mysql

Now we need to edit our MySQL server configuration to reflect the changes, use your favorite text editor to make the changes. We will use nano:
nano /etc/my.cnf

Look for the two following lines:

We need to change these to our new location:

Save the file and start your MySQL server:
service mysqld start

Your MySQL server will now be using your new data directory.

Update: With certain OS and MySQL versions the above changes may not work, and a symlink will work fine. Use the following steps after you've installed your SQL server.

service mysqld stop / service mariadb stop
mv /var/lib/mysql /home/mysql
ln -s /home/mysql /var/lib/mysql
service mysqld start / service mariadb start

Was this answer helpful? 28 Users Found This Useful (713 Votes)