How to change MySQL data directory
Information managed by the MySQL server is stored under a directory known as the data directory. The following list briefly describes the items typically found in the data directory, with cross references for additional information:
- Data directory subdirectories. Each subdirectory of the data directory is a database directory and corresponds to a database managed by the server. All MySQL installations have certain standard databases:
- The
mysql
directory corresponds to themysql
system schema, which contains information required by the MySQL server as it runs. This database contains data dictionary tables and system tables. - The
performance_schema
directory corresponds to the Performance Schema, which provides information used to inspect the internal execution of the server at runtime. - The
sys
directory corresponds to thesys
schema, which provides a set of objects to help interpret Performance Schema information more easily. - The
ndbinfo
directory corresponds to thendbinfo
database that stores information specific to NDB Cluster (present only for installations built to include NDB Cluster).
- The
Here is the steps to change Data directory in MySQL
- Stop MySQL using the following command:
sudo /etc/init.d/mysql stop
- Copy the existing data directory (default located in
/var/lib/mysql
) using the following command:sudo cp -R -p /var/lib/mysql /newpath
- edit the MySQL configuration file with the following command:
sudo gedit /etc/mysql/my.cnf # or perhaps /etc/mysql/mysql.conf.d/mysqld.cnf
- Look for the entry for
datadir
, and change the path (which should be/var/lib/mysql
) to the new data directory. - Look for lines beginning with
/var/lib/mysql
. Change/var/lib/mysql
in the lines with the new path. - Save and close the file
- Restart MySQL with the command:
sudo /etc/init.d/mysql restart
- Now login to MySQL and you can access the same databases you had before.