How to change MySQL default database/data location

Lets say you just setup a RAID1 Array :D and you want everything that matters saved there, your web projects are the easies part, just copy the files and modify the vhost, but your databases are also vulnerable and are even more critical then the files. MySQL requires a few extra steps to change its data location and here they are:

Changing MySQL default database/data location

Step 1: Stop the mysql service

root@somehost# service mysql stop

Step 2: Copy the existing mysql files to the new directory you’ll be using

*Note: Chances are that the existing mysql files are in /var/lib/mysql so I’ll be using that location in my tutorial

cp -R /var/lib/mysql /home/raid1_md0/mysql

Step 3: Open the mysql config file my.cnf and modify the line starting with datadir to your new location

root@somehost# vi /etc/mysql/my.cnf


# * Basic Settings
user = mysql
pid-file = /var/run/mysqld/
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /home/raid1_md0/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql


Save the file by pressing “:w” and Enter and quit vim

Now you can start mysql by typing
service mysql start

And you’re done