The MySQL data directory resides at var/lib/mysql by default. When you want to change this default directory then follow the below steps. This method of changing MySQL data directory will help to think of mounting an additional disk on your system.

The data directory changing procedure includes three major steps. They are,

Step 1: Before starting the process we should stop the mysql service by stopping the mysql daemon (mysqld) program. Use the following command to stop the mysqld.

service mysql stop

Step 2: Now we need to copy all the present data to the new directory, through this process we can avoid the loss of old data due to directory path change. The following commands copy the databases from /var/lib/mysql to the new location as per your wish. Here we move into newdata/mysql.

cp -rap /var/lib/mysql /newdata/mysql

Now change the ownership of the new data directory, by assigning mysql user to this new path

chown mysql.mysql /newdata/mysql

Step 3: Once all the existing data are copied to the new directory, update this new path in the default mysql configuration file. Find the etc/my.cnf  file and replace the below lines

datadir=/data/mysql
socket=/data/mysql/mysql.sock

Step 4: After completing all the above steps start the mysql service, it will use the new path.

The command to start the mysql service daemon is

service mysqld start

Categorized in:

Tagged in:

, ,