Mostly MySQL uses /var/lib/mysql directory as default data directory for Linux based systems. Which are going to change to another location in this article.
Instruction’s to Change Default MySQL Data Directory:Follow the below steps to make all the changes. In some cases service name, default data directory or MySQL configuration file path change. So use all the command as per you system settings.
1. Stop MySQLBefore making any changes, first make sure to stop mysql service
# service mysqld stop
2. Change Data DirectoryNow copy default MySQL data directory (/var/lib/mysql) to other location as per your requirement. Also set the required MySQL ownership on new directory location. As per below command, we are relocating data directory to /data/mysql.
# cp -rap /var/lib/mysql /data/mysql # chown mysql.mysql /data/mysqlNow edit MySQL default configuration file /etc/my.cnf and update values of datadir and socket variable.
Change From: datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock Change To: datadir=/data/mysql socket=/data/mysql/mysql.sock
3. Start MySQLAfter making all above changes. finally start MySQL service. Now it will use new data directory path
# service mysqld startThis article has been tested with CentOS 6.5 with MySQL 5.5.37. If you face any issue during MySQL startup check mysql log file /var/log/mysqld.log for any errors.