How to Change MySQL Data Directory in Linux

here'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:
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

We need to change these to our new location:
datadir=/ssd/mysql
socket=/ssd/mysql/mysql.sock

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

  • mariadb, change data directory, mysql, database
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

CentOS 7.5 - NGINX (Mainline), PHP 7.3, LetsEncrypt SSL - Installation & Configuration

NGINX is superior to most other web servers in regards to performance, potential to mitigate...

Force HTTPS / SSL for cPanel domain

Add the following to the top of the .htaccess file in your websites document root to force...