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:
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
Most Popular Articles
Setup Teamspeak 3 Server on Linux (CentOS)
If you run a gaming community, a Teamspeak 3 server is probably a popular choice for you to have...
Disable root SSH access and setup sudo user on CentOS
Disabling root SSH logins and adding an additional sudo user is a good security measure for any...
Mojang Authentication Down (OpenJDK - Ubuntu/Debian)
If you're using Linux Ubuntu / Debian with Java OpenJDK (Spigot, Craftbukkit, Forge, etc.) and...
Force HTTPS / SSL for cPanel domain
Add the following to the top of the .htaccess file in your websites document root to force...
Resize KVM VPS Partition with GParted ISO
Resize a Linux OS partition to use extra drive space allocated after an upgrade. A few notes:1)...