
In a default MySQL installation on a Linux system, the MySQL data files are located under the /var/lib/mysql directory.
But on many Linux systems, this location might not be the best place. If the system disk is partitioned into several partitions, the size of file system where the ‘/var/lib/mysql’ directory belongs might not be dimensioned to hold the required databases.
Fixing this issue is easy, as explained in this post.
Configuring the directory that holds the MySQL data files
The “datadir” directive in the MySQL configuration file “/etc/mysql/my.cnf” specifies the directory where the datafiles are located.
The default setting for this directive is:
1 2 3 |
datadir = /var/lib/mysql |
Moving the data files
To move the data files, the database service must be stopped first:
1 2 3 |
$ sudo /etc/init.d/mysql stop |
Next, the datafiles are moved to the destination directory. For instance, if a filesystem has been prepared to hold the datafiles, and has been mounted as “/data”:
1 2 3 |
$ sudo mv /var/lib/mysql /data/mysql |
Next, edit the “/etc/mysql/my.cnf” configuration file to update the value of the “datadir” directive, and finally start again the database service:
1 2 3 4 5 6 7 |
$ sudo vi /etc/mysql/my.cnf ... datadir = /data/mysql ... $ sudo /etc/init.d/mysql start |
And that’s all!
—