How to Change MySQL Data Directory in Linux Centos

By default Linux CentOS or other Linux Distro installation, MySQL data directory is stored in /var/db/mysql, how can I change it to other directory in example /db/mysql?

It is always better to have MySQL Data Directory store in a specific partition/drive. It will help on performance and better management and scalability. You change change the data store directory in Linux by editing /etc/my.cnf file.

Edit /etc/my.cnf file


# vi /etc/my.cnf

Change the data directory structure


datadir=/db/mysql
socket=/db/mysql/mysql.sock

After the file has been updated, restart MySQL service.